Hyperref 包如何影响常规 \ref 命令以破坏此代码

Hyperref 包如何影响常规 \ref 命令以破坏此代码

我用 LaTeX 写论文,并在后续章节中引用研究问题。我给问题贴上标签,并设计了\nthref(见下面的代码)以通过其标签引用第 n 个研究问题。

我的问题是,如果我的代码包含 hyperref 包(我也喜欢它),我的\nthref代码就会崩溃。似乎使用 hyperref 时,\ref{}仍然会插入一个数字,但事实并非如此。\ifx\ref{#1}=1即使\ref问题变成 1,我也无法再触发语句。我很困惑,因为在渲染的 PDF 中,1 看起来一模一样。难道不应该\ref{q:whatever}总是用同样的东西替换吗?困惑!

我的问题是 hyperref 到底对 -command 做了什么,\ref以破坏下面示例中的代码。删除\usepackage{hyperref}当然是一种解决方案,但我并不喜欢,主要是因为我无法理解为什么我必须这么做。

\documentclass[11pt,twoside]{book} 
\usepackage[utf8]{inputenc}

% Hyperref destroys \nthref below
\usepackage{hyperref}

% let nthref give an ordinal word for the reference to the label of of an enumeric list environment - like research questions
\newcommand\nthref[1]{\ifcase\ref{#1}???\or{first}\or{second}\or{third}\or{fourth}\or{fifth}\else???\fi}

\begin{document}
There are research questions like:
\begin{enumerate}
\label{sec:questions}
  \item What is a sunset?\label{q:sunset}
  \item Can sunsets be modelled?\label{q:model_sunset}
  \item How many sunsets are there in two pineapples?\label{q:pinaple_relation}
\end{enumerate}
% Research questions are labeled as:
%   q:sunset
%   q:model_sunset
%   q:pinaple_relation

In research question \ref{q:sunset} (the \nthref{q:sunset} research question) sunsets are considered. Sunsets remain the themes of the \nthref{q:model_sunset} and \nthref{q:pinaple_relation} questions.

\end{document}

答案1

\hyperref的版本不可\ref扩展,即它不能\ifcase直接在等等测试中使用它来查询它的值。

\hyperref\ref命令是受保护的版本,并且还提供了\ref*以省略超链接。

但是,可以使用来自包(无论如何都包含在内)\getrefnumber的宏来代替。如果未找到引用,则返回。refcounthyperref-1

\documentclass[11pt,twoside]{book} 
\usepackage[utf8]{inputenc}

% Hyperref destroys \nthref below
\usepackage{hyperref}

% let nthref give an ordinal word for the reference to the label of of an enumeric list environment - like research questions
\newcommand\nthref[1]{\ifcase\getrefnumber{#1}???\or{first}\or{second}\or{third}\or{fourth}\or{fifth}\else???\fi}

\begin{document}

There are research questions like:
\begin{enumerate}
\label{sec:questions}
  \item What is a sunset?\label{q:sunset}
  \item Can sunsets be modelled?\label{q:model_sunset}
  \item How many sunsets are there in two pineapples?\label{q:pinaple_relation}
\end{enumerate}
% Research questions are labeled as:
%   q:sunset
%   q:model_sunset
%   q:pinaple_relation

In research question \ref{q:sunset} (the \nthref{q:sunset} research question) sunsets are considered. Sunsets remain the themes of the \nthref{q:model_sunset} and \nthref{q:pinaple_relation} questions.

\end{document}

相关内容