有没有办法让钩子在代码之前工作?
\documentclass{article}
\usepackage{etoolbox}
\begin{document}
\hhh % this does not work
foo
\appto\hhh{test1}
bar
\hhh % this work
\end{document}
答案1
您可以将值存储在aux
文件中并从那里调用它们。
\documentclass{article}
% \setaux{var}{val} sets variable var to val
\newcommand\setaux[2]{\expandafter\xdef\csname aux#1\endcsname{#2}}
% \useaux{var} expands to the value of var
\newcommand\useaux[1]{\ifcsname aux#1\endcsname\csname aux#1\endcsname\else ???\fi}
\makeatletter
% \saveaux{var}{val} sets variable var to val and saves it also to the aux file
% such that the value is available anywhere in the document (after the next run).
\newcommand\saveaux[2]{%
\immediate\write\@auxout{\string\setaux{#1}{#2}}%
\setaux{#1}{#2}%
}
\makeatother
\begin{document}
\useaux{hhh}
foo
\saveaux{hhh}{test1}
bar
\useaux{hhh}
\end{document}
第一次运行后,你会得到
???
foo
bar
test1
第二次运行后,你会得到
test1
foo
bar
test1