Tx · LaTeX & TikZ

How to Wrap an enumitem List in a LaTeX Macro

Published By the 9to5Science editorial team

At a glance

  1. 1. Use an environment

    Define a new environment to keep the enumerate list’s beginning and end together.

  2. 2. Pass list options

    Give the environment an optional argument and forward it to enumitem.

  3. 3. Set a default

    Choose default label settings for lists where you do not supply options.

  4. 4. Override when needed

    Pass different enumitem settings in brackets for a particular list.

If you want to reuse an enumitem-formatted enumerate list, define a LaTeX environment that forwards its optional argument to enumerate. This is usually a better fit than a command: an environment can group the list’s opening and closing commands in one definition, while still allowing different formatting options each time you use it.

Define a reusable enumerate environment

Load enumitem, then use \newenvironment with one optional argument. The following complete example sets a numeric label by default and allows a list to override that label:

\documentclass{article}
\usepackage{enumitem}

\newenvironment{myenum}[1][label=\arabic*.]
  {\begin{enumerate}[#1]}
  {\end{enumerate}}

\begin{document}

\begin{myenum}
  \item This list uses the default numeric label.
  \item Its labels are 1. and 2.
\end{myenum}

\begin{myenum}[label=\alph*)]
  \item This list uses lowercase letters.
  \item Its labels are b) after the first item.
\end{myenum}

\end{document}

The #1 in the definition represents the environment’s optional argument. It is inserted into the option list for enumerate. In \newenvironment{myenum}[1][label=\arabic*.], the [1] says that the environment takes one argument, and the final bracketed expression supplies its default. Thus \begin{myenum} uses label=\arabic*., while \begin{myenum}[label=\alph*)] passes a different label setting to enumitem.

The starred forms \arabic* and \alph* are provided by enumitem for labels that change with the item number. The punctuation after the starred command is part of the label: \arabic*. produces labels such as 1., and \alph*) produces labels such as a).

Pass more than one option

The optional argument can contain multiple comma-separated enumitem settings, just as it would when used directly with enumerate:

\begin{myenum}[label=\Roman*., leftmargin=*, itemsep=0pt]
  \item First item.
  \item Second item.
\end{myenum}

Here the labels use uppercase Roman numerals, leftmargin=* lets enumitem calculate the left margin from the label, and itemsep=0pt removes extra space between items. The optional-argument default in the \newenvironment definition is used only when you omit the argument; an explicit argument replaces it entirely rather than merging with it. For example, with the definition above, \begin{myenum}[itemsep=0pt] does not also retain the default label=\arabic*.. If you need persistent defaults that per-list options can override, set them separately with enumitem, for example \setlist[enumerate]{label=\arabic*.}.

You can also omit the default argument in the definition if every use should specify its settings:

\newenvironment{myenum}[1]
  {\begin{enumerate}[#1]}
  {\end{enumerate}}

Then start each list with options, for example \begin{myenum}[label=\alph*)].

Why use an environment rather than a command?

A command can be useful for producing a short piece of text or for setting up a list, but an enumerate list has both a beginning and an end. An environment expresses that structure directly: its begin code starts the list, and its end code closes it. You then write matching \begin{myenum} and \end{myenum} statements around the items.

Avoid defining a command that opens enumerate and then relying on a separate, unrelated command to close it. Such pairs are easier to mismatch, and they make the document structure less clear. If the purpose is to customize an entire list, a new environment is the natural wrapper.

If you do not need a reusable name or default settings, use enumitem directly:

\begin{enumerate}[label=\alph*)]
  \item First item.
  \item Second item.
\end{enumerate}

Common mistakes

  • Forgetting to load enumitem: Options such as label=\alph*) are supplied by that package. Add \usepackage{enumitem} in the preamble.
  • Using the wrong argument syntax: The default belongs in the definition, as in \newenvironment{myenum}[1][label=\arabic*.]. The per-list options go after the environment name: \begin{myenum}[label=\alph*)].
  • Leaving out #1: If the begin code does not include [#1], the options you pass when using the environment will not reach enumerate.
  • Mismatching the environment names: Close a custom myenum environment with \end{myenum}, not \end{enumerate}. The wrapper’s end code handles closing the underlying list.
  • Treating the label as plain text: The * in \arabic* or \alph* is meaningful to enumitem; it stands for the current item counter. Use the starred form in the label setting when you want labels to update for each item.

For further label and spacing options, see the enumitem package documentation.

References

  1. enumitem: Customize lists

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.