3

I have multiple repeated

\begin{enumerate}[label=A\alph*), ref=A\alph*]

and I want to reduce code duplication. I would also like to avoid creating new environments if possible. I just want a simple way (if there is one) to do something like this:

\newcommand{\enumopts}[1]{label={#1}\alph*), ref={#1}\alph*}
\begin{enumerate}[\enumopts{A}]

The example above is not working. If it is relevant, I am referring to the enumitem package.

I should note that the following works:

\newcommand{\elabel}[1]{{#1}\alph*)}
\newcommand{\eref}[1]{{#1}\alph*}
\begin{enumerate}[label=\elabel, ref=\eref]

How can I do it?

2
  • 1
    In this specific case, setlist from enumitem package → tex.stackexchange.com/questions/313463/…. in the general case, you need to learn some TeX programming.
    – user202729
    Nov 29, 2023 at 11:44
  • If enumitem was using expkv as the key=value implementation you could use its expansion notation. You could even write a wrapper using expkv to have arbitrary key lists with expansion notation, but I guess simply defining a new key for enumitem as done in the accepted answer is easier and way more straight forward... :)
    – Skillmon
    Nov 29, 2023 at 16:05

2 Answers 2

10

You can define new keys:

\documentclass{article}
\usepackage{enumitem}
\SetEnumitemKey{alph}{label=#1\alph*), ref=#1\alph*}
\begin{document}
\begin{enumerate}[alph=A]
\item abc\label{test}
\end{enumerate}

\ref{test}

\begin{enumerate}[alph=B]
\item abc
\end{enumerate}

\end{document}

enter image description here

0

Though I strongly recommend the answer provided by @UlrikeFischer for the problem at hand, for the more general question on how to expand a macro inside of a key list the following defines such a preprocessor using expkv.

This allows to use expkv's expansion notation to specify precise expansions of single list elements.

In the following example I use these expansion rules:

  • o\r expand the first token once (o) and reinsert the result as key=value input (\r)
  • R expand the single token once (or if it's a register, expand to the register's contents), then reinsert the result as key=value input (not usable for the macro taking argument \enumopts).
\documentclass{article}

\usepackage{expkv}

\makeatletter
\newcommand\kvpreproc[2]
  {%
    \expanded
      {\unexpanded{#1}{\expanded{\ekvparse\kvpreproc@k\kvpreproc@kv{#2}}}}%
  }
\newcommand\kvpreproc@k[1]{, {\unexpanded{#1}}}
\newcommand\kvpreproc@kv[2]{, {\unexpanded{#1}}= {\unexpanded{#2}}}
\makeatother

\usepackage{enumitem}
\newcommand{\enumopts}[1]{label={#1}\alph*), ref={#1}\alph*}
\newcommand\moreopts{labelsep=1em}

\begin{document}
\kvpreproc{\begin{enumerate}[}{nosep, o\r: \enumopts{A}, R: \moreopts}]
  \item foo
  \item bar
  \item baz
\end{enumerate}

\kvpreproc{\begin{enumerate}[}{nosep, o\r: \enumopts{A}}]
  \item foo
  \item bar
  \item baz
\end{enumerate}
\end{document}

enter image description here

You must log in to answer this question.

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