3

I want to highlight the line numbers of the lines of the Listing to which I refer. See the question Highlight line numbers in Listings for details about how to highlight one line number.

Now I want to highlight multiple line numbers. The pasted code works, unless you uncomment the inclusion of the hyperref package.

Does anybody know how to get this to work?

BTW: Hyperref conflicts also with the solution of 1.

BTW2: Without the highlighting hyperref causes no problems.

\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
}



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

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

%\usepackage{hyperref}

\begin{document}
\begin{lstlisting}[style=cppStyle,escapeinside={(*@}{@*)},caption={Definition of a dictionary in \texttt{readPPProperties.H}},label=lst:createDictionaryIOobject,     numberstyle={\ifnum\value{lstnumber}=\lineref{code:ioObjectReadFlag}\color{green}\else\ifnum\value{lstnumber}=\lineref{code:ioObjectWriteFlag}\color{green}\else\fi\fi}] 
IOdictionary ppProperties
(
    IOobject
    (
        "ppProperties",
        runTime.constant(),
        mesh,
        IOobject::MUST_READ_IF_MODIFIED, (*@\label{code:ioObjectReadFlag}@*)
        IOobject::NO_WRITE (*@\label{code:ioObjectWriteFlag}@*)
    )
);
\end{lstlisting}
\end{document}
0

1 Answer 1

4

The key is to use \getrefnumber from refcount instead of your \lineref, because \ref doesn't expand to a number, but is much more complicated when hyperref is involved, as it wants to create links.

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


% 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
}

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


\begin{document}
\begin{lstlisting}[
  style=cppStyle,
  escapeinside={(*@}{@*)},
  caption={Definition of a dictionary in \texttt{readPPProperties.H}},
  label=lst:createDictionaryIOobject,
  numberstyle={%
    \ifnum\value{lstnumber}=\getrefnumber{code:ioObjectReadFlag}%
      \color{green}%
    \else
      \ifnum\value{lstnumber}=\getrefnumber{code:ioObjectWriteFlag}%
        \color{green}
      \fi
    \fi
  }
] 
IOdictionary ppProperties
(
    IOobject
    (
        "ppProperties",
        runTime.constant(),
        mesh,
        IOobject::MUST_READ_IF_MODIFIED, (*@\label{code:ioObjectReadFlag}@*)
        IOobject::NO_WRITE (*@\label{code:ioObjectWriteFlag}@*)
    )
);
\end{lstlisting}

This is a reference: \ref{code:ioObjectReadFlag}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .