# highlight line numbers in listings

I would like to highlight the line numbers of the lines I refer to with lstlistings. I use escape characters to define labels in my Listing to which I refer later in my document (e.g. see Line X of Listing Y).

How can I make the line number of line X of Listing Y colorful?

The image below shows how it should look like. This was created by comparing the line number to a certain value. I would like to compare the line number to the value of a reference.

Here is a MWE in which I compare the line number against a hardcoded 5. My question is now, how to highlight the line number of the line in which I defined the label code:label?

\documentclass{article}
\usepackage{xcolor}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\usepackage{listings}

% define C++ style
\lstdefinestyle{cppStyle}
{
basicstyle=\footnotesize,
tabsize=2,
captionpos=b,
frame=lines,
breaklines=true,
% language related
language=C++,
keywordstyle=\color{blue},
stringstyle=\color{mauve},
% numbering
numbers=left,
numberstyle=\tiny
}

\begin{document}
\begin{lstlisting}[
style=cppStyle,
escapeinside={(*@}{@*)},
caption={Manual Definition -- \emph{Oh, what a mess!}},
label=lst:notNumbered,
numberstyle={\ifnum\value{lstnumber}=5\color{green}\fi}
]

// Solve the Momentum equation (*@\label{code:label}@*)

tmp<fvVectorMatrix> UEqn
\end{lstlisting}
\end{document}

-
Can you please unaccept my answer and accept phfaist's instead? I unwittingly overcomplicated the matter by reimplementing what was already provided by the listings package! –  John Wickerson May 28 '14 at 18:18
@John thanks nonetheless for your well done answer. –  Dohn Joe May 28 '14 at 18:58
This question explaines how to highlight more than one line [tex.stackexchange.com/questions/191771/…. –  Dohn Joe Jul 16 '14 at 16:52
Highlighting line numbers introduces a package conflict with hyperref. –  Dohn Joe Jul 16 '14 at 16:53
Conflict solved: [tex.stackexchange.com/questions/191771/… –  Dohn Joe Jul 16 '14 at 18:01

@John's answer should work well and is very well explained; for completeness here's an alternative solution.

For me, it works to simply replace the line

numberstyle={\ifnum\value{lstnumber}=5\color{green}\fi}


by

numberstyle={\ifthenelse{\equal{\ref{code:label}}{\value{lstnumber}}}{\color{green}}{}}


using \ifthenelse from the ifthen package (of course don't forget the corresponding \usepackage{ifthen}).

-
Pah! You're absolutely right :-). I hadn't realised that \label works properly inside listings; that is, I hadn't realised that \label would grab the current line number. –  John Wickerson May 28 '14 at 18:16
What if I wanted to place several labels (e.g. 2 labels) within the Listing and highlight all the lines in which a label was placed? I seem to be unable to come up with a working nested condition. –  Dohn Joe Jul 16 '14 at 14:49
@DohnJoe Sorry I didn't have time to answer, but I see now others have already done so and have provided a better solution :) –  phfaist Jul 22 '14 at 9:44

This is a bit tricky because the line number to be highlighted is not defined until later on in the file. However, you can use an idea previously expounded by egreg (http://tex.stackexchange.com/a/49032/25356) and piggyback on LaTeX's existing labelling mechanism, which writes information to the .aux file in order to be persistent between compilation runs.

## Explanation

The following \labelline command associates the given label with the number of the current line (which is given by \thelstnumber).

\newcommand\labelline[1]{%
\def\@currentlabel{\thelstnumber}\label{#1}}


The following \lineref command extracts the value from the given label, returning 0 if the label is not yet defined. (The label foo is internally stored as the command \r@foo, hence the check of whether r@#1 is defined.)

\newcommand\lineref[1]{
\@ifundefined{r@#1}{0}{\ref{#1}}
}


Note that you'll need two passes of the compiler in order to resolve the labels. Happily, having used the existing labelling infrastructure, the compiler will give you the standard hint about "run LaTeX again to get references right".

## Complete code

\documentclass{article}
\usepackage{xcolor}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\usepackage{listings}

% define C++ style
\lstdefinestyle{cppStyle}
{
basicstyle=\footnotesize,
tabsize=2,
captionpos=b,
frame=lines,
breaklines=true,
% language related
language=C++,
keywordstyle=\color{blue},
stringstyle=\color{mauve},
% numbering
numbers=left,
numberstyle=\tiny
}

\makeatletter
\newcommand\labelline[1]{%
\def\@currentlabel{\thelstnumber}\label{#1}}
\newcommand\lineref[1]{
\@ifundefined{r@#1}{0}{\ref{#1}}
}
\makeatother

\begin{document}

\begin{lstlisting}[
style=cppStyle,
escapeinside={(*@}{@*)},
caption={Manual Definition -- \emph{Oh, what a mess!}},
label=lst:notNumbered,
numberstyle={\ifnum\value{lstnumber}=\lineref{code:label}\color{green}\fi}
]