在章节标题中隐藏 cleveref 链接

在章节标题中隐藏 cleveref 链接

我希望链接总体上有颜色,但不希望章节标题中的链接有颜色。

梅威瑟:

\documentclass{article}

\usepackage[colorlinks]{hyperref}
\usepackage[nameinlink]{cleveref}

\begin{document}

% The section to reference
\section{Section Title}\label{sec:A}

% Colour links in normal text, like this one
Reference to \cref{sec:A}.

% This colours the link even in the section title, which I don't want
\section{Reference to \Cref{sec:A}}

% This hides the link in normal text, so...
Reference to {\hypersetup{hidelinks}\cref{sec:A}}.

% ... this should do what I want, but doesn't work.
% \section{Reference to {\hypersetup{hidelinks}\Cref{sec:A}}}

\end{document}

答案1

章节标题中的命令的问题在于它们必须要么可扩展,要么健壮。两者\Cref皆非\hypersetup

由于\hypersetup仅在排版文本时才会更改设置,因此它不需要可扩展(也不可能做到)。因此,您可以使用以下方法使其变得健壮:

\let\ORGhypersetup\hypersetup
\protected\def\hypersetup{\ORGhypersetup}

现在\Cref不同了。可以变得健壮,但在 PDF 书签中你会看到Reference to sec:A它不是那么好。但是,您可以通过加载包并执行以下操作,使其在hyperref设置 PDF 书签时可扩展:crossreftools

\pdfstringdefDisableCommands{%
  \def\hypersetup#1{}%
  \let\Cref\crtCref
  \let\cref\crtcref
}

(我也禁用了\hypersetup否则你会得到Reference to hidelinksSection 1:)。

工作代码:

\documentclass{article}

\usepackage[colorlinks]{hyperref}
\usepackage[nameinlink]{cleveref}
\usepackage{crossreftools}

\let\ORGhypersetup\hypersetup
\protected\def\hypersetup{\ORGhypersetup}
\pdfstringdefDisableCommands{%
  \def\hypersetup#1{}%
  \let\Cref\crtCref
  \let\cref\crtcref
}

\begin{document}

% The section to reference
\section{Section Title}\label{sec:A}

% Colour links in normal text, like this one
Reference to \cref{sec:A}.

% This colours the link even in the section title, which I don't want
\section{Reference to \Cref{sec:A}}

% This hides the link in normal text, so...
Reference to {\hypersetup{hidelinks}\cref{sec:A}}.

% ... this should do what I want, but doesn't work.
\section{Reference to {\hypersetup{hidelinks}\Cref{sec:A}}}

\end{document}

输出:

在此处输入图片描述

相关内容