这是一个后续问题是否可以声明脚注编号右对齐但使用动态宽度?。
我尝试在 LaTeX 而不是 TeX 中实现 @wipet 针对原始问题给出的解决方案。据我所知,我理解了原理,但无法让它工作:似乎信息aux
正确地写入了文件,因为
\@namedef{page@of@footnote@0}{1} \@namedef{max@footnote@on@page@1}{0}
\@namedef{page@of@footnote@1}{1} \@namedef{max@footnote@on@page@1}{1}
\@namedef{page@of@footnote@2}{2} \@namedef{max@footnote@on@page@2}{2}
\@namedef{page@of@footnote@100}{3} \@namedef{max@footnote@on@page@3}{100}
但我无法访问它
\csuse{max@footnote@on@page@\csuse{page@of@footnote@\thefootnote}}
(结果为空字符串)。
完整代码
\documentclass{scrbook}
\usepackage{etoolbox}
\newlength{\fnotemargin}
\setlength{\fnotemargin}{4.5mm}
\deffootnote{\fnotemargin}{1em}{%
\makebox[\fnotemargin][l]{\thefootnotemark}%
}
\makeatletter
\preto{\footnote}{%
\protected@write\@auxout{}{%
\string\@namedef{page@of@footnote@\thefootnote}{\thepage}
\string\@namedef{max@footnote@on@page@\thepage}{\thefootnote}
}%
\def\thispagesmaxfootnote{%
\csuse{max@footnote@on@page@\csuse{page@of@footnote@\thefootnote}}%
}%
(max. fnote on this page = \thispagesmaxfootnote)% FOR TESTING
\ifnum\thispagesmaxfootnote>99\relax
\setlength{\fnotemargin}{4.5mm}
\fi
}
\makeatother
\begin{document}
Test\footnote{Test}\par
Test\footnote{Test}
\newpage
Test\footnote{Test}
\newpage
\setcounter{footnote}{100}
Test\footnote{Test}
\end{document}
问题
所以问题是,为什么虽然信息写在文件中,但我却得到了错误的结果aux
?
答案1
完整代码应如下所示(我在 MWE 本身添加了一些注释/解释):
\documentclass{scrbook}
\usepackage{etoolbox}
% a new length for the footnote margin
\newlength{\fnotemargin}
% the "normal" (narrow) value should be 4.5mm
\setlength{\fnotemargin}{4.5mm}
% now we define how the footnotes look
% - first argument: margin for footnotes = the above defined length
% - second argument: the par intent for footnotes = 1em
% - third argument: how the number should be printed in the apparatus
% = a box of with \fnotemargin containing the mark left aligned
\deffootnote{\fnotemargin}{1em}{%
\makebox[\fnotemargin][l]{\thefootnotemark}%
}
% make @ available as part of macro names
\makeatletter
% with \preto we prepend some code to \footnote, it will be executed every time \footnote is used
\preto{\footnote}{%
% wirte the following (third argument) to the *.aux file (this will be used later)
\protected@write\@auxout{}{%
% \string protects the following macro to be written verbatim in the file
% The first line defines a macro named page@of@footnote@n where n is the current
% (at time of writing to file) footnote value. This the definition is the current
% page number.
\string\csgdef{page@of@footnote@\the\value{footnote}}{\thepage}
% the second line defines a macro named max@footnote@on@page@m (m = current page
% number) to be the current footnote number.
\string\csgdef{max@footnote@on@page@\thepage}{\the\value{footnote}}
% When later (second/further TeX run) the auxfile is read all these defenition will be
% executed. Then we have one macro for every footnote (\page@of@footnote@n)
% containing the info on which page it appears and multiple definitions
% for each page -- where the last one wins -- max@footnote@on@page@m containing
% the information about the highest footnote number on that page.
% (Note: The use of \csname (variants of it) allows us to use numbers as part of
% macro names which is prohibited normally.)
}%
% now we define the macro \thispagesmaxfootnote to contain the value of the
% highes footnote on this page. Ths information is taken from the definitions
% written to the *.aus file in the previous TeX run.
\edef\thispagesmaxfootnote{%
0\csuse{max@footnote@on@page@\csuse{page@of@footnote@\the\value{footnote}}}%
}%
% (max. fnote on this page = \thispagesmaxfootnote)% FOR TESTING
% lastly we test wether the footnote value is grater than 99 (the couter is off by
% one at this pointcompared with the printed value; thus <98 instead of <99).
\ifnum\thispagesmaxfootnote>98\relax
% if TRUE we set the wide margin
% (set wide margin)% FOR TESTING
\setlength{\fnotemargin}{7.5mm}%
\else
% if FALSE we set the narrow margin
% (set norrow margin)% FOR TESTING
\setlength{\fnotemargin}{4.5mm}%
\fi
}
% for cases where the footnote number is reset in each chapter for instance the
% above definition must be expaned to make the information macro cotain
% the chapter number too. I.e. page@of@footnote@\the\value{footnote}
% could be changed to page@of@footnote@\the\value{chapter}@\the\value{footnote}
% deactivate @ as part of macro names
\makeatother
\begin{document}
Test\footnote{Test}\par
Test\footnote{Test}
\newpage
Test\footnote{Test}
\newpage
\setcounter{footnote}{100}
Test\footnote{Test}
\end{document}
笔记:如果每个章节的脚注计数器都重置为 1,则必须向辅助宏添加章节标识符,如下所示:
\preto{\footnote}{%
\protected@write\@auxout{}{%
\string\csgdef{page@of@footnote@chap\the\value{chapter}@fn\the\value{footnote}}{\thepage}
\string\csgdef{max@footnote@on@page@\thepage}{\the\value{footnote}}
}%
\edef\thispagesmaxfootnote{%
0\csuse{max@footnote@on@page@\csuse{page@of@footnote@chap\the\value{chapter}@fn\the\value{footnote}}}%
}%
\ifnum\thispagesmaxfootnote>98\relax
\setlength{\fnotemargin}{1.75em}%
\else
\setlength{\fnotemargin}{1.25em}%
\fi
}