3

Usually, when you have the following document:

\documentclass{article}
\usepackage{enumitem}
\begin{document}
\begin{enumerate}
\item Mary
\item[had] a little 
\item lamb
\end{enumerate}
\end{document}

It will format as

  1. Mary
had a little
  2. lamb

I would want it to format as

  1. Mary
  2 (had). a little
  3. lamb

In other words, I would like the optional argument that can be passed to an \item not to replace the usual label, but I want it to appear additionally. In particular, the counter should still be stepped.

This might seem like a subtlety, but note that the argument should appear before the period and after the counter. In fact, this is just a minimal example - I would like to have greater flexibility in placing the optional argument within the label.

Is there a way to do this with the enumitem package?

2 Answers2

4

You could try it this way:

\documentclass{article}
\usepackage{enumitem}
\newcommand\labeltext{}
\newcommand\specialitem[1][]{%
 {\renewcommand\labeltext{ (#1)}\item\leavevmode}}
\begin{document}
\begin{enumerate}[label=\arabic*\noexpand\labeltext.]
\item Mary
\specialitem[had] a little 
\item lamb
\end{enumerate}
\end{document}

Remarks:

  1. I'm using a special command but it should also be possible to patch \item but then you would loose the standard behaviour.

  2. The correct alignment of the label is left as an exercise ;-).

Ulrike Fischer
  • 327,261
  • That's clever! Also, it does exactly what I want. Also, exercise solved. Thanks a bunch! =) – Jesko Hüttenhain Mar 09 '13 at 00:08
  • I do not fully understand, what you do, so it might be superfluous, but: Jesko writes in a comment to egreg above, that for the references he wants only the counters without the added text. In enumitem for such cases exists an option ref, here I would suggest ref=\arabic*.. – Speravir Mar 09 '13 at 03:51
  • No no, don't worry, this is precisely what I wanted. I am using cleveref anyway. – Jesko Hüttenhain Mar 09 '13 at 15:25
3

You can define you own custom environment in which you redefine \item macro:

enter image description here

Notes:

  • If you wish to redefine a macro that has optional parameters, you have to use \LetLtxMacro from the letltxmacro package . A detailed description of \LetLtxMacro can be found at this question at When to use \LetLtxMacro?.

  • I have used the xstring package for string comparison as I prefer its syntax, but this can be done without that pacakge if required.

Code:

\documentclass{article}
\usepackage{enumitem}
\usepackage{xstring}
\usepackage{letltxmacro}

\LetLtxMacro{\OldItem}{\item} \newenvironment{MyEnumerate}[1][]{% \renewcommand{\item}[1][]{\OldItem \IfStrEq{##1}{}{}{(##1)}}% \begin{enumerate}[#1]% }{% \end{enumerate}% }%

\begin{document} \begin{MyEnumerate} \item Mary \item[had] a little \item lamb \end{MyEnumerate} \end{document}

Peter Grill
  • 223,288
  • 2
    This might seem like a subtlety, but I would want the argument to appear before the period and after the counter. More precisely, I would like to have greater flexibility in placing the optional argument within the label. – Jesko Hüttenhain Mar 08 '13 at 08:23