Discussion:
Syntax for loading listings languages
(too old to reply)
Peter Flynn
2024-02-28 00:58:44 UTC
Permalink
Using the listings package. I am curious as to why the LaTeX language
has to be loaded and used as [LaTeX]TeX, but a language or dialect I
define myself can be both loaded and used by simple name alone.

===================================================================
\documentclass{article}

\usepackage{listings}
\lstdefinelanguage{DocBook}[]{XML}{morekeywords={chapter,para}}
\lstdefinelanguage{LaTeXe}[LaTeX]{TeX}{morekeywords={DeclareLanguageMapping}}
\lstloadlanguages{DocBook,[LaTeX]TeX,LaTeXe}

\begin{document}

\begin{lstlisting}[language={[LaTeX]TeX}]
\tableofcontents
\end{lstlisting}

\begin{lstlisting}[language=DocBook]
<chapter><para>
\end{lstlisting}

\begin{lstlisting}[language=LaTeXe]
\DeclareLanguageMapping
\end{lstlisting}

\end{document}
===================================================================

Peter
Holger Schieferdecker
2024-02-28 11:45:57 UTC
Permalink
Post by Peter Flynn
Using the listings package. I am curious as to why the LaTeX language
has to be loaded and used as [LaTeX]TeX, but a language or dialect I
define myself can be both loaded and used by simple name alone.
Maybe I don't get your point, but I want to share my thoughts on that
issue. Let me add that I'm not an expert with listings.

As far as I understand the manual there are single languages and
languages with one or more dialects. In the former case you can load the
language by using its name. For languages with dialects you need to
specify which dialect you want to use. But it is possible to define a
default dialect for a language. In that case you can load the language
with that dialect by simply using the language name. According to the
manual for TeX the default dialect is plain. So just loading TeX as
language would invoke plain TeX.

In your example below you define a new language called LaTeXe based on
[LaTeX]{TeX}. Now LaTeXe is a language without dialects and can be
loaded without specifying one.

If I understand you correctly you would like to load [LaTeX]{TeX} by
just specifying LaTeX. Which doesn't work.

Now I tried something which presumably doesn't make sense, but it works.

\lstdefinelanguage[Peter]{MyHTML}[]{HTML}{...}
\lstdefinelanguage[Peter]{MyXML}[]{XML}{...}

You can define several languages with the same dialect name. So
specifying just the dialect is not distinct.

%%%%%
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\lstdefinelanguage[Peter]{MyHTML}[]{HTML}{morekeywords={Peter}}
\lstdefinelanguage[Peter]{MyXML}[]{XML}{morekeywords={Peter}}

\begin{document}

\begin{lstlisting}[language={[Peter]{MyHTML}}]
Hallo Peter
\end{lstlisting}

\begin{lstlisting}[language={[Peter]{MyXML}}]
Hallo Peter
\end{lstlisting}

\end{document}
%%%%%

I hope this helps a bit.

Holger
Peter Flynn
2024-02-28 16:31:11 UTC
Permalink
Post by Holger Schieferdecker
Post by Peter Flynn
Using the listings package. I am curious as to why the LaTeX
language has to be loaded and used as [LaTeX]TeX, but a language or
dialect I define myself can be both loaded and used by simple name
alone.
Maybe I don't get your point,
I explained it badly. I should have written "a language or dialect I
define myself, either completely new or based on an existing dialect of
an existing language".
Post by Holger Schieferdecker
As far as I understand the manual there are single languages and
languages with one or more dialects. In the former case you can load
the language by using its name. For languages with dialects you need
to specify which dialect you want to use. But it is possible to
define a default dialect for a language. In that case you can load
the language with that dialect by simply using the language name.
As I understand it, you can invoke the default dialect by loading
[]{lang} (ie empty square brackets).
Post by Holger Schieferdecker
According to the manual for TeX the default dialect is plain. So just
loading TeX as language would invoke plain TeX.
Right.
Post by Holger Schieferdecker
In your example below you define a new language called LaTeXe based
on [LaTeX]{TeX}. Now LaTeXe is a language without dialects and can be
loaded without specifying one.
OK. That's what I hoped. I need to define some extra commands not in the
[LaTeX] dialect.
Post by Holger Schieferdecker
If I understand you correctly you would like to load [LaTeX]{TeX} by
just specifying LaTeX. Which doesn't work.
No, I just wanted to make sure of the rules.
Post by Holger Schieferdecker
Now I tried something which presumably doesn't make sense, but it works.
\lstdefinelanguage[Peter]{MyHTML}[]{HTML}{...}
\lstdefinelanguage[Peter]{MyXML}[]{XML}{...}
You can define several languages with the same dialect name. So
specifying just the dialect is not distinct.
Right, because they are dialects of a different base language.
Post by Holger Schieferdecker
I hope this helps a bit.
Yes, thank you. In effect, for my purposes the *only* language to load
that requires a dialect is [LaTeX]{TeX}, as far as I can see.

Peter

Loading...