8

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 05 '15 at 01: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 05 '15 at 01:39
  • @jon It should return 5, just like the \item after \item[8] returns 9. – algorithmshark Feb 05 '15 at 01:44
  • 2
    And if there are 4 \items after the fourth one? Should it use 8 again? – cfr Feb 05 '15 at 01:47
  • What does 'compatible' mean? That you need to be able to use the standard enumitem options to further customise the output? Or that you can use the packages facilities for other environments, but not the customised one? – cfr Feb 05 '15 at 01:49
  • @cfr Sure. With optional argument, it changes the counter; without, it simply increments. – algorithmshark Feb 05 '15 at 01:50
  • @cfr I would like [label=(\letter*)] to work, at least. – algorithmshark Feb 05 '15 at 01:51
  • 1
    @algorithmshark Well that isn't even supported by enumitem! – cfr Feb 05 '15 at 02:58

2 Answers2

7

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

egreg
  • 1,121,712
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

Manuel
  • 27,118