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?


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

  • 1
    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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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