# How to compare number from \newenvironment argument

I am trying to create a new environment as follows

\newenvironment{letters}[1]{%
\vspace{-3mm}
\ifnum #1=1%
\else
\begin{multicols}{#1}
\fi
\begin{enumerate}[label=\textbf{(\alph*)}]
}{%
\end{enumerate}
\ifnum #1=1%
\else
\end{multicols}
\fi
\vspace{-5mm}
}


My question is: Why doesn't it work?!

Note: this is not a duplicate from this post, since I don't want to do the \RenewDocumentEnvironment. Instead, I want to create a new environment.

If you look at the log you'll see that the first thing TeX complains about is:

! Illegal parameter number in definition of \endletters.
1
l.22     }


That's because you tried to use #1 in the \end part of the environment, but you can't use the arguments passed to the \begin part in the \end part of an environment.

If you were to do that, then the usage of the environment would have to be something like this:

\begin{letters}{1}
\item stuff
\end{letters}{1}


passing the argument to \end{letters} too.

To do what you want you need to devise another way to pass that information to the \end part. For instance, with a \newif:

\documentclass{article}
\usepackage{enumitem}
\usepackage{multicol}

\newif\ifLettersMulticol
\newenvironment{letters}[1]{%
\vspace{-3mm}
\ifnum #1=1
\else
\LettersMulticoltrue
\begin{multicols}{#1}
\fi
\begin{enumerate}[label=\textbf{(\alph*)}]
}{%
\end{enumerate}
\ifLettersMulticol
\end{multicols}
\fi
\vspace{-5mm}
}

\begin{document}

\begin{letters}{1}
\item  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
\item  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
\item  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
\item  consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
\item  cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
\item  proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\end{letters}

\begin{letters}{2}
\item  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
\item  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
\item  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
\item  consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
\item  cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
\item  proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\end{letters}

\end{document}


Also, as it's been noted in the comments, it's not a good idea to add a % at the end of the line in \ifnum tests (and a couple other cases too). When TeX is scanning the numbers in the \ifnum test, it is expanding tokens, and will continue to do so until something that is not a number is found. For instance, the following (perhaps idiotic, but serves as demonstration) code

\ifnum1=0%
\ifnum2=2
1%
\else
2%
\fi
a%
\else
b%
\fi


produces a, instead of b, as someone that took a quick glance at the \ifnum1=0 would guess. If, however, you remove the very first %, the output is b.

Also, a missing % at the end of a \ifnum#1=1 test will not appear in the output, because TeX's number scanning mechanism says that a number can be followed by an optional space, so this one is safe. To be sure, of course, one can end the scanning of the \ifnum test with \relax: \ifnum#1=1\relax.

In this particular case, the % wouldn't casuse troubles, but it's probably better to be on the safe side.

• maybe stress that you correctly removed the 1% and replaced them by 1<end of line space> else the \ifnum test won't work correctly (except if one gets lucky).
– user4686
Nov 14, 2018 at 21:26
• @jfbu In this case one is lucky, because \else just follows 1 and so TeX knows it has to stop evaluating the number or the conditional would go wrong. Of course a proper constant termination is much better. Nov 14, 2018 at 21:45
• @jfbu Thanks for the reminder. I forgot I changed that :-) Nov 14, 2018 at 23:10
• @egreg I agree. The \pagenumbering{gobble} was there because I forgot to remove it from another code, sorry for the noise. Thanks :-) Nov 14, 2018 at 23:12
• +1 for the added explanation about \ifnum and termination of numbers. \relax is not an option if you want to maintain expandability, but in 99% of user cases, that is not the goal, so \relax. You can also use \space. But the most general is \numexpr#1\relax because it allows for #1 both explicit digit tokens or a TeX count variable. (I think vaguely that @egreg had a non e-TeX trick somewhere recently but I forgot where).
– user4686
Nov 15, 2018 at 8:04