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?

5
  • 3
    A simple solution would be \item (had) a little, but perhaps that is not an option. Commented Mar 8, 2013 at 8:09
  • What about references? How should item 2 be referenced? With 2 or with 2(had)?
    – egreg
    Commented Mar 8, 2013 at 8:34
  • Just with 2, as in, with the usual counter value. Commented Mar 8, 2013 at 8:51
  • It would be interesting to know why you don't just do it as Peter Grill suggests. Could you please add that to the question? Thanks! Commented Mar 8, 2013 at 13:37
  • It's basically in the comment to Peters answer, but I put it in the original question as well. Commented Mar 9, 2013 at 0:10

2 Answers 2

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 ;-).

3
  • That's clever! Also, it does exactly what I want. Also, exercise solved. Thanks a bunch! =) Commented Mar 9, 2013 at 0: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
    Commented Mar 9, 2013 at 3:51
  • No no, don't worry, this is precisely what I wanted. I am using cleveref anyway. Commented Mar 9, 2013 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}
1
  • 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. Commented Mar 8, 2013 at 8:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .