# How to end a user-defined environment having conditional start definitions?

For silly example, the following user-defined environment initiates either the itemize environment or the enumerate environment, depending on the optional argument. But if the argument variable cannot be referenced in the environment's closing definition, how does one specify whether it is itemize or enumerate that is to be ended?

\documentclass{article}

\usepackage{ifthen}

\newenvironment{selectlist}[1]{%
\ifthenelse{\equal{#1}{itemize}}
{\begin{itemize}}
{\begin{enumerate}}
}
{\end{???????????}}  %This is the question

\begin{document}

\begin{selectlist}{itemize}
\item Hello world!   %itemized
\end{selectlist}

\begin{selectlist}{enumerate}
\item Hello world!   %enumerated
\end{selectlist}

\end{document}


## 1 Answer

Instead of opening the environment, use the true/false arguments to define a macro:

\documentclass{article}

\usepackage{ifthen}

\newenvironment{selectlist}[1]
{%
\ifthenelse{\equal{#1}{itemize}}
{\def\selectedlist{itemize}}
{\def\selectedlist{enumerate}}%
\begin{\selectedlist}%
}
{\end{\selectedlist}}

\begin{document}

\begin{selectlist}{itemize}
\item Hello world!   %itemized
\end{selectlist}

\begin{selectlist}{enumerate}
\item Hello world!   %enumerated
\end{selectlist}

\end{document}


Of course, \ifthenelse is redundant:

\newenvironment{selectlist}[1]
{\def\selectedlist{#1}\begin{\selectedlist}}
{\end{\selectedlist}}


would do the same. With xparse it's even easier:

\usepackage{xparse}
\NewDocumentEnvironment{selectlist}{m}
{\begin{#1}}
{\end{#1}}


because in \NewDocumentEnvironment the parameters are also available in the “end part”.

• Funny how a solution seems obvious once it's shown to you! Your point on the redundancy of \ifthenelse is also well noted. May I ask what the purpose of {m} is in your xparse solution? – steven_nevets Aug 31 '15 at 22:51
• See the xparse documentation — "m" means one mandatory (curly braces) argument. – Will Robertson Aug 31 '15 at 22:58