8

I was wondering if there was a command to find what at what depth of the enum counter (i.e. enumi, enumii, etc.) a command was at compile time. I'm attempting to create a \question command. What I have so far:

\newcommand{\question}[2][\value{enumi}+1]{\setcounter{enumi}{#1-1} \item \textbf{#2}}

Which works great for the first level (Note: requires the calc package). What I'm looking for is a command (or series of commands) to replace the \value{enumi}+1 with whatever the current depth is so that something like:

\begin{enumerate}
   \question{Whatever}
   \question[10]{Blah}
      \begin{enumerate}
         \question[2]{Stuff}
      \end{enumerate}
\end{enumerate}

would produce output like:

1. Whatever
10. Blah
   (b). Stuff
0

1 Answer 1

9

You need to know the depth of the current enumerate to modify the appropriate counter; \@enumdepth holds the current enumeration nesting depth:

\documentclass{article}
\usepackage{calc}

\makeatletter
\newcommand{\question}[2][\value{enum\romannumeral\@enumdepth}+1]{\setcounter{enum\romannumeral\@enumdepth
}{#1-1} \item \textbf{#2}}
\makeatother
\begin{document}

\begin{enumerate}
   \question{Whatever}
   \question[10]{Blah}
      \begin{enumerate}
         \question[2]{Stuff}
        \begin{enumerate}
           \question[6]{Other stuff}
        \end{enumerate}
      \end{enumerate}
\end{enumerate}

\end{document}

enter image description here

3
  • 3
    You beat me to it. Two notes: 1) You don't need \the after \romannumeral as it automatically looks for a number; 2) I'd include the enum\romannumeral\@enumdepth in the default for the optional argument as well as in the command body.
    – Joseph Wright
    Sep 5, 2011 at 19:19
  • Thanks, I wasn't able to find anything on \@enumdepth when I was searching (although I can now obviously find plenty). Sep 5, 2011 at 20:02
  • You can find the definition \newcount\@enumdepth \@enumdepth = 0 in the LaTeX kernel. Run texdoc source2e in a terminal, and search for \@enumdepth. Sep 5, 2011 at 21:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .