# XML highlighting - different style for attributes

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?

• Has there been any development on this yet? Does the answer provided help any or not? – Edd Apr 6 '11 at 7:56
• @Edd I'm sorry, i've already ran the presentation without using your advice, anyway, i'll take a look at it soon and if it help, i swear, i'll give you an upvote/accept-question-mark. I'm busy right now, just give me few more days... – Radek Simko Apr 6 '11 at 8:36
• Absolutely no problem. I was just curious as to whether this fixed your problem or whether there was something deeper to investigate. I'm not so fussed about the upvote/answer to be honest! – Edd Apr 6 '11 at 9:30
• Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – jubobs Jan 15 '14 at 23:35

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}


\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
}


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}


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