2

In this thread, @schtandard taught me how to predetermine whether I had 4 enumerate levels or 3, and condition on this how I supecified my labels. His/her code worked beautifully so I tried to extend it to check whether I had 2, 3, or 4 levels. But I wasn't able to do it. Here's my extension of @schtandard's code. The example has 3 levels, and if I were coding correctly, the macro \whichLevel would say 3 levels, but it says two levels, so I'm not conditioning properly. Could somebody point out what I'm doing wrong, please?

\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}
  • In case you expected otherwise: @-mentions don't do anything in questions. – schtandard Sep 27 '19 at 15:48
1

There are two issues with your code:

  1. You need to set \global\@enumerate@leveliv@usedtrue whenever level 4 is used, regardless of how many levels you expected at the beginning of the document (the same holds for the other levels).
  2. You need to set the all switches globally in the .aux file.
\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}

However, if you want to store the maximum list depth, it is better to use a counter than a series of switches.

\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}
  • Perfect, thanks very much – Leo Simon Sep 28 '19 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.