4

How can you set various words in your lstlisting to various text colors?

For instance, I want the word "fail" to appear in red and the word "pass" in green.

I have the feeling there should be some easy escape possibility!?

What did not work

I tried using the parameter

escapeinside={(*@}{@*)}

and then within the lstlisting

(*@\color{red}@*) fail (*@\color{black}@*)

But this has no effect.

Therefore, I've tried

$\textcolor{red}{\lstinline!fail!}$

but listings does not like nested lstinline within lstlistings.

Complex solution

a not quite suitable solution is described in \textcolor within lstlisting?: it is intended for one color only, so I would have to add something like

moredelim=**[is][\color{red}]{@}{@},

for every color I use :(

2 Answers 2

7

The escaped part is a group, so color command issued there are lost at the end. There is no official way to set a color globally but you can look here How can I change the text color in such a way that the effect transcends groups?.

Beside this you can try to set the color with \aftergroup (but color of keywords will overwrite this settings):

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{listings}

\begin{document}

\def\redcolor{\color{red}}
\def\blackcolor{\color{black}}
\begin{lstlisting}[escapechar=@]
#@\aftergroup\redcolor@include@\aftergroup\blackcolor@ <fstream.h>
void main () {
fail
pass
}
\end{lstlisting}

\end{document}
4
  • Hey, thanks for both advices :) Are you sure about the escape? I picked up the escape sequence {(*@} in the (old) lstlisting manual, and it works fine so far (mainly for labels). Jul 24, 2013 at 21:28
  • Why does your example require xcolor? It should work just as well for the color package, right? Jul 24, 2013 at 21:29
  • 2
    No I'm not sure about the escape - and after some test I think I mixed it up with something else. I edit the answer. And regarding the color: You didn't provide an example so I used Gonzalos as starting point. You can use color instead if you want, but I would prefer xcolor. Jul 25, 2013 at 6:56
  • Perfect for cases where keywords do not work. +1
    – u17
    Nov 17, 2019 at 20:21
13

One option would be to use additional keyword sets:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{listings}

\lstset{
language=C++,
basicstyle=\small\ttfamily,
columns=fullflexible,
keywords=[2]{fail},
keywords=[3]{pass},
keywordstyle={\color{blue!80!black}},
keywordstyle=[2]{\color{red!80!black}},
keywordstyle=[3]{\color{green!50!black}},
}

\begin{document}

\begin{lstlisting}
#include <fstream.h>
void main () {
fail
pass
}
\end{lstlisting}

\end{document}

enter image description here

3
  • Works great in my case, thanks (+1) If no answer comes up that is more flexible (for other people that might want to mark the same word in different colours), I'll mark this answer as accepted in a couple of days. Jul 24, 2013 at 13:14
  • In this particular case, I prefer to use emph and emphstyle instead of keywords and keywordstyle because "fail" and "pass" are not keywords of C++. Jul 25, 2013 at 9:16
  • Though this is a great answer, I accepted Ulrike's, since aftergroup is the more flexible solution and explains and resolves the core problem. Jul 25, 2013 at 11:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .