[Tex/LaTex] lstlistings – How to highlight single quotes og keywords

highlightinglistingspunctuation

I want to highlight some keywords in a listing. One keyword has an apostrophe. I searched and read already quite some time and found a couple of postings that textcomp could be very helpful for quotes.

Following a minimal example. The goal would be do color "Don't" also orange.
Does anybody know my mistake?

Best, TomBoo

\documentclass[a4paper, 12pt]{article}

\usepackage{listings, xcolor}
\usepackage{textcomp}

\definecolor{darkblue}{rgb}{0.0,0.0,0.6} 

\lstdefinelanguage{XML} 
{ 
  identifierstyle=\color{darkblue}, 
}

\lstdefinestyle{my-xml-style}
{
                    basicstyle=\ttfamily\footnotesize,  % Nichtproportionale Schrift, klein für den Quellcode
                    extendedchars=true,                                 % Alle Zeichen vom Latin1 Zeichensatz anzeigen.
                    keywords=[1]{attribute, xmlns},
                    keywordstyle=[1]\color{red},
                    keywords=[2]{Tove, Jani, Reminder},
                    keywordstyle=[2]\color{cyan},
                    keywords=[3]{Don\'t, forget, me, this, weekend, Don\textquotesingle t},
                    keywordstyle=[3]\color{orange},                 
                    morestring=[b]", 
                    stringstyle=\color{violet},
                    frame=tblr
}
\lstdefinelanguage{myxml}{language=XML,style=my-xml-style}



\begin{document}

\begin{lstlisting}[language=myxml]
<note attribute="main" xmlns="http://www.w3.org/1999/xhtml">
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>
\end{lstlisting}

\end{document}

Best Answer

The problem is that the character ' is not recognised as letter or digit so it can't be used in a keyword.

If you want to do it, you can tell listings that you want ' to behave as a letter, adding

alsoletter={'}

to your style.

The following MWE

\documentclass[a4paper, 12pt]{article}

\usepackage{listings, xcolor}
\usepackage{textcomp}

\definecolor{darkblue}{rgb}{0.0,0.0,0.6}

\lstdefinelanguage{XML}
{
  identifierstyle=\color{darkblue},
}

\lstdefinestyle{my-xml-style}
{
                    basicstyle=\ttfamily\footnotesize,  % Nichtproportionale Schrift, klein für den Quellcode
                    extendedchars=true,                                 % Alle Zeichen vom Latin1 Zeichensatz anzeigen.
                    keywords=[1]{attribute, xmlns},
                    keywordstyle=[1]\color{red},
                    keywords=[2]{Tove, Jani, Reminder},
                    keywordstyle=[2]\color{cyan},
                    keywords=[3]{Don't, forget, me, this, weekend},
                    keywordstyle=[3]\color{orange},
                    alsoletter={'},
                    morestring=[b]",
                    stringstyle=\color{violet},
                    frame=tblr
}
\lstdefinelanguage{myxml}{language=XML,style=my-xml-style}



\begin{document}

\begin{lstlisting}[language=myxml]
<note attribute="main" xmlns="http://www.w3.org/1999/xhtml">
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>
\end{lstlisting}

\end{document}

gives the desired result:

enter image description here