# How to use boolean to toggle display of environment that may contain verbatim?

I'd like to create an environment solution that either displays its contents or nothing depending on whether the boolean solutions is set. Currently I'm trying:

\documentclass{article}

\usepackage{ifthenx}
\newboolean{solutions}
\setboolean{solutions}{true}

\usepackage{color}
\newenvironment{solution}%
{%
\ifsolutions%
\color{red}
\subsubsection*{Solution}%
}%
{%
\fi%
}

\begin{document}

Write some code:
\begin{solution}
\begin{verbatim}
a = 1;
a++;
\end{verbatim}
\end{solution}

\end{document}


This works great for \setboolean{solutions}{true} but fails for \setboolean{solutions}{false}. I get an error:

! Incomplete \iffalse; all text was ignored after line 21.
<inserted text>
\fi
<*> foo

?


https://tex.stackexchange.com/a/204450/13600 recommends using the environ package, but this is not a real environment so it fails if with an verbatim environment in the \BODY. In turn, https://tex.stackexchange.com/a/51256/13600 recommends some work arounds for environ like using special macros or external files.

Is there a cleaner way to do what I want?

• You want to have a look at the comment package. – egreg Oct 19 '18 at 15:38
• See also tex.stackexchange.com/questions/159820/4427, particularly the answer by the author of comment. – egreg Oct 19 '18 at 15:40
• I'm a little lost, it seems comment has the same problems with verbatim. – Alec Jacobson Oct 19 '18 at 15:47
• The author said he has fixed the issue. – egreg Oct 19 '18 at 15:47
• I have version 3.8 and still having issues (but maybe I'm not doing exactly what you intend.) – Alec Jacobson Oct 19 '18 at 15:49

When TeX sees \iffalse (maybe during macro expansion), it starts skipping tokens until finding the matching \fi, without any macro expansion. It just takes a count of the (explicit) conditionals it finds, in order to determine the proper nesting.

Thus the \fi hidden in \end{solutions} is never scanned when the boolean is set to false.

This seems to do what you want:

\documentclass{article}

\usepackage{comment}

\usepackage{color}
\newenvironment{solution}
{\color{red}\subsubsection*{Solution}}
{}

%\excludecomment{solution}

\begin{document}

Write some code:
\begin{solution}
á prògrâm
\begin{verbatim}
a = 1;
a++;
\end{verbatim}
\end{solution}

some text following

\end{document}


If you don't want to print the solutions, just uncomment the line

%\excludecomment{solution}


to

\excludecomment{solution}

• This is great. Thanks. Is there a way to tie the exclusion to my boolean solutions ? – Alec Jacobson Oct 19 '18 at 15:54
• @AlecJacobson No. When skipping text in the false branch of a conditional, TeX doesn't expand macros, so it cannot get to see the \fi that only appears when \end{solutions} is expanded. – egreg Oct 19 '18 at 15:58
• OK, I think I can work around that. This is working for verbatim but not if my solution contents contains a includegraphics. What's up with that? – Alec Jacobson Oct 19 '18 at 18:24