1

I use the enumitem package to set counter types, for example most of the time I don't need more than 3 levels, and this is my preferred setup:

\setlist[enumerate,1]{label={\arabic*)},ref={\arabic*)}}
\setlist[enumerate,2]{label={\roman*)},ref={\roman*)}}
\setlist[enumerate,3]{label={\alph*)},ref={\alph*)}}

Just in case I need a fourth level, I've set

\setlist[enumerate,4]{label={\Alph*)},ref={\Alph*)}}

But of course, conditional on needing four levels, It would be much more pleasing to use something like

\setlist[enumerate,1]{label={\arabic*)},ref={\arabic*)}}
\setlist[enumerate,2]{label={\Roman*)},ref={\Roman*)}}
\setlist[enumerate,3]{label={\roman*)},ref={\roman*)}}
\setlist[enumerate,4]{label={\alph*)},ref={\alph*)}}

So what I'd like to do is find the maximum number of levels after the first run of pdflatex, and then condition on that number to set the label types. is there some way of doing this?

Here's an MWE. For this example I have four levels and want to condition on the existence of this many active levels, and then switch to \Roman in level 2 and \roman in level 3.

\documentclass{amsart}
\usepackage{enumitem}
\setlist[enumerate,1]{label={\arabic*).},ref={\arabic*)}}
\setlist[enumerate,2]{label={\roman*)},ref={\roman*)}}
\setlist[enumerate,3]{label={\alph*)},ref={\alph*)}}
\setlist[enumerate,4]{label={\Roman*)},ref={\Roman*)}}
\begin{document}
\begin{enumerate}
    \item level 1
    \begin{enumerate}
        \item  level 2
        \begin{enumerate}
            \item level 3   
            \begin{enumerate}
                \item level 4
            \end{enumerate} 
        \end{enumerate} 
    \end{enumerate}
\end{enumerate}
\end{document}

Thanks for any suggestions.

  • Sure, just use it to the .aux file. Could you provide an MWE, please? This will very much increase your chances that someone will post a complete answer. – schtandard Aug 30 at 9:31
  • I've added an MWE – Leo Simon Aug 31 at 13:03
1

Sure, just use the .aux file, for example like this.

\documentclass{article}

\usepackage{enumitem}

\makeatletter
  \newif\if@enumerate@leveliv@used
  \AtBeginDocument{%
    \if@enumerate@leveliv@used
      \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
      \setlist[enumerate,1]{label={\arabic*)}}
      \setlist[enumerate,2]{label={\roman*)}}
      \setlist[enumerate,3]{label={\alph*)}}
      \setlist[enumerate,4]{label={\Alph*)}, before*={\global\@enumerate@leveliv@usedtrue}}
    \fi
    \@enumerate@leveliv@usedfalse
  }
  \AtEndDocument{%
    \protected@write\@auxout{}{%
      \global
      \if@enumerate@leveliv@used
        \protect\@enumerate@leveliv@usedtrue
      \else
        \protect\@enumerate@leveliv@usedfalse
      \fi
    }%
  }
\makeatother

\begin{document}

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

\end{document}

  • As always with the .aux file, you have to run LaTeX twice for changes to take effect.
  • If you want ref to have the same value as label, you don't have to set it explicitly.
  • Perfect, thanks very much. – Leo Simon Sep 2 at 12:27

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.