在几个帖子中,第二个是这个,,@schtandard 教我如何根据我拥有的枚举级别数进行条件化。下面的 MWE 说明了结构中一个非常奇怪的问题。编写的代码运行良好,识别出我使用的级别数为 3 并相应地运行。但是,如果您注释掉最后一行,即,word
它会认为只有两个级别。然后,如果您注释掉倒数第二行,即,\newpage
它再次正常工作。所以,有一个关于\newpage
,它是不是紧接着是一些文本,把事情搞乱了。(显然,这个问题出现的现实环境更加复杂,但这个例子似乎把问题隔离开了。)有人能解释一下发生了什么,以及如何使自己免受这个问题的影响吗?
非常感谢您的帮助!
\documentclass[11pt,reqno,fleqn]{amsart}%
\usepackage{enumitem}
%This came from this thread
% https://tex.stackexchange.com/questions/508327/predetermine-the-number-of-nested-levels-before-assigning-label-types-followup?noredirect=1#comment1289746_508327
\makeatletter
\newcounter{enumerate@max@level}
\def\register@enumerate@level#1{%
\ifnum\value{enumerate@max@level}<#1\relax
\setcounter{enumerate@max@level}{#1}%
\fi
}
\AtBeginDocument{%
\ifcase\numexpr\value{enumerate@max@level}-3\relax
% 3 levels
\setlist[enumerate,1]{label={\arabic*)\hspace{1ex}}, before*={\register@enumerate@level{1}}}
\setlist[enumerate,2]{label={\roman*)\hspace{1ex}}, before*={\register@enumerate@level{2}}}
\setlist[enumerate,3]{label={\alph*)\hspace{1ex}}, before*={\register@enumerate@level{3}}}
\setlist[enumerate,4]{label={XX}, before*={\register@enumerate@level{4}}}
\or
% 4 levels
\setlist[enumerate,1]{label={\arabic*)\hspace{1ex}}, before*={\register@enumerate@level{1}}}
\setlist[enumerate,2]{label={\Roman*)\hspace{1ex}}, before*={\register@enumerate@level{2}}}
\setlist[enumerate,3]{label={\roman*)\hspace{1ex}}, before*={\register@enumerate@level{3}}}
\setlist[enumerate,4]{label={\alph*)\hspace{1ex}}, before*={\register@enumerate@level{4}}}
\else
% default
\setlist[enumerate,1]{label={\roman*)\hspace{1ex}}, before*={\register@enumerate@level{1}}}
\setlist[enumerate,2]{label={\alph*)\hspace{1ex}}, before*={\register@enumerate@level{2}}}
\setlist[enumerate,3]{label={XX}, before*={\register@enumerate@level{3}}}
\setlist[enumerate,4]{label={XX}, before*={\register@enumerate@level{4}}}
\fi
\edef\enumeratemaxlevel{\theenumerate@max@level}
\setcounter{enumerate@max@level}{0}%
}
\AtEndDocument{%
\protected@write\@auxout{}{%
\protect\setcounter{enumerate@max@level}{\theenumerate@max@level}%
}%
}
\makeatother
\begin{document}
\begin{enumerate}
\item Enumerate 1)
\begin{enumerate}
\item Enumerate i)
\begin{enumerate}
\item Enumerate a)
\end{enumerate}
\end{enumerate}
\end{enumerate}
\newpage
word
\end{document}
答案1
\theenumerate@max@level
立即将的值写入.aux
,而不是使用\protected@write
:
\documentclass{amsart}
\usepackage{enumitem}
%This came from this thread
% https://tex.stackexchange.com/questions/508327/predetermine-the-number-of-nested-levels-before-assigning-label-types-followup?noredirect=1#comment1289746_508327
\makeatletter
\newcounter{enumerate@max@level}
\def\register@enumerate@level#1{%
\ifnum\value{enumerate@max@level}<#1\relax
\setcounter{enumerate@max@level}{#1}%
\fi
}
\AtBeginDocument{%
\ifcase\numexpr\value{enumerate@max@level}-3\relax
% 3 levels
\setlist[enumerate,1]{label={\arabic*)\hspace{1ex}}, before*={\register@enumerate@level{1}}}
\setlist[enumerate,2]{label={\roman*)\hspace{1ex}}, before*={\register@enumerate@level{2}}}
\setlist[enumerate,3]{label={\alph*)\hspace{1ex}}, before*={\register@enumerate@level{3}}}
\setlist[enumerate,4]{label={XX}, before*={\register@enumerate@level{4}}}
\or
% 4 levels
\setlist[enumerate,1]{label={\arabic*)\hspace{1ex}}, before*={\register@enumerate@level{1}}}
\setlist[enumerate,2]{label={\Roman*)\hspace{1ex}}, before*={\register@enumerate@level{2}}}
\setlist[enumerate,3]{label={\roman*)\hspace{1ex}}, before*={\register@enumerate@level{3}}}
\setlist[enumerate,4]{label={\alph*)\hspace{1ex}}, before*={\register@enumerate@level{4}}}
\else
% default
\setlist[enumerate,1]{label={\roman*)\hspace{1ex}}, before*={\register@enumerate@level{1}}}
\setlist[enumerate,2]{label={\alph*)\hspace{1ex}}, before*={\register@enumerate@level{2}}}
\setlist[enumerate,3]{label={XX}, before*={\register@enumerate@level{3}}}
\setlist[enumerate,4]{label={XX}, before*={\register@enumerate@level{4}}}
\fi
\edef\enumeratemaxlevel{\theenumerate@max@level}
\setcounter{enumerate@max@level}{0}%
}
\AtEndDocument{%
\immediate\write\@auxout{%
\noexpand\setcounter{enumerate@max@level}{\theenumerate@max@level}%
}%
}
\makeatother
\begin{document}
\begin{enumerate}
\item Enumerate 1)
\begin{enumerate}
\item Enumerate i)
\begin{enumerate}
\item Enumerate a)
\end{enumerate}
\end{enumerate}
\end{enumerate}
\newpage
%word
\end{document}
这会将以下内容保留在您的.aux
:
\relax
\newlabel{tocindent-1}{0pt}
\newlabel{tocindent0}{0pt}
\newlabel{tocindent1}{0pt}
\newlabel{tocindent2}{0pt}
\newlabel{tocindent3}{0pt}
\global \c@enumerate@max@level 3\relax
\gdef \@abspage@last{1}