这是\g@addto@macro 附加位置特定引用。在这种情况下,我使用 来应用自动表格填充\immediate\write\@auxout
。原因是我想将整个表格定位在文档的开头。但正如您所见,引用一直在更新,而我不知道如何使用\expand
或\noexpand
控制这种行为。下面的最小示例是不言自明的,也展示了我想要实现的目标。表格外的引用表现如预期。但是,表格内的引用却没有(我认为问题出在命令 上\addtotable
)。我的问题是,如何更改定义的命令,以便表格也能正确更新?
\documentclass{article}
\usepackage{nameref}
\usepackage{xcolor}
\usepackage{hyperref}
\newcommand\foorows{}
\newcommand\foo{%
\begin{table}[htp]
\centering
\begin{tabular}{ |c| }
\hline
Section \\ \hline % Header row
\foorows
\hline
\end{tabular}
\end{table}}
\makeatletter
\newcommand\addtotable[1]{%
\immediate\write\@auxout{\unexpanded{\g@addto@macro\foorows{#1 \\}}}%
}%
\makeatother
%
\makeatletter
\newcommand{\currentname}{\@currentlabelname}
\makeatother
\makeatletter
\newcommand{\refthis}{%
\hyperlink\@currentHref\@currentlabel
}
\newcommand\refthistype[1]{%
\begingroup
\Hy@localanchornametrue
\expandafter\hyper@makecurrent\noexpand{#1}%
\hyperlink\@currentHref{\csname the#1\endcsname}%
\endgroup
}
\makeatother
\newcommand{\example}[1]{
~\\
\textbf{Example}\\
This is important
Environment example correctly referrs to
Section~\refthistype{section}
and
Subsection~\refthis.
\begin{equation}
\label{#1}
1+1=2.
\end{equation}
%
This
Environment now
referes to Subsection~\refthis\, instead of Equation~\ref{#1}.
But this is not so bad since we can make a work around.
%
\addtotable{Equation~\ref{#1}}
\addtotable{refthis~\refthis}
\addtotable{refthistype\{section\}~\refthistype{section}}
~\\
}
\begin{document}
\section{Section~1}
\label{Section 1}
Main table:
\foo
\subsection{Subsection~1.1}
\label{Subsection 1.1}
\subsection{Subsection~1.2}
\label{Subsection 1.2}
The references correctly refer to Section~\refthis,
\currentname, Section~\refthistype{section},
and Subsection~\refthistype{subsection}.
\example{Eq1}
Partial table:
\addtotable{\refthis}
\foo
\refthis
\section{Section~2}
\section{Section~3}
\label{Section 3}
\subsection{Subsection~3.1}
\label{Subsection 3.1}
The references correctly refer to Section~\refthis,
\currentname, Section~\refthistype{section},
and Subsection~\refthistype{subsection}.
\example{Eq2}
Full table:
\addtotable{\refthis}
\foo
\textcolor{red}{Some of the references in the table changed.}
\\~\\
Expected table output:
\begin{table}[htp]
\centering
\begin{tabular}{ |c| }
\hline
Section \\\hline % Header row
Equation~\ref{Eq1}\\\hline
Subsection~\ref{Subsection 1.2}\\\hline
refthistype\{section\}\ref{Section 1}\\\hline
Subsection~\ref{Subsection 1.2}\\\hline
Equation~\ref{Eq2}\\\hline
Subsection~\ref{Subsection 3.1}\\\hline
refthistype\{section\}\ref{Section 3}\\\hline
Subsection~\ref{Subsection 3.1}\\\hline
\end{tabular}
\end{table}
\end{document}
答案1
您\immediate\write\@auxout{\unavoided{...}}
将停止所有扩展,但您需要扩展一些命令。相反,您可以使用\protected@write\@auxout{}{...}
通过 LaTeX 机制控制扩展\protect
。
此外,还\refthis
必须进行更改以允许扩展,您大多必须在此处添加括号:
\newcommand{\refthis}{%
\protect\hyperlink{\@currentHref}{\@currentlabel}
}
修复起来比较困难\refthistype
。如果不更改大量 hyperref 内部结构,当前版本就无法实现扩展,但你可以通过一些小技巧来避免这种情况:
\DeclareRobustCommand\@currentlink[1]{\hyperlink\@currentHref{\csname the#1\endcsname}}
\newcommand\refthistype[1]{%
\begingroup
\protect\Hy@localanchornametrue
\protect\@nameuse{c@#1}=\the\value{#1}
\protect\hyper@makecurrent{#1}%
\@currentlink{#1}%
\endgroup
}
结合起来,给出了一个有效的例子:
\documentclass{article}
\usepackage{nameref}
\usepackage{xcolor}
\usepackage{hyperref}
\newcommand\foorows{}
\newcommand\foo{%
\begin{table}[htp]
\centering
\begin{tabular}{ |c| }
\hline
Section \\ \hline % Header row
\foorows
\hline
\end{tabular}
\end{table}}
\makeatletter
\DeclareRobustCommand\realaddtotable[1]{\g@addto@macro\foorows{#1 \\}}
\newcommand\addtotable[1]{%
\protected@write\@auxout{}{\realaddtotable{#1}}%
}%
\makeatother
%
\makeatletter
\newcommand{\currentname}{\@currentlabelname}
\makeatother
\makeatletter
\newcommand{\refthis}{%
\protect\hyperlink{\@currentHref}{\@currentlabel}
}
\DeclareRobustCommand\@currentlink[1]{\hyperlink\@currentHref{\csname the#1\endcsname}}
\newcommand\refthistype[1]{%
\begingroup
\protect\Hy@localanchornametrue
\protect\@nameuse{c@#1}=\the\value{#1}
\protect\hyper@makecurrent{#1}%
\@currentlink{#1}%
\endgroup
}
\makeatother
\newcommand{\example}[1]{
~\\
\textbf{Example}\\
This is important
Environment example correctly referrs to
Section~\refthistype{section}
and
Subsection~\refthis.
\begin{equation}
\label{#1}
1+1=2.
\end{equation}
%
This
Environment now
referes to Subsection~\refthis\, instead of Equation~\ref{#1}.
But this is not so bad since we can make a work around.
%
\addtotable{Equation~\ref{#1}}
\addtotable{refthis~\refthis}
\addtotable{refthistype\{section\}~\refthistype{section}}
~\\
}
\begin{document}
\section{Section~1}
\label{Section 1}
Main table:
\foo
\subsection{Subsection~1.1}
\label{Subsection 1.1}
\subsection{Subsection~1.2}
\label{Subsection 1.2}
The references correctly refer to Section~\refthis,
\currentname, Section~\refthistype{section},
and Subsection~\refthistype{subsection}.
\example{Eq1}
Partial table:
\addtotable{\refthis}
\foo
\refthis
\section{Section~2}
\section{Section~3}
\label{Section 3}
\subsection{Subsection~3.1}
\label{Subsection 3.1}
The references correctly refer to Section~\refthis,
\currentname, Section~\refthistype{section},
and Subsection~\refthistype{subsection}.
\example{Eq2}
Full table:
\addtotable{\refthis}
\foo
\textcolor{red}{Some of the references in the table changed.}
\\~\\
Expected table output:
\begin{table}[htp]
\centering
\begin{tabular}{ |c| }
\hline
Section \\\hline % Header row
Equation~\ref{Eq1}\\\hline
Subsection~\ref{Subsection 1.2}\\\hline
refthistype\{section\}\ref{Section 1}\\\hline
Subsection~\ref{Subsection 1.2}\\\hline
Equation~\ref{Eq2}\\\hline
Subsection~\ref{Subsection 3.1}\\\hline
refthistype\{section\}\ref{Section 3}\\\hline
Subsection~\ref{Subsection 3.1}\\\hline
\end{tabular}
\end{table}
\end{document}