考虑以下 MWE:
\documentclass{article}
\newlength{\templen}
\makeatletter
\AtEndDocument{%
\immediate\write\@auxout{\noexpand\setlength{\noexpand\templen}{150pt}}}
\makeatother
\begin{document}
\typeout{Document start}
\verb!1:! \the\templen \par
\setlength{\templen}{100pt}
\verb!2:! \the\templen
\end{document}
编译两次得到输出
1: 0.0pt
2: 100.0pt
即使.aux
包含
\relax
\setlength {\templen }{150pt}
根据.log
,.aux
在文档开始之前读取,如下所示\typeout
:
(./latex_stuff.aux)% <------------- This is where the .aux is read
\openout1 = `latex_stuff.aux'.
%...
Document start % <------------- This is the document start
那么,为什么输出结果不像
1: 150.0pt
2: 100.0pt
答案1
该.aux
文件在组内读取,并且\setlength
是本地命令。方便的是,您可以使用前缀\setlength
(不确定是否有记录):
\documentclass{article}
\newlength{\templen}
\makeatletter
\AtEndDocument{%
\immediate\write\@auxout
{\noexpand\global\noexpand\setlength{\noexpand\templen}{150pt}}}
\makeatother
\begin{document}
\typeout{Document start}
\verb!1:! \the\templen \par
\setlength{\templen}{100pt}
\verb!2:! \the\templen
\end{document}
以下是加载文件的\document
(调用时出现 问题)定义的摘录。它清楚地显示了 读取的本地范围(来自\begin{document}
.aux
.aux
latex.ltx
):
\def\document{\endgroup
\ifx\@unusedoptionlist\@empty\else
\@latex@warning@no@line{Unused global option(s):^^J%
\@spaces[\@unusedoptionlist]}%
\fi
\@colht\textheight
\@colroom\textheight \vsize\textheight
\columnwidth\textwidth
\@clubpenalty\clubpenalty
\if@twocolumn
\advance\columnwidth -\columnsep
\divide\columnwidth\tw@ \hsize\columnwidth \@firstcolumntrue
\fi
\hsize\columnwidth \linewidth\hsize
\begingroup\@floatplacement\@dblfloatplacement% <-- Group start
\makeatletter\let\@writefile\@gobbletwo
\global \let \@multiplelabels \relax
\@input{\jobname.aux}% <-- .aux read in
\endgroup% <-- Group end
% ...
}