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.

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

I want to give listings and somehow mark a particular set of lines. My preference would be to make them italic. (The purpose of wanting to do this is so I can say "these lines are required but you do not yet need to understand them"). I am open to other ways of marking the lines (bold, putting a star next to them etc.

How should I go about doing this?

share|improve this question
Numbering them isn't enough? – vonbrand Feb 5 '13 at 12:54
No, I don't want to say "disregard lines 12, 15, 18 through 21 and 29". – Joe Feb 5 '13 at 12:56
Ah, sorry, I can see how my question was ambiguous. I have edited it. – Joe Feb 5 '13 at 12:59
Maybe color would be a way to go. For some suggestions see here: tex.stackexchange.com/questions/36911/… – myrtille Feb 5 '13 at 14:37
That looks very hopeful, thanks. If it works for me I'll close this question as a duplicate. – Joe Feb 5 '13 at 15:07
up vote 2 down vote accepted

You could use escaping to latex to emphasize the intended lines.

Check out Chapter 4.14 Escaping to LATEX of the Listings package manual.

Or, you could refer to certain line numbers.

Check out Chapter 7 How to's

This is some code of mine: i searched for all running processes on my machine and fed the output to grep. grep then searched for the pattern 'Foam'. I marked all matches red in the listing.

\begin{lstlisting}[escapechar=§,caption={In der Liste der laufenden Prozesse suchen},label=lst:listProcessesFindFoam] 
user@host:~$ ps -ef | grep Foam
user  11005  5117  0 17:11 pts/2    00:00:05 mpirun -np 4 twoPhaseEuler§\color{red}{Foam}§ -parallel
user  11006 11005 99 17:11 pts/2    00:40:27 twoPhaseEuler§\color{red}{Foam}§ -parallel
user  11007 11005 99 17:11 pts/2    00:40:28 twoPhaseEuler§\color{red}{Foam}§ -parallel
user  11008 11005 99 17:11 pts/2    00:40:27 twoPhaseEuler§\color{red}{Foam}§ -parallel
user  11009 11005 99 17:11 pts/2    00:40:26 twoPhaseEuler§\color{red}{Foam}§ -parallel
user  11673 11116  0 17:52 pts/12   00:00:00 grep --color=auto §\color{red}{Foam}§
user  32041     1  0 Aug01 ?        00:00:31 evince /tmp/lyx_tmpdir.J18462/lyx_tmpbuf0/open§\color{red}{Foam}§UserManual_CDLv2.pdf
share|improve this answer
Can I change the background color of code line this way? – Masud Rahman Mar 5 '14 at 8:10
Try \colorbox instead of \color. Between the escape-characters (in this case the §) you can state any latex code. So coloring the lines in the listing works the same as coloring any text in latex. – Dohn Joe Mar 5 '14 at 8:24

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.