2

I'd like to implement some XML code in my document. However, the syntax of my listings doesn't seem to be acknowledged. So my output looks like this right now:

Screenshot of current XML output

Obviously, that doesn't make any sense. I tried to redefine the keywords, using "more keywords" but that didn't cause the desired effect. I'm using the listings package. Could anyone help me with a correct syntax definition for my XML Listings? (It's SAPUI5 identifiers, that's why some of them are not "common")

Here's the necessary code to reproduce my document:

\documentclass[11pt, a4paper, toc = bibliography, toc = listof,twoside,table,titlepage]{scrartcl}
\usepackage{listings}
\usepackage{color}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstdefinelanguage{JavaScript}{
  keywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, if, in, while, do, else, case, break},
  keywordstyle=\color{blue}\bfseries,
  ndkeywords={class, export, boolean, throw, implements, import, this},
  ndkeywordstyle=\color{blue}\bfseries,
  identifierstyle=\color{black},
  sensitive=false,
  comment=[l]{//},
  morecomment=[s]{/*}{*/},
  stringstyle=\color{red}\ttfamily,
  morestring=[b]',
  morestring=[b]"
}
\lstset{frame=tb,
  language=JavaScript,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numbers=left,
  numberstyle=\tiny\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=3
}

\begin{document}
\begin{lstlisting}[caption={Aggregation Binding über die View} ,captionpos=b, language=XML,  label=lst:aggregationBinding]
<Table id="table" items="{Students}>
    <columns>
        <Column id="matrikelnummer">
            <header>
                <Text text="Matrikelnummer:"/>
            </header>
        </Column>
        <Column id="name">
            <header>
                <Text text="Name:"/>
            </header>
        </Column>
    </columns>
    <items>
        <ColumnListItem>
            <cells>
                <ObjectIdentifier text="{Matrikelnummer}"/>
                <ObjectIdentifier text="{Name}"/>
            </cells>
        </ColumnListItem>
    </items>
</Table>
\end{lstlisting}
\end{document}
moewe
  • 175,683
sonja
  • 123
  • Excuse me, but your document is created with LaTeX? If is possible can you add, please? – Sebastiano Dec 07 '18 at 10:49
  • 1
    You may want to look into the Pygments-based syntax highlighting provided by the minted package. Because it uses Python and not TeX its highlighting capabilities can be more advanced in some areas (and may be easier to tweak). Possibly related: https://tex.stackexchange.com/q/10255/35864 – moewe Dec 07 '18 at 10:57
  • sorry for not providing everything necessary! I added my code now (I hope that'S everything relevant)! @Sebastiano – sonja Dec 07 '18 at 11:10
  • @moewe is this sufficient as a MWE ? :) And I'll look into your reference.. but I would think that it should be possible with the listings package.. – sonja Dec 07 '18 at 11:10
  • 1
    Almost: It is missing a \documentclass. Ideally we can just paste and run the example without adding any additional code (see the link I gave above). You may also want to remove the excessive indent of the lines (probably introduced by the code highlighting). I'm a bit confused that all your \lst... settings are for JavaScript, but your question is about XML. Do you have a specific setup for XML already? (And I'm still not sure on the expected/desired output.) – moewe Dec 07 '18 at 11:13
  • @moewe My list settings are for javascript since I'm also using listings with javascript content, but the package doesn't preset anything for javascript. but for XML everything should be predefined thatÄs why i didn't define anything else for that language.. – sonja Dec 07 '18 at 11:14
  • @sonja Perfect, and thank you for your cooperation and patience. – Sebastiano Dec 07 '18 at 11:30
  • Thank you for making the example compilable. I have removed the excessive indentation to make the code easier to read (if I have removed something important, please add it back in and accept my apologies). Since I'm still not sure what the expected output for the XML highlighting would be I'm afraid I can't be of any great use. – moewe Dec 07 '18 at 13:30
  • @moewe my expected output is the correct highlighting of the XML syntax. So everything in <> brackets should be the same color, every codeword like "text" should be the same color and every string like "matrikel nummer" should be the same color. So that you can easily see the structure of my XML code. I have other listings in my document that contain JavaScript code and that's why I defined the JavaScript language. But I have the feeling that this overwrote the XML syntax highlighting or the package "listing" can't identify the keywords here.. – sonja Dec 07 '18 at 13:49
  • You are quite probably missing a closing " in items="{Students}, that should probably read items="{Students}". With that quotation mark back in all strings have the same colour and the rest is black. You now need to manually add all keywords/code words (like text) that need a different colour. – moewe Dec 07 '18 at 13:56
  • haha oops you're right. but now how do I add the keywords and identifiers? everything except for the "strings" is black. but I'd like e.g. to be in a different color and then "id" also in another different color.. :/
    – sonja Dec 07 '18 at 14:55

1 Answers1

3

The main reason why the syntax highlighting is off is the missing " in items="{Students}. This throws listings' string highlighting off quite a bit (but I assume an XML parser would also complain, so ...).

With the " added in the output is already very sensible, but the presets for XML in listings don't know a great deal of predefined keywords. So if you want to colour things like text, you need to add them yourself. As far as I can see the easiest way to extend an existing language is by defining a new style (see Extend a language with additional keywords?).

\documentclass[11pt, a4paper]{scrartcl}
\usepackage{listings}
\usepackage{xcolor}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstset{frame=tb,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numbers=left,
  numberstyle=\tiny\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=3
}

\lstdefinestyle{XML}{
  language=XML,
  morekeywords={text,id},
}

\begin{document}
\begin{lstlisting}[caption={Aggregation Binding über die View} ,captionpos=b, style=XML,  label=lst:aggregationBinding]
<Table id="table" items="{Students}">
    <columns>
        <Column id="matrikelnummer">
            <header>
                <Text text="Matrikelnummer:"/>
            </header>
        </Column>
        <Column id="name">
            <header>
                <Text text="Name:"/>
            </header>
        </Column>
    </columns>
    <items>
        <ColumnListItem>
            <cells>
                <ObjectIdentifier text="{Matrikelnummer}"/>
                <ObjectIdentifier text="{Name}"/>
            </cells>
        </ColumnListItem>
    </items>
</Table>
\end{lstlisting}
\end{document}

gives

Highlighted XML code: Strings and keywords (currently only <code>text</code> and <code>id</code>) are highlighted.

In XML syntax highlighting you may find more extended styles for XML highlighting.

If you need more complex rules or hope for a more automatic solution, maybe minted is worth a look. minted is based on Pygments and can make use of the power of Python to highlight your code.

moewe
  • 175,683