# Conditionals used in defining of new environment

I want to produce two versions of one document with different content. The idea is to comment out certain sections if a logical variable is false. This logical variable will be inputted eventually through an argument to a package. My difficulty is that my code either does not work or it ignores boolean variable. Here is sample code which does not work. Error is "missing number treated as zero"

\usepackage{ifthen,comment}
\newboolean{iflecturer}
\newenvironment{myitemize}
{\ifthenelse{iflecturer}
{\begin{itemize}}{\begin{comment}}
}
{\ifthenelse{iflecturer}
{\end{itemize}}{\end{comment}}
}
\begin{document}
\setboolean{iflecturer}{true}% \setboolean{iflecturer}{false}
\begin{myitemize}
\item Monday
\item Tuesday
\item Wednesday
\end{myitemize}
\end{document}

• the problem is that the \begin{comment} begins a comment in the definition, it is not seen as then part of the if then. – Guido Oct 3 '14 at 8:07
• \ifthenelse{\boolean{lecturer}}{true}{false}; but \begin{comment} cannot be used in the definition of an environment anyway. – egreg Oct 3 '14 at 8:45

I think it's better to use the \excludecomment macro provided by the comment package. This way you don't need to start and end comment environments from inside macros or environments of your own.

\documentclass{article}
\usepackage{ifthen}
\usepackage{comment}
\newboolean{iflecturer}
\setboolean{iflecturer}{false}

\ifthenelse{\boolean{iflecturer}}{}{\excludecomment{solution}}

\begin{document}
This is a question.

\begin{solution}
And this is the solution.
\end{solution}

\end{document}

• What if I want to customize the solution environment? For example setting the color to red? – Alec Jacobson Oct 19 '18 at 14:57
• @AlecJacobson --- take a look at section 3 (Special comments) in the comment package documentation. – Ian Thompson Oct 20 '18 at 8:58

You can use the environ package instead of comment to discard the content of an environment.

\documentclass{article}

\usepackage{etoolbox}
\usepackage{environ}
\newtoggle{lecture}

\NewEnviron{myitemize}{%
\iftoggle{lecture}
{\begin{itemize}\BODY\end{itemize}}
{}%
}

\begin{document}
\toggletrue{lecture}
The environment is visible
\begin{myitemize}
\item test
\item test
\end{myitemize}
\togglefalse{lecture}
after this the environment is hidden.
\begin{myitemize}
\item test
\item test
\end{myitemize}
Test
\end{document}


This is what you get:

• Many thanks. I learned from all answers. David – David Reynolds Oct 4 '14 at 15:26
• @DavidReynolds glad they helped. The best to thank is to upvote and to accept the answer it helped you the most. To ‘Accept’ an answer click on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). – Guido Oct 4 '14 at 18:32