0

On How to create a enumerate list adding a custom prefix before each item number? I learned how to create a custom environment to create labels. But now, it is missing the enumitem shortlabels feature:

By taking the original example, I managed to pass the delimiter type to the nested environment list: enter image description here

\documentclass{scrbook}

\usepackage{enumitem}
\usepackage{xparse}
\newcounter{enumerateoptionalcount}

\NewDocumentEnvironment{enumerateoptional}{O{)}}{%
    \setcounter{enumerateoptionalcount}{0}%
    \renewcommand*\descriptionlabel[1]{%
      \stepcounter{enumerateoptionalcount}%
      \normalfont\bfseries ##1~\arabic{enumerateoptionalcount}#1%
    }%
    \description%
  }%
  {\enddescription}

\begin{document}

\begin{enumerateoptional}[)]
   \item[Some first] item one
   \item[Some second] item two
   \item[Some third] item three
   \item[Some fourth] item four
   \item[Some Fifth] item five
\end{enumerateoptional}

\end{document}

But how can I also pass the number type as enumerate environment has, i.e.,

  1. If I call \begin{enumdescript}[1)], then, the number are 1) ..., 2) ...
  2. If I call \begin{enumdescript}[i)], then, the number are i) ..., ii) ...
  3. If I call \begin{enumdescript}[I)], then, the number are I) ..., II) ...
  4. ...

I tried to look at the enumitem package source-code, but I cannot understand anything about it. For example, \ifx\enit@b\enit@c\else ... \enit@elt{##1}{##2}, what is enit? What does elt mean? What are enit@a, enit@b, enit@c doing?

\newcommand\SetEnumerateShortLabel[2]{%
  \let\enit@a\@empty
  \def\enit@elt##1##2{%
    \def\enit@b{#1}\def\enit@c{##1}%
    \ifx\enit@b\enit@c\else
      \expandafter\def\expandafter\enit@a\expandafter{%
        \enit@a
        \enit@elt{##1}{##2}}%
    \fi}%
  \enit@marklist
  \expandafter\def\expandafter\enit@a\expandafter{%
    \enit@a
    \enit@elt{#1}{#2}}%
  \let\enit@marklist\enit@a}

\SetEnumerateShortLabel{a}{\alph*}
\SetEnumerateShortLabel{A}{\Alph*}
\SetEnumerateShortLabel{i}{\roman*}
\SetEnumerateShortLabel{I}{\Roman*}
\SetEnumerateShortLabel{1}{\arabic*}
  • 2
    Why do you reimplement the enumerate environment instead of using the one provided by enumitem? You could just pass the optional parameter to it then. – siracusa Oct 16 '19 at 6:57
  • Because I did not know I could use it that way. – user Oct 16 '19 at 15:21
2

You can simplify the code in the OP by using tools from the enumitem package to create a customised enumerate environment. If you do this then you can change the labels in the enumerate in standard way provided by the enumitem package:

\begin{enumerateoptional}[label=\alph*]
   \item[Some first] item one
   \item[Some second] item two
   \item[Some third] item three
   \item[Some fourth] item four
   \item[Some Fifth] item five
\end{enumerateoptional}

to produce:

enter image description here

whereas by default, when you omit the [label=\alph*], you get:

enter image description here

The trick is to redefine the \item command to do what you want. To do this I make a "copy" of the real item command and then define a custom \optionalItem that gives your format. Then in the specifications for the custom enumerate environment you add before=\let\item\optionalItem to use the custom item command.

Here is the full code:

\documentclass{scrbook}

\usepackage{enumitem}
\newlist{enumerateoptional}{enumerate}{1}
\setlist[enumerateoptional]{
    before=\let\item\optionalItem,
    label=\arabic*,
    nosep,
    labelindent=20mm,
    leftmargin=*
}
\let\realItem\item

\newcommand\optionalItem[1][]{%
  \refstepcounter{enumerateoptionali}% increment the counter
  \realItem[\bfseries#1~\theenumerateoptionali)]%
}

\begin{document}

\begin{enumerateoptional}
   \item[Some first] item one
   \item[Some second] item two
   \item[Some third] item three
   \item[Some fourth] item four
   \item[Some Fifth] item five
\end{enumerateoptional}

\begin{enumerateoptional}[label=\alph*]
   \item[Some first] item one
   \item[Some second] item two
   \item[Some third] item three
   \item[Some fourth] item four
   \item[Some Fifth] item five
\end{enumerateoptional}

\end{document}
| improve this answer | |

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.