Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 :(

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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}
share|improve this answer
    
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). –  DaveBall aka user750378 Jul 24 '13 at 21:28
    
Why does your example require xcolor? It should work just as well for the color package, right? –  DaveBall aka user750378 Jul 24 '13 at 21:29
1  
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. –  Ulrike Fischer Jul 25 '13 at 6:56

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

share|improve this answer
    
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. –  DaveBall aka user750378 Jul 24 '13 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++. –  Paul Gaborit Jul 25 '13 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. –  DaveBall aka user750378 Jul 25 '13 at 11:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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