在分配标签类型之前预先确定嵌套级别数(后续)

在分配标签类型之前预先确定嵌套级别数(后续)

此主题,@schtandard 教我如何预先确定我有 4 个枚举级别还是 3 个,并以此为条件确定我如何指定标签。他的/她的代码运行良好,所以我尝试扩展它以检查我有 2、3 还是 4 个级别。但我做不到。这是我对 @schtandard 代码的扩展。该示例有 3 个级别,如果我编码正确,宏 \whichLevel 会说 3 个级别,但它说 2 个级别,所以我没有正确调节。有人能指出我做错了什么吗?

\documentclass{amsart}
\usepackage{enumitem}
\makeatletter
  \newif\if@enumerate@leveliv@used
  \newif\if@enumerate@levelii@used
  \newif\if@enumerate@leveliii@used
  \AtBeginDocument{%
    \if@enumerate@leveliv@used
        \def\whichLevel{level 4 is the true condition}
      \setlist[enumerate,1]{label={\arabic*)}}
      \setlist[enumerate,2]{label={\Roman*)}}
      \setlist[enumerate,3]{label={\roman*)}}
      \setlist[enumerate,4]{label={\alph*)}, before*= {\global\@enumerate@leveliv@usedtrue}}
    \else
        \if@enumerate@leveliii@used
            \def\whichLevel{level 3 is the true condition}
            \setlist[enumerate,1]{label={\arabic*)}}
            \setlist[enumerate,2]{label={\roman*)}}
            \setlist[enumerate,3]{label={\alph*)}, before*= {\global\@enumerate@leveliii@usedtrue}}
        \else
            \def\whichLevel{level 2 is the true condition}
            \setlist[enumerate,1]{label={\roman*)}}
            \setlist[enumerate,2]{label={\alph*)},before*= {\global\@enumerate@levelii@usedtrue}}
        \fi
    \fi
    \@enumerate@leveliv@usedfalse
    \@enumerate@levelii@usedfalse
    \@enumerate@leveliii@usedfalse
  }
  \AtEndDocument{%
    \protected@write\@auxout{}{%
      \global
        %
        %
       \if@enumerate@leveliv@used
            \protect\@enumerate@leveliv@usedtrue
       \else
            \protect\@enumerate@leveliv@usedfalse
            \if@enumerate@leveliii@used
                \protect\@enumerate@leveliii@usedtrue
            \else
                \protect\@enumerate@leveliii@usedfalse
                \if@enumerate@levelii@used
                    \protect\@enumerate@levelii@usedtrue
                \else
                    \protect\@enumerate@levelii@usedfalse
                \fi
            \fi
       \fi
    }%
  }
\makeatother
\begin{document}
\begin{enumerate}
  \item level 1
       \item \whichLevel
  \begin{enumerate}
    \item   level 2
    \begin{enumerate}
        \item   level 3
    \end{enumerate}
  \end{enumerate}
\end{enumerate}
\end{document}

答案1

您的代码有两个问题:

  1. \global\@enumerate@leveliv@usedtrue无论文档开头预计有多少个级别,您都需要在使用 4 级时进行设置(其他级别也是如此)。
  2. 您需要在文件中全局设置所有开关.aux
\documentclass{amsart}

\usepackage{enumitem}

\makeatletter
  \newif\if@enumerate@leveliv@used
  \newif\if@enumerate@levelii@used
  \newif\if@enumerate@leveliii@used
  \AtBeginDocument{%
    \if@enumerate@leveliv@used
      \def\whichLevel{level 4 is the true condition}
      \setlist[enumerate,1]{label={\arabic*)}}
      \setlist[enumerate,2]{label={\Roman*)},       before*={\global\@enumerate@levelii@usedtrue}}
      \setlist[enumerate,3]{label={\roman*)},       before*={\global\@enumerate@leveliii@usedtrue}}
      \setlist[enumerate,4]{label={\alph*)},        before*={\global\@enumerate@leveliv@usedtrue}}
    \else
        \if@enumerate@leveliii@used
            \def\whichLevel{level 3 is the true condition}
            \setlist[enumerate,1]{label={\arabic*)}}
            \setlist[enumerate,2]{label={\roman*)}, before*={\global\@enumerate@levelii@usedtrue}}
            \setlist[enumerate,3]{label={\alph*)},  before*={\global\@enumerate@leveliii@usedtrue}}
            \setlist[enumerate,4]{label={XX},       before*={\global\@enumerate@leveliv@usedtrue}}
        \else
            \def\whichLevel{level 2 is the true condition}
            \setlist[enumerate,1]{label={\roman*)}}
            \setlist[enumerate,2]{label={\alph*)},  before*={\global\@enumerate@levelii@usedtrue}}
            \setlist[enumerate,3]{label={XX},       before*={\global\@enumerate@leveliii@usedtrue}}
            \setlist[enumerate,4]{label={XX},       before*={\global\@enumerate@leveliv@usedtrue}}
        \fi
    \fi
    \@enumerate@leveliv@usedfalse
    \@enumerate@levelii@usedfalse
    \@enumerate@leveliii@usedfalse
  }
  \AtEndDocument{%
    \protected@write\@auxout{}{%
      \global
      \if@enumerate@leveliv@used
        \protect\@enumerate@leveliv@usedtrue
      \else
        \protect\@enumerate@leveliv@usedfalse
        \global
        \if@enumerate@leveliii@used
          \protect\@enumerate@leveliii@usedtrue
        \else
          \protect\@enumerate@leveliii@usedfalse
          \global
          \if@enumerate@levelii@used
            \protect\@enumerate@levelii@usedtrue
          \else
            \protect\@enumerate@levelii@usedfalse
          \fi
        \fi
      \fi
    }%
  }
\makeatother

\begin{document}

\begin{enumerate}
  \item level 1
  \item \whichLevel
  \begin{enumerate}
    \item level 2
    \begin{enumerate}
      \item level 3
    \end{enumerate}
  \end{enumerate}
\end{enumerate}

\end{document}

但是,如果要存储最大列表深度,最好使用计数器而不是一系列开关。

\documentclass{amsart}

\usepackage{enumitem}

\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*)}, before*={\register@enumerate@level{1}}}
      \setlist[enumerate,2]{label={\roman*)},  before*={\register@enumerate@level{2}}}
      \setlist[enumerate,3]{label={\alph*)},   before*={\register@enumerate@level{3}}}
      \setlist[enumerate,4]{label={XX},        before*={\register@enumerate@level{4}}}
    \or
      % 4 levels
      \setlist[enumerate,1]{label={\arabic*)}, before*={\register@enumerate@level{1}}}
      \setlist[enumerate,2]{label={\Roman*)},  before*={\register@enumerate@level{2}}}
      \setlist[enumerate,3]{label={\roman*)},  before*={\register@enumerate@level{3}}}
      \setlist[enumerate,4]{label={\alph*)},   before*={\register@enumerate@level{4}}}
    \else
      % default
      \setlist[enumerate,1]{label={\roman*)},  before*={\register@enumerate@level{1}}}
      \setlist[enumerate,2]{label={\alph*)},   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}

There are \enumeratemaxlevel\ enumeration levels in this document.

\begin{enumerate}
  \item level 1
  \begin{enumerate}
    \item level 2
    \begin{enumerate}
      \item level 3
    \end{enumerate}
  \end{enumerate}
\end{enumerate}

\end{document}

相关内容