10

I need to highlight syntax in my LaTeX presentation, so I've found this answer: XML syntax highlighting

\lstdefinelanguage{XML}
{
  morestring=[b]",
  morestring=[s]{>}{<},
  morecomment=[s]{<?}{?>},
  stringstyle=\color{black},
  identifierstyle=\color{darkblue},
  keywordstyle=\color{cyan},
  morekeywords={xmlns,version,type},
  backgroundcolor=\color{lightgray},
  numbers=left,
  numberstyle=\footnotesize\ttfamily\color{gray},
  numbersep=0.5pt
}

Unfortunately I didn't find any way, how to specify extra style for text in attributes.

For example:

<xml>
    <person age="22" sex="female">Ann</person>
</xml>

It the case above, Ann and 22, female would have the same style... How to separate styles for those two things?

Radek Simko
  • 333
  • 1
  • 4
  • 9

2 Answers2

10

When you add the color package and change

morestring=[b]",

into

morestring=[b][\color{red}]",

the attributes will color red.

This will color the attributes red in your xml listings.

\begin{lstlisting}[language=xml, frame=single]
<xml>
    <person age="22" sex="female">Ann</person>
</xml>
\end{lstlisting}

enter image description here

\documentclass{article}

\usepackage{listings}
\usepackage{color}
\definecolor{lightgray}{rgb}{.7,.7,.7}
\definecolor{gray}{rgb}{.4,.4,.4}
\definecolor{darkblue}{rgb}{0,0,.3}
\begin{document}

\begin{lstlisting}[language=xml, frame=single]
<xml>
    <person age="22" sex="female">Ann</person>
</xml>
\end{lstlisting}

 \lstdefinelanguage{XML}
{
  morestring=[b][\color{red}]",
  morestring=[s]{>}{<},
  morecomment=[s]{<?}{?>},
  stringstyle=\color{black},
  identifierstyle=\color{darkblue},
  keywordstyle=\color{cyan},
  morekeywords={xmlns,version,type},
  backgroundcolor=\color{lightgray},
  numbers=left,
  numberstyle=\footnotesize\ttfamily\color{gray},
  numbersep=0.5pt
}

\begin{lstlisting}[language=xml, frame=single]
<xml>
    <person age="22" sex="female">Ann</person>
</xml>
\end{lstlisting}

\end{document}




 \lstdefinelanguage{XML}
{
  morestring=[b][\color{red}]",
  morestring=[s]{>}{<},
  morecomment=[s]{<?}{?>},
  stringstyle=\color{black},
  identifierstyle=\color{darkblue},
  keywordstyle=\color{cyan},
  morekeywords={xmlns,version,type},
  backgroundcolor=\color{lightgray},
  numbers=left,
  numberstyle=\footnotesize\ttfamily\color{gray},
  numbersep=0.5pt
}
David Carlisle
  • 757,742
Andra
  • 817
  • 7
  • 14
6

The minted package produces code which has tag-names, attribute-names, attribute-values and tag-contents printed differently, so the following code would produce the output which follows it:

\documentclass{minimal}
\usepackage{minted}
\begin{document}
\begin{minted}{xml}
    <xml>
        <person age="22" sex="female">Ann</person>
    </xml>
\end{minted}
\end{document}

Screenshot of sample minted XML output

Note, however, that the minted package use a python program called pygments, which is fairly straightforward to install as detailed in the minted documentation.

Edd
  • 1,122