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

I'm using the listings package and include code into my document through \lstincludelistings. The problem is that I want to set some code lines to bold to indicate changes from previous code segments.

How can I achieve this without having to copy-paste the code directly into the document?

share|improve this question
up vote 6 down vote accepted

A solution would be to define a macro that invokes \lstinputlisting three times (for the part before the line to emphasize, the line itself, and the part after the line to emphasize) and adopts the listing style with \lstset to achive the desired highlighting:


\usepackage{xcolor}   % for coloring as emph
\usepackage{etoolbox} % for \ifnumcomp
\usepackage{beramono} % a tt font that has a boldface series


% The style to be applied for the highlighted line:

% input listing and emphasize a specific line
% \lstinputemph[listings options]{line number}{filename}
 \lstset{aboveskip=0pt, belowskip=0pt, showlines=true,#1}%
 }% else


With file hello.c as follows

#include <stdio.h>
void main() {
  printf("Hello, World\n")

This lead to the following output (second example uses a different background color for the highlighting effect)

Example Output 1

Example Output 2

share|improve this answer
That's quite nifty! But what if I want to emphasize more than one line? – gablin Sep 14 '11 at 10:54
@gablin: Then you should probably rephrase the title of your question ;-) No, seriously: (1) Do you need to just give a (e.g. comma-separated) list of lines (like 1,5,6,7,12) or do you also need ranges (like 1,5-7,12) (2) Do you insist on a font change to highlight the lines or would a different background color (as in my second example) be suitable. – Daniel Sep 14 '11 at 20:11
Hm, yeah that would make it much harder. I actually ended up not emphasizing any lines at all, but you did answer my question so I'll accept your answer. – gablin Sep 29 '11 at 19:36

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.