7

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}

to give the following Good output, instead of the Bad output:

Good and Bad outputs, as given by MWE below

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}

Bad:

\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.

What is the correct way of going about this?

  • 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
  • 2
    And if there are 4 \items after the fourth one? Should it use 8 again? – cfr Feb 5 '15 at 1:47
  • 1
    @algorithmshark Well that isn't even supported by enumitem! – cfr Feb 5 '15 at 2:58
5

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}%
  \addtocounter{\@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}

enter image description here

4
\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.

enter image description here

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

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.