TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Friends, I'm stuck with a unusual situation: I'm trying to highlight specific words of a TeX code inside a verbatim environment. I know listings can help me by defining a set of keywords, but in this case I want to highlight specific words in a specific line, e.g.,

My code

My first attempt was using one of the examples in the fancyvrb documentation:




\textit{% This is a comment}
First verbatim line.
\fbox{Second} verbatim line.
\textcolor{red}{Third} verbatim line.


It works quite well. The problem is, when I try to escape \:

\textit{% This is a comment}
This line has \textcolor{blue}{\\textbf}\{this command\} highlighted.

it appears to insert a tabulation instead:

My attempt 1

My second attempt was indeed using listings:






This line has |\keyword{textbf}|{this command} highlighted.

But not \textbf{this one}.

My attempt 2

Yay! Unfortunatelly, I could not make this idea work for math, since when inside \keyword, the math stuff is still active (I guess). :(

I'd like to be able to highlight other characters too, like $ and { }. I was thinking of at most 4 colors available, say, red, blue, grey and green:

My code 1

I'm open to suggestions. Does anybody have an idea? :)

share|improve this question
up vote 24 down vote accepted

I would use a replacement by literate:



        texcl,%set comments in an extra line

Good morning <B>{sunshine}, the Earth says <E>{hello}! % it's from a song
See <$>a^2=b^2+c^2<$$> and tell me if you understand it. 
I don't want colors here, \emph{they are bad}. %true story
I can even understand $\pi$. % I prefer pie

enter image description here

share|improve this answer
Fantastic, Marco! Thanks a lot! (I'm out of votes now, but I'll upvote it ASAP). :) – Paulo Cereda Jan 20 '12 at 13:04
its not like verbatim know.. because this not working when I used tabular environment inside the lstlisting environment. – David Mar 10 at 16:55

Your Answer


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.