将 ref/pageref 目标标签放置在部分内

将 ref/pageref 目标标签放置在部分内

是否有可能以某种方式放置一个“虚拟标签”,其行为在与一起使用时就像最近的“结构标签”一样,\ref但在与一起使用时仍会产生标签实际放置的页码\pageref

请考虑以下示例:

\documentclass{article}
\usepackage{lipsum}

\begin{document}

\section{First Section}
\label{first}

This is my first section. Let me add enough text to fill up more than one page here.
\lipsum[1-6]

Fine, so we've written quite a lot of text. Now let's talk about \textbf{some important topic} --
\lipsum[7-10]

\section{Second Section}

This is where things get interesting. In this section, I would like to discuss something
entirely different, which is nonetheless related to \textbf{some important topic} that I 
introduced earlier in section \ref{first} on page \pageref{first}.

\end{document}

由于某种原因,第一部分跨越了多页,也涵盖了多个主题。稍后,我想添加对该主题的引用,以防读者想再次查阅。生成的引用\ref完全正确——第一部分确实涵盖了重要主题。但是,页面引用不正确——它指向该部分出现的第一页,即第 1 页。在这种情况下,重要主题仅在第 2 页提及,因此读者必须仔细阅读该部分中不相关的部分。

我想改进这种行为,让页码指向讨论实际主题的位置。显而易见的解决方案可能是将文本分成不同的\subsection部分,但在实际文档中,这不是一个选择:文档已经很大,结构非常完善,在简介中我有很多定义,我希望能够引用,但每个定义只跨越一两段。为每个定义添加一个子部分会使简介很难阅读。

我还希望能够为\refand\pageref命令保留相同的标签,因为我使用了fancyref包。最后,可引用的(这是一个词吗?)\labels可能出现在结构内的不同深度——有时我需要引用整个\part,有时只需要引用一个\subsubsection

答案1

对重要事实的页面引用使用另一个标签:

\documentclass{article}
\usepackage{lipsum}    
\makeatletter
\def\magicref#1{\expandafter\magicref@i#1\@nil}
\def\magicref@i#1::#2\@nil{section \ref{#1} on page \pageref{#1::#2}}
\makeatother
\begin{document}

\section{First Section}
\label{first}

This is my first section. Let me add enough text to fill up more than one page here.
\lipsum[1-6]

Fine, so we've written quite a lot of text. Now let's talk about 
\textbf{some important topic}\label{first::important} --  %%%%%%%%%%%%%%%
\lipsum[7-10]

\section{Second Section}

This is where things get interesting. In this section, I would like to discuss something
entirely different, which is nonetheless related to \textbf{some important topic} that I 
introduced earlier (see \magicref{first::important}).

\end{document}

\ref{first}\ref{first:important}总相同。但\pageref可以不同。\magricref分成\ref\pageref

答案2

我来晚了一点(好吧,我遇到了这个问题,上面的方法对我的特定文档设置不起作用),但由于页面引用去了(我的个人论文有内部超链接)部分,我想,在阅读了原始海报提到的“虚拟标签”之后,我为什么不\phantomsection从尝试一下hyperref package,因为我无论如何都在使用那个包。

也就是说,我使用了 并将hyperref package\phantomsection”放在标签的左侧\label{important},两者都位于重要议题

此外,我设置了参数colorlinks=trueallcolors=black以便输出文档看起来像原始海报所需的输出。当然,唯一的区别是单击页码会直接带您到重要议题

\documentclass{article}
\usepackage{lipsum}

\usepackage{hyperref}
\hypersetup{
  colorlinks=true,
  allcolors=black,
  pdfencoding=auto,
  psdextra
}

\begin{document}

\section{First Section}
\label{first}

This is my first section. Let me add enough text to fill up more than one page here.
\lipsum[1-6]

Fine, so we've written quite a lot of text. Now let's talk about \phantomsection\label{important}\textbf{some important topic} --
\lipsum[7-10]

\section{Second Section}

This is where things get interesting. In this section, I would like to discuss something
entirely different, which is nonetheless related to \textbf{some important topic} that I 
introduced earlier in section \ref{first} on page \pageref{important}.
\end{document}

答案3

为什么你不能简单地在重要主题后使用标签?

IE

\documentclass{article}
\usepackage{lipsum}

\begin{document}

\section{First Section}
\label{first}

This is my first section. Let me add enough text to fill up more than one page here.
\lipsum[1-6]

Fine, so we've written quite a lot of text. Now let's talk about
\textbf{some important topic}\label{important} --
\lipsum[7-10]

\section{Second Section}

This is where things get interesting. In this section, I would like to discuss something 
entirely different, which is nonetheless related to \textbf{some important topic} that 
I introduced earlier in section \ref{important} on page \pageref{important}.

\end{document}

相关内容