将解决方案扩展为我之前的问题我想告诉 LaTeX 运行多次,直到定义变量。在演示示例中,我希望 A 和 C 部分的输出相同。有什么想法吗?
\documentclass{article}
\usepackage{hyperref}
\usepackage{varioref}
\newcounter{mycounter}
\makeatletter
\newcommand{\defineAndPrintXX}[3]{%
\refstepcounter{mycounter}%
\begingroup\edef\x{\endgroup\noexpand\@namedef{#1@id}{FSR \themycounter}}\x
\@namedef{#1@definition}{#2}
\hypertarget{#1}{%
\subsection{\csname#1@id\endcsname}\label{#1}}
% use the definition, i.e. #2
\csname#1@definition\endcsname
#3
}
\newcommand{\referToXX}[1]{
\protect\hyperlink{#1}{\csname#1@id\endcsname} (see section \vref{#1}) \newline
\csname#1@definition\endcsname
}
\begin{document}
\section{A}
Test before definition: \referToXX{test-1}
\section{B}
\defineAndPrintXX{test-1}{Definition 1}{Bla bla}
\defineAndPrintXX{test-2}{Definition 2}{Bla bla}
\section{C}
Test after definition: \referToXX{test-1}
\end{document}
答案1
使用包参考值您可以指定自己的属性(不仅仅是页码、通过增加的计数器值\refstepcounter
等),这些属性的值将在放置(zref-)标签进行交叉引用时被记录。
只要您的每个 XX-thingies 都创建另一个小节并放置用于交叉引用的标签,您就不需要指定\hypertarget
/,\hyperlink
因为 hyperref-package 会自动为每个小节创建一个目标/目的地(在放置交叉引用标签的情况下),可以通过 创建超链接进行访问。\hyperref[{⟨cross-referencing-label⟩}]{⟨text of the hyperlink⟩}
\documentclass{article}
\usepackage{zref}
\usepackage{varioref}
\usepackage{hyperref}
\newcounter{XXcounter}
\makeatletter
\zref@newprop{XXid}{}%
\zref@newprop{XXdefinition}{}%
\newcommand{\defineAndPrintXX}[3]{%
\refstepcounter{XXcounter}%
\zref@setcurrent{XXid}{FSR\protect~\theXXcounter}%
\zref@setcurrent{XXdefinition}{#2}%
\par
\subsection{FSR~\theXXcounter}%
\@bsphack
\begingroup
\let\@bsphack\relax
\let\@esphack\relax
\label{#1}%
\zref@labelbyprops{#1}{XXid,XXdefinition}%
\endgroup
\@esphack
#2\par#3%
}%
\newcommand{\referToXX}[1]{%
\zifrefundefined{#1}{\nfss@text{\reset@font\bfseries??}}{%
\hyperref[{#1}]{%
\zref@extractdefault{#1}{XXid}{}%
} (see section \vref{#1})\newline
\zref@extractdefault{#1}{XXdefinition}{}%
}%
}%
\makeatother
\begin{document}
\section{Tests before definitions}
\noindent\verb|\referToXX{test-1}|:\\
\referToXX{test-1}
\noindent\verb|\referToXX{test-2}|:\\
\referToXX{test-2}
\section{Place of definitions}
\defineAndPrintXX{test-1}{Definition 1}{Bla bla}
\defineAndPrintXX{test-2}{Definition 2}{Bla bla}
\section{Tests after definitions}
\noindent\verb|\referToXX{test-1}|:\\
\referToXX{test-1}
\noindent\verb|\referToXX{test-2}|:\\
\referToXX{test-2}
\end{document}
如果(无论出于何种原因)您坚持要有另一个目标/目的地,其名称与交叉引用标签的名称/的第一个参数相对应\defineAndPrintXX
,那么您也可以通过 zref 属性来处理这个问题:
\documentclass{article}
\usepackage{zref}
\usepackage{hyperref}
\usepackage{varioref}
\newcounter{XXcounter}
\makeatletter
\zref@newprop{XXid}{}%
\zref@newprop{XXdefinition}{}%
\zref@newprop{XXdestination}{}%
\newcommand{\defineAndPrintXX}[3]{%
\refstepcounter{XXcounter}%
\zref@setcurrent{XXid}{FSR\protect~\theXXcounter}%
\zref@setcurrent{XXdefinition}{#2}%
\zref@setcurrent{XXdestination}{#1}%
\par
\hypertarget{#1}{\subsection{FSR~\theXXcounter}}%
\@bsphack
\begingroup
\let\@bsphack\relax
\let\@esphack\relax
\label{#1}%
\zref@labelbyprops{#1}{XXid,XXdefinition,XXdestination}%
\endgroup
\@esphack
#2\par#3%
}%
\newcommand{\referToXX}[1]{%
\zifrefundefined{#1}{\nfss@text{\reset@font\bfseries??}}{%
\hyperlink{\zref@extractdefault{#1}{XXdestination}{}}{%
\zref@extractdefault{#1}{XXid}{}%
} (see section \vref{#1})\newline
\zref@extractdefault{#1}{XXdefinition}{}%
}%
}%
\makeatother
\begin{document}
\section{Tests before definitions}
\noindent\verb|\referToXX{test-1}|:\\
\referToXX{test-1}
\noindent\verb|\referToXX{test-2}|:\\
\referToXX{test-2}
\section{Place of definitions}
\defineAndPrintXX{test-1}{Definition 1}{Bla bla}
\defineAndPrintXX{test-2}{Definition 2}{Bla bla}
\section{Tests after definitions}
\noindent\verb|\referToXX{test-1}|:\\
\referToXX{test-1}
\noindent\verb|\referToXX{test-2}|:\\
\referToXX{test-2}
\end{document}