6

How can I add leading zeros in the items that are listed by the packages etaremune and enumerate? For example, I would like to have the labels to be:

A01, A02, A03, ..., A09, A10

Here is an example code. How can I add the leading zeros locally and globally?

\documentclass{article}

\usepackage{etaremune}
\usepackage{enumitem}
\setlist[enumerate]{nosep} % To remove the spacing


\begin{document}
First list

\renewcommand*{\labelenumi}{A\theenumi.}
\begin{etaremune}[topsep=0pt,itemsep=0pt,partopsep=0pt,parsep=0pt,] 
    \item item 10
    \item item 09
    \item item 08
    \item item 07
    \item item 06
    \item item 05
    \item item 04
    \item item 03
    \item item 02
    \item item 01
\end{etaremune}

Second list
\begin{enumerate}[label={M\arabic*.}]
    \item item 01
    \item item 02
\end{enumerate}

Third list
\begin{enumerate}[label={M\arabic*.}, resume]
    \item item 03
    \item item 04
\end{enumerate}

\end{document}

Here is enter image description herethe output:

2 Answers2

8

[Edit I'm editting this to make the solution clearer, inlight of the evolution of the both the question and its solution. As a result, the comments below may not make much sense...]

The code below makes it possible to do what you want using just the enumitem package. In particular, etaremune is no longer needed because we trick enumerate into behaving like an etaremune environment using enumitem.

The code uses enumitem's \AddEnumerateCounter command to define two new counters: \PaddingUp and \PaddingDown. Both counters pad item numbers with a 0 if the counter is less than 10. The \PaddingDown counter has the extra feature of surreptitiously decreasing the item counter by one (it actually decreases it by two so that when the counter is next incremented the net effect is that it will have decreased by one).

The \PaddingUp and \PaddingDown counters can be used like any of the standard enumitem counter macros. Consequently,

\begin{enumerate}[label=Up\PaddingUp*.]
...
\end{enumerate}

creates an enumerate environment with item labels Up01., Up02., ... whereas

\begin{enumerate}[label=Down\PaddingDown*.,start=10]
...
\end{enumerate}

creates an enumerate environment with item labels Down10., Down09., ...

The following code prodcuces the OP's requested output:

\documentclass{article}
\usepackage{enumitem}

\makeatletter
\AddEnumerateCounter{\PaddingUp}{\two@digits}{A00}
\AddEnumerateCounter{\PaddingDown}{\two@digits}{A00}
\newcommand\PaddingUp[1]{\expandafter\two@digits\csname c@#1\endcsname}
\newcommand\PaddingDown[1]{\PaddingUp{#1}\addtocounter{#1}{-2}}
\makeatother

\setlist[enumerate]{nosep,label=M\PaddingUp*.} % To remove the spacing
\newlist{etaremune}{enumerate}{1}% one level of nesting
\setlist[etaremune]{nosep,label=A\PaddingDown*.}

\begin{document}

First list
\begin{etaremune}[start=10]
    \item item 10
    \item item 09
    \item item 08
    \item item 07
    \item item 06
    \item item 05
    \item item 04
    \item item 03
    \item item 02
    \item item 01
\end{etaremune}

Second list
\begin{enumerate}
    \item item 01
    \item item 02
\end{enumerate}

Third list
\begin{enumerate}[resume]
    \item item 03
    \item item 04
\end{enumerate}

\end{document}

For good measure, the code above defines an etaremune enviroment with the nosep option. This is possibly misleading, however, because the decreasing counters are given by setting the label to label=\PaddingDown*. Above, this is done using setlist. Note that it is necessay to explicitly set the starting value of the item counters in an etaremune environment. If you don't set a starting value then they will start from 1, which is probably not what you want.

Finally, perhaps I should point out that it is easy to change the letters etc labelling these environments as you use them. For example, to change them to use P's and Q's you would write:

\begin{enumerate}[label=P\PaddingDown*.]% item -> P#. going up
    \item item 01 \item item 02
\end{enumerate}

\begin{enumerate}[label=Q\PaddingUp*.]% item -> P#. going down
    \item item 01 \item item 02
\end{enumerate}
  • Thanks. It works perfectly. How can I do the same thing for enumerate? The code you kindly posted only works for etaremune and the ones in my document that are using enumerate are intact and no leading zeros are printed. Can this be modified such that it works for enumerate as well: \begin{enumerate}[label={M\arabic*.},] \item item 01 \item item 02 \end{enumerate} – shashashamti2008 Aug 04 '14 at 03:19
  • @A2009 Actually, this is a "bug" in my fix in that it will also modify how the counters are printed in an enumerate environment. Luckily you want this! To have this work you need to put the \renewcommand\theenumi... in your preamble at the top of your document. I think that the eumerate environments for which this is failing must be before this commend in your document. Is this correct? –  Aug 04 '14 at 03:28
  • Actually, it is already at the top of the document. – shashashamti2008 Aug 04 '14 at 03:30
  • Oh, I see from your comment that you must be using the enumitem package, so the reason why this doesn't work is that the label={M\arabic*.} is redefining the labels...will see if there is a good way to get this to work in the enumitem framework. –  Aug 04 '14 at 03:33
  • 1
    If you only want your modified \theenumi to have an effect locally, place it in the environment before for the first \item. – A.Ellett Aug 04 '14 at 03:41
  • @A2009 I've added some code to make this work with enumitem. –  Aug 04 '14 at 03:49
  • Thanks Andrew. Unfortunately I have many lists whose labels are customozied. Some of them start with M, some start with A, C, T, P, J. Can I have your thoughts on this? – shashashamti2008 Aug 04 '14 at 04:09
  • @A2009 The code above will work fine for you then. I have redefine the etaremune environment using the features of enumitem. You can define your labels at the start of the environment using label=M\ZoerPadded*. for increasing conters and label=A\zeroPadded*. for decreasing counters replacing A and M s needed –  Aug 04 '14 at 04:17
  • Instead of \@PaddingUp and \@PaddingDown (which are the same, anyway) you could use LaTeX's \two@digits. Is there a reason you use TeX's \def instead of LaTeX's \newcommand? – cgnieder Aug 04 '14 at 07:57
  • @cgnieder I didn't know about \two@digits. Of course, it's equivalent to what's above, but it does shorten the commands so I'll edit. Will also change to using \newcommand...I'm aware of the arguments in favour of \newcomman but I still use \def, partly because it is more flexible and partly out of habit. –  Aug 04 '14 at 11:01
  • @A2009 As you didn't say that this solved your problem perhaps it is not clear from the solution that you can modify the labels on the environments above as you need them. I've added another example to make this clearer. –  Aug 11 '14 at 23:49
0

This can be done directly with these two lines:

\newcommand\harabic[1]{\ifnum#1<10\relax0\fi#1}
\renewcommand*{\labelenumi}{\labelprefix\protect\harabic{\theenumi}.}

Then, one only need define the leading letter by way of \labelprefix. The MWE:

\documentclass{article}
\usepackage{etaremune}
\usepackage{enumitem}
\setlist[enumerate]{nosep} % To remove the spacing
\newcommand\harabic[1]{\ifnum#1<10\relax0\fi#1}
\renewcommand*{\labelenumi}{\labelprefix\protect\harabic{\theenumi}.}
\def\labelprefix{M}
\begin{document}
First list

{\def\labelprefix{A}%
\begin{etaremune}[topsep=0pt,itemsep=0pt,partopsep=0pt,parsep=0pt,] 
    \item item 10
    \item item 09
    \item item 08
    \item item 07
    \item item 06
    \item item 05
    \item item 04
    \item item 03
    \item item 02
    \item item 01
\end{etaremune}}

Second list
\begin{enumerate}
    \item item 01
    \item item 02
\end{enumerate}

Third list
\begin{enumerate}[resume]
    \item item 03
    \item item 04
\end{enumerate}

\end{document}

enter image description here