4

I'm trying to make some parts in HTML code in a lstlisting coloured by using \textcolor in an escaped sequence. I've used this technique in another code snippet where everything worked out fine. For some reason the only time when I don't get an error while compiling is when I use german quotation marks, but they don't show up properly. The code as condensed example is basically this:

\documentclass[11pt,a4paper,xcolor=dvipsnames]{report}
\usepackage[latin9]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{listings}
\usepackage[dvipsnames]{xcolor}

\lstset{basicstyle=\footnotesize\ttfamily,frame=leftline,escapechar=@}

\begin{document}
\begin{lstlisting}[language=HTML]
...
<input type="text" @\textcolor{Green}{id="`hex"'}@ /> 
... 
<input type="text" @\textcolor{Green}{id="`string"'}@ />
\end{lstlisting}
\end{document}

It comes up like this:

enter image description here

So what I need to know is why does this happen, what can I do about it and can I possibly get it to work with english quotation marks?

4

You may change the current language via the new command \entextcolor (using \foreignlanguage from babel):

\documentclass[11pt,a4paper,xcolor=dvipsnames]{report}
\usepackage[latin9]{inputenc}
\usepackage[english,ngerman]{babel}
\usepackage{listings}
\usepackage[dvipsnames]{xcolor}

\lstset{basicstyle=\footnotesize\ttfamily,frame=leftline,escapechar=@}

\newcommand\entextcolor[2]{\foreignlanguage{english}{\textcolor{#1}{#2}}}

\begin{document}
\begin{lstlisting}[language=HTML]
...
<input type="text" @\entextcolor{Green}{id="`hex"'}@ /> 
... 
<input type="text" @\entextcolor{Green}{id="`string"'}@ />
\end{lstlisting}
\end{document}

enter image description here

|improve this answer|||||
  • Thank you so much, works fine! Still very new to LaTeX so there's a lot to learn – Lunaetic Jul 19 '16 at 9:39

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.