突出显示列表中的行号,与 hyperref 包冲突

突出显示列表中的行号,与 hyperref 包冲突

我想突出显示我引用的清单中的行号。请参阅问题突出显示列表中的行号有关如何突出显示一个行号的详细信息。

现在我想突出显示多个行号。粘贴的代码有效,除非您取消注释该hyperref包的包含。

有人知道如何让它工作吗?

顺便说一句:Hyperref 也与以下解决方案相冲突1

BTW2:没有突出显示hyperref不会造成任何问题。

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

答案1

关键是使用\getrefnumber来自refcount而不是你的\lineref,因为\ref不会扩展为数字,但是当hyperref涉及到时会变得更加复杂,因为它想要创建链接。

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

在此处输入图片描述

相关内容