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},
commentstyle=\color{dkgreen},
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}
]
/* #include "readTwoPhaseEulerFoamControls.H" */
#include "readTwoPhaseLESEulerFoamControls.H"
// Solve the Momentum equation (*@\label{code:label}@*)
tmp<fvVectorMatrix> UEqn
\end{lstlisting}
\end{document}
Best Answer
@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
by
using
\ifthenelse
from theifthen
package (of course don't forget the corresponding\usepackage{ifthen}
).