3

In the following code, I write two macros for \begin{enumerate}...\end{enumerate}. In trying to get more than one optional argument, I used the method in More than one optional argument for newcommand. However, when I add an extra linebreak between \items, the \NewDocumentCommand macro fails while the original \begin{enumerate}...\end{enumerate} is fine, and \newcommand macro is also fine.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

% if do enumitem before enumerate, cause error????? https://tex.stackexchange.com/questions/238691/enumitem-always-throwing-errors \usepackage{enumerate} \usepackage[shortlabels]{enumitem}

\newcommand{\myenumerate}[2][4]{\begin{enumerate}[(a), leftmargin=0.2in,parsep=0pt,itemsep=#1pt] #2 \end{enumerate}}

\NewDocumentCommand{\myenumeratebad}{ O{10} O{a} m }{\begin{enumerate}[(#2),leftmargin=0.3in,parsep=0pt,itemsep=#1pt,topsep=8pt] #3 \end{enumerate}}

\begin{document}

\begin{enumerate}[(i),leftmargin=0.3in,parsep=0pt,itemsep=4pt,topsep=8pt] \item awef

\item awef \end{enumerate}

\myenumerate[10]{ \item awe

\item awf }

\myenumeratebad[10][i]{ \item awe \item awf }

\myenumeratebad[10][i]{ \item awe \item awf }

\end{document}

P.S. in making my MWE, there was an issue putting enumitem before enumerate, but not the other way around. I only learned about this from a comment here Enumitem always throwing errors.

Qrrbrbirlbel
  • 119,821
D.R
  • 771
  • 1
    you can not use enumerate and enumiten at the same time you are using enumitem here so delete the enumerate package – David Carlisle Oct 05 '22 at 06:58
  • @DavidCarlisle I think if I delete the enumerate package I lose the ability to write [(a)] or [(i)] to specify what type of bullet points I want – D.R Oct 05 '22 at 15:53
  • 1
    No you do not: you are not using enumerate at all. If you have two packages one does \def\qqq{a} the other does \def\qqq{b} then you can not load both packages and have \qqq magically be a and b at the same time. You load enumitem second so it wins and loading enumerate first just makes things slower. – David Carlisle Oct 05 '22 at 15:58
  • @DavidCarlisle ok I see, thank you for the clarification. – D.R Oct 05 '22 at 18:29

1 Answers1

4

By default \newcommand creates a macro that is defined long, i.e. accepting paragraph parameters, i.e. an empty line. (A short macro uses \newcommand*.)

With \NewDocumentCommand all paramaters are by default short – unless the argment is prefixed by a +. (Yes, you can specify the length on an “argument-by-argument basis”, page 3 of the xparse manual.)

So, you're going to need

\NewDocumentCommand{\myenumeratebad}{ O{10} O{a} +m }{%
  \begin{enumerate}[(#2),leftmargin=0.3in,parsep=0pt,itemsep=#1pt,topsep=8pt]
  #3
  \end{enumerate}}

Though, you might just define your own environment again:

\NewDocumentEnvironment{myenumerateenv}{O{10} O{a}}{%
  \begin{enumerate}[(#2),leftmargin=0.3in,parsep=0pt,itemsep=#1pt,topsep=8pt]}
  {\end{enumerate}}

which you can use with or without empty lines.

\begin{myenumerateenv}[10][i]
\item awe

\item awf \end{myenumerateenv}

Qrrbrbirlbel
  • 119,821