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.

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
add comment

1 Answer 1

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:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{listings}

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

\lstset{basicstyle=\ttfamily}

% The style to be applied for the highlighted line:
\lstdefinestyle{highlight}{basicstyle=\ttfamily\bfseries\color{red}}
%\lstdefinestyle{highlight}{backgroundcolor=\color{orange!30}}

% input listing and emphasize a specific line
% \lstinputemph[listings options]{line number}{filename}
\newcommand{\lstinputemph}[3][\empty]{%                                                    
 \lstset{aboveskip=0pt, belowskip=0pt, showlines=true,#1}%
 \ifnumcomp{#2}{=}{0}{\lstinputlisting{#3}}{%
   \ifnumcomp{#2}{=}{1}{}{\lstinputlisting[lastline=\the\numexpr#2-1\relax]{#3}}%            
   \lstinputlisting[firstnumber={#2},firstline={#2},lastline={#2},style=highlight]{#3}
   \lstinputlisting[firstnumber={\the\numexpr#2+1},firstline=\the\numexpr#2+1\relax]{#3}
 }% else
}

\begin{document}
  \lstinputemph[language=C]{3}{hello.c}
\end{document}

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
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
add comment

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.