2

I am trying to implement syntax highlighting in listings for XML. As a follow-up to this answer, I have tried to grey-out the delimiters themselves, but have not had much luck despite a few attempts:

\documentclass{article}
\pagestyle{empty}% for cropping
\usepackage{listings}
\usepackage{xcolor}

% colours
\colorlet{cdel}{blue}   % delimiters
\colorlet{ckey}{green}  % keywords
\colorlet{ccom}{gray}   % comments
\colorlet{cstr}{brown}  % strings
\colorlet{cide}{orange} % identifiers
\colorlet{ctxt}{black}  % text

% sets a function for moredelim to try to add
%   colored delimiters to the start and end
%      see: https://tex.stackexchange.com/a/147848/1232
\def\beginlstdelim#1#2#3#4%
{%
    \def\endlstdelim{#2\egroup}%
    \color{#3}#1\bgroup\color{#4}\aftergroup\endlstdelim%
}

\lstdefinestyle{lst}{
    frame={single},
    basewidth=0.5em,    
    basicstyle={\ttfamily\footnotesize},   
    identifierstyle=\color{cide},
    keywordstyle=\color{ckey},
    stringstyle=\color{cstr},
    commentstyle=\color{ccom},
    tabsize=2,
    showtabs=false,
    showspaces=false,
    showstringspaces=false,
    extendedchars=true,
    columns=fixed,
    basewidth=0.5em
}

\lstdefinelanguage{xml}{ 
    keywords={xml},
    %literate={<}{{{\color{cdel}<}}}{1} {>}{{{\color{cdel}>}}}{1} {:}{{{\color{cdel}:}}}{1} {/}{{{\color{cdel}/}}}{1} {?}{{{\color{cdel}?}}}{1} {=}{{{\color{cdel}=}}}{1},  
       %^^ attempt 1: delimiters don't work
    %moredelim=**[is][\beginlstdelim{>}{<}{cdel}{ctxt}]{>}{<}, 
       %^^ attempt 2: messes with formatting    
    morestring=[s][\color{ctxt}]{>}{<},
    morestring=[b]",
    morecomment=[s]{!--}{--},
    morecomment=[s]{?}{?}
}

\begin{document}

\begin{lstlisting}[style=lst,language=xml]
<?xml version="1.0" encoding="UTF-8"?>
<mwe>
  <num>1</num>  <!-- the number is black -->
  <string xml:lang="en">Hello there</string>
  <comment>Why is this not black?</comment>
</mwe>
\end{lstlisting}
\end{document}

The code above gives:

enter image description here

(Don't worry; these are not the colours I will use in the end, but just used here to distinguish the different parts.)

What I would like is to grey out the delimiters (e.g, <, >, /, etc.). The code also includes a couple of (failed) attempts I made.

The first attempt was to use literate to append a color to the delimiters, but then other parts of the code cannot find those delimiters. The second attempt was to try to adapt this answer using a custom function on moredelim=**[is] to append and prepend coloured delimiters, but this messes with the formatting a lot, plus it misses the opening < character. I know I can turn the delimiters grey by setting the `basicstyle`` grey, but then the text turns grey too.


Any ideas on how I can format the delimiters seperately from their content?

3

If you want every < and > to have the same color, you can use low-level formating:

\documentclass{article}
\pagestyle{empty}% for cropping
\usepackage{listings}
\usepackage{xcolor}

% colours
\colorlet{cdel}{blue}   % delimiters
\colorlet{ckey}{green}  % keywords
\colorlet{ccom}{gray}   % comments
\colorlet{cstr}{brown}  % strings
\colorlet{cide}{orange} % identifiers
\colorlet{ctxt}{black}  % text

% sets a function for moredelim to try to add
%   colored delimiters to the start and end
%      see: https://tex.stackexchange.com/a/147848/1232
\makeatletter
\lst@CCPutMacro
    \lst@ProcessOther {"3C}{\lst@ttfamily{\textcolor{blue}{<}}{\textcolor{blue}{<}}}
     \lst@ProcessOther
     {"3E}{\lst@ttfamily{\textcolor{blue}{>}}{\textcolor{blue}{>}}}
    \@empty\z@\@empty
\makeatother

\lstdefinestyle{lst}{
    frame={single},
    basewidth=0.5em,
    basicstyle={\ttfamily\footnotesize},
    identifierstyle=\color{cide},
    keywordstyle=\color{ckey},
    stringstyle=\color{cstr},
    commentstyle=\color{ccom},
    tabsize=2,
    showtabs=false,
    showspaces=false,
    showstringspaces=false,
    extendedchars=true,
    columns=fixed,
    basewidth=0.5em
}

\lstdefinelanguage{xml}{
    keywords={xml},
    morestring=[s][\color{ctxt}]{>}{<},
    morestring=[b]",
    morecomment=[s]{!--}{--},
    morecomment=[s]{?}{?}
}

\begin{document}

\begin{lstlisting}[style=lst,language=xml]
<?xml version="1.0" encoding="UTF-8"?>
<mwe>
  <num>1</num>  <!-- the number is black -->
  <string xml:lang="en">Hello there</string>
  <comment>Why is this not black?</comment>
</mwe>
\end{lstlisting}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.