LaTeX Manually Numbered Items

I write all my school assignments and exercises in LaTeX and this leads to many enumerate environments. Of course, the exercises aren’t given out as “1 - 12”, but rather “4, 6, 13 b, 24”, and to deal with this I’ve had to write the following:

\begin{enumerate}
    \setcounter{enumi}{3}
    \item Answer

    \setcounter{enumi}{5}
    \item Answer

    \setcounter{enumi}{12}
    \item
        \begin{enumerate}
            \setcounter{enumii}{1}
            \item Part B answer
        \end{enumerate}

    \setcounter{enumi}{23}
    \item Last answer
\end{enumerate}

This method of resetting the counters would be fine if I only had to do this a couple of times, but for the questions I get assigned this is becomes very tedious and error-prone; I often forget to set the counter to 1 less than the number I want (since \item increments it) and forget which counter I need to be resetting. On top of this, it’s harder to skim the text to find question 13 since you have to look for ‘12’.

The simpler way

To simplify numbering these exercise questions, I’ve created a new command called \nitem. It takes one argument: the number you want the question to be. This makes it more clear what question number you’re working on and makes it easier to find the question later if you want to come back to it.

The new command is the following:

\makeatletter
\newcommand{\nitem}[1]{
    \newcount\cnt
    \cnt=#1
    \advance\cnt by -1
    \setcounter{enum\romannumeral\@enumdepth}{\cnt}\item
}
\makeatother

If you want a simplified version which requires the calc package, the following is equivalent:

\usepackage{calc}
\makeatletter
\newcommand{\nitem}[1]{
    \setcounter{enum\romannumeral\@enumdepth}{#1-1}\item
}
\makeatother

Now, the example above can be replaced by the following:

\begin{enumerate}
    \nitem{4} Answer

    \nitem{6} Answer

    \nitem{13}
        \begin{enumerate}
            \nitem{2} Part B answer
        \end{enumerate}

    \nitem{24} Last answer
\end{enumerate}

Since you may not want to set consecutive numbers (although for exercises it will make them easier to find), you can still use \item just as you did before and it will continue numbering as expected.

Published on September 10, 2014.