![突出显示列表中的行号,与 hyperref 包冲突](https://linux22.com/image/294482/%E7%AA%81%E5%87%BA%E6%98%BE%E7%A4%BA%E5%88%97%E8%A1%A8%E4%B8%AD%E7%9A%84%E8%A1%8C%E5%8F%B7%EF%BC%8C%E4%B8%8E%20hyperref%20%E5%8C%85%E5%86%B2%E7%AA%81.png)
我想突出显示我引用的清单中的行号。请参阅问题突出显示列表中的行号有关如何突出显示一个行号的详细信息。
现在我想突出显示多个行号。粘贴的代码有效,除非您取消注释该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}