目前,在我的论文中,我使用全局计数器对定理(和注释等)进行编号。如果参考文献距离标签太远,那么在印刷版中很难找到定理。有没有简单的方法可以修改我的参考命令以添加“在第 XXX 页”,以防参考文献和标签之间有 3 页的差距?你有什么建议或经验吗?
据我了解的文档,诸如 varioref 之类的软件包仅支持一页差异条件;但即使这样我也无法激活。
最小工作示例:
\documentclass{scrreprt}
\usepackage{blindtext} % lore ipsum...
\usepackage{amsthm}
\newtheorem{thm}{Theorem}
\begin{document}
% example theorem:
\begin{thm}\label{thm:foo}
The future belongs to those who believe in their dreams.
\end{thm}
% generate more than 3 pages of text:
\blindtext[20]
% reference:
Reference to theorem \ref{thm:foo}. Here an automatic
``on page \pageref{thm:foo}'' would be nice.
\end{document}
Mico 的 MWE(参见下文讨论):
\documentclass{scrreprt}
\usepackage{blindtext} % lore ipsum...
\usepackage{amsmath}
\usepackage{ntheorem}
\usepackage{varioref}
\usepackage{hyperref}
\newtheorem{thm}{Theorem}
\begin{document}
% theorem foo
\begin{thm}\label{thm:foo}
The future belongs to those who believe in their dreams, see (\vref{eqn:bar})
\end{thm}
% some pages of text...
\blindtext[15]
% cross-reference to theorem within equation which is labeled itself (same works for figures)
\begin{align}\label{eqn:bar}
a+b=c\quad\text{(cf. Theorem \vref{thm:foo})}
\end{align}
\end{document}
导致
! Package amsmath Error: Multiple \label's: label 'eqn:bar' will be lost.
! Package amsmath Error: Multiple \label's: label '4@xvr' will be lost.
- 进一步的警告...
(部分)解决方案(基于基督徒的想法)
在尝试了 Christians 的建议后,我想到了一个解决方案(它似乎有效)。我必须在加载之前存储操纵的\ref
命令。然后,我可以将其用作标准引用和可选的“页面”版本。出于某种奇怪的原因,这个\vref
hyperref
\ref
\vref
仅与 babel 配合使用。为了避免错误的间距,添加“%”非常重要。
\documentclass{scrreprt}
\usepackage{blindtext} % lore ipsum...
\usepackage{amsmath}
\usepackage[english]{babel} % < for some strange reason HAS to be loaded
%% --- ref manipulations as suggested by Christian Hupfer:
\makeatletter
\let\latex@@ref\ref
\newcounter{pagegaptreshhold}
\setcounter{pagegaptreshhold}{2}
\newcounter{tmpcntr}
\renewcommand{\ref}[1]{%
\setcounter{tmpcntr}{\value{page}}%
\addtocounter{tmpcntr}{-\getpagerefnumber{#1}}%
\ifnum\value{tmpcntr} < 0 %
\setcounter{tmpcntr}{\numexpr -1*\value{tmpcntr}}%
\fi%
\ifnum\value{tmpcntr} > \value{pagegaptreshhold}
\latex@@ref{#1} on page \pageref{#1}%
\else%
\latex@@ref{#1}%
\fi%
}
\makeatother
%% --- end cmhughes suggestion ---
\let\vref\ref
\usepackage{hyperref}
\newtheorem{thm}{Theorem}
\begin{document}
% theorem foo
\begin{thm}\label{thm:foo}
The future belongs to those who believe in their dreams, see (\vref{eqn:bar})
\end{thm}
% some pages of text...
\blindtext[30]
% cross-reference to theorem within equation which is labeled itself (same works for figures)
\begin{align}\label{eqn:bar}
a+b=c\quad\text{(cf. Theorem \vref{thm:foo})}
\end{align}
In particular, we have now the choice between
\begin{itemize}
\item ``on page''-if-too-far-away-style: Theorem \vref{thm:foo}
\item classic style: Theorem \ref{thm:foo}
\end{itemize}
\end{document}
答案1
\documentclass{article}
\usepackage{refcount}
\usepackage{blindtext}
\makeatletter
\let\latex@@ref\ref
\newcounter{pagegaptreshhold}
\setcounter{pagegaptreshhold}{2}
\newcounter{tmpcntr}
\renewcommand{\ref}[1]{%
\setcounter{tmpcntr}{\value{page}}%
\addtocounter{tmpcntr}{-\getpagerefnumber{#1}}%
\ifnum\value{tmpcntr} < 0 %
\setcounter{tmpcntr}{\numexpr -1*\value{tmpcntr}}%
\fi%
\ifnum\value{tmpcntr} > \value{pagegaptreshhold}
\latex@@ref{#1} on page \pageref{#1}%
\else%
\latex@@ref{#1}%
\fi%
}
\makeatother
\begin{document}
\section{first}
In\ref{section::second} we will see that...
\blindtext[40]
\section{second} \label{section::second}
Here: \ref{section::second}
\end{document}
答案2
(在 OP 发布第二个更详细的 MWE 后更新答案。)
该varioref
包在生成页面调用方面实现了以下决策规则:
如果被交叉引用的项目与交叉引用位于同一页面,则不会产生与页面相关的词缀;
如果交叉引用的项目和交叉引用位于相邻页面上,则在交叉引用上附加“在上一页”或“在下一页”。
如果交叉引用的项目和交叉引用本身相隔两页或多页,则会生成“在第 x 页”的词缀。
如果您提前知道交叉引用和被交叉引用的项目之间会隔着几页,您也可以使用指令\fullref
,它比更简单(使用更少的机器周期...)\vref
。
如果您同时使用varioref
和hyperref
,则还应加载该cleveref
包:这样做可修复前两个包之间的(轻微)冲突。如果您加载cleveref
,则项目的名称(“方程”、“定理”等)将自动作为交叉引用调用的前缀。
\documentclass{scrreprt}
\usepackage{blindtext} % lorem ipsum...
\usepackage{amsmath}
\usepackage[amsmath]{ntheorem}
\usepackage{varioref} % for \vref and \fullref macros
\usepackage[colorlinks]{hyperref}
\usepackage[nameinlink]{cleveref}
\newtheorem{thm}{Theorem}
\begin{document}
\setcounter{chapter}{1} % just for this example
% theorem foo, with cross-reference to an equation
\begin{thm}\label{thm:foo}
The future belongs to those who believe in their dreams (see \vref{eqn:bar}).
\end{thm}
% some pages of text...
\blindtext[15]
% equation bar, with cross-reference to a theorem
\begin{align}\label{eqn:bar}
a+b=c\quad\text{(cf.\ \fullref{thm:foo})}
\end{align}
\end{document}