# Optional argument to \item

I am typesetting an ordered list where the entries may appear out of order, but should be assumed in order otherwise. I don't want to have to jump through \setcounter hoops to do this. That is, I want something like

\begin{enumerate}
\item First
\item Second
\item[8] Eighth!
\item Ninth!
\item[4] Fourth
\end{enumerate}


A minimum working example follows.

\documentclass{article}
\usepackage{enumitem} % preferably something compatible with this guy
\begin{document}

Good:

\begin{enumerate}
\item[1.] First
\item[2.] Second
\item[8.] Eighth!
\item[9.] Ninth!
\item[4.] Fourth
\end{enumerate}

\begin{enumerate}
\item First
\item Second
\item[8] Eighth!
\item Ninth!
\item[4] Fourth
\end{enumerate}

\end{document}


It should be noted that I don't want to have to specify the period afterwards, in case my list using a different numbering, e.g. Roman. Compatibility with enumitem is preferred, but not required.

• And what should the counter return if you have a plain \item after the \item[4] Fourth? Will it be 6? 3? something else? – jon Feb 5 '15 at 1:39
• If you tell us what interface are you looking for it would be easier. I mean, you want that the optional argument changes only the value of the counter, and then let the label print itself (that would mean, under certain circumstances, that \item[1] would print (a), for instance), or you want to have both options. In case it's the latter, what “interface” are you thinking of to differentiate both situations? – Manuel Feb 5 '15 at 1:39
• @jon It should return 5, just like the \item after \item[8] returns 9. – algorithmshark Feb 5 '15 at 1:44
• And if there are 4 \items after the fourth one? Should it use 8 again? – cfr Feb 5 '15 at 1:47
• @algorithmshark Well that isn't even supported by enumitem! – cfr Feb 5 '15 at 2:58

You don't want to abuse the optional argument to \item; better defining a \nextitem command:

\documentclass{article}

\makeatletter
\newcommand\nextitem[1]{%
\setcounter{\@enumctr}{#1}%
}
\makeatother

\begin{document}
\begin{enumerate}
\item First
\item Second
\nextitem{8}
\item Eighth!
\item Ninth!
\nextitem{4}
\item Fourth
\end{enumerate}
\end{document}


\documentclass{scrartcl}

\usepackage{enumitem}

\usepackage{etoolbox,xparse}

\AtBeginEnvironment{enumerate}
{\let\originalitem\item
\RenewDocumentCommand\item{o}{\IfValueTF{#1}
{\setcounter{enumi}{\numexpr#1-1\relax}\originalitem}{\originalitem}}}

\begin{document}

Good:

\begin{enumerate}
\item First
\item Second
\item[8] Eighth!
\item Ninth!
\item[4] Fourth
\end{enumerate}

And

\begin{enumerate}[label=(\alph*)]
\item First
\item Second
\item[8] Eighth!
\item Ninth!
\item[4] Fourth
\end{enumerate}

\end{document}


I removed some \begingroup..\endgroup I had originally, and I don't know why it works the same way wether I grouped or not.

This does only work with one level enumerations. You can't nest enumerates.

• You can always get the original behaviour with \originalitem. – Manuel Feb 5 '15 at 2:06