# How to implement a verbatim or null environment using a boolean within \newcommand

I am trying to use a boolean environment within a \newcommand statement to selectively implement a verbatim environment. When I set the boolean to true it should put the "Answer" within the verbatim environment, effectively making it disappear. Instead, it spits out the error !File ended while scanning use of \next. Related, the boolean code from this answer.

\documentclass {article}
\usepackage {fontspec}
\setmainfont {TeX Gyre Schola}
\usepackage{etoolbox}   % for booleans
\usepackage{verbatim}   % for the comment environment

% setup a new boolean
\newbool{hidetrofficlight}
\setbool{hidetrofficlight}{true}
%%%%If the boolean is set to false
%%%%the verbatim environment is not implemented
%%%%and this code compiles fine, when set to "true"
%%%%it spits out the error "!File ended while scanning use of \next

% new environment

% set conditional behavior of environment

% macro for the answer using the boolean environment

% macro putting the question and answer together
\newcommand{\QA}[2]{%
\subsection{Question: \vskip.1in \noindent\normalsize #1?}
\BA{#2}

\begin{document}

\hsize6in
\hoffset-.5in
\noindent
\tableofcontents\eject
\centerline{\huge{Questions for Jonathan Dough}}

\QA{Did the donuts really need to be made}{Yes. Because the donuts needed to be eaten, we had to make them}

\end{document}


Double lualatex compiling with the boolean set to false gives the desired results:

But when compiled with a true boolean, instead of commenting out the "Answer" portion, it throws the error.

The issue with your original code is that (i) the answer environment occurs inside a macro (ii) the answer environment, in turn, invokes a verbatim-like environment. (The comment environment is a special verbatim environment.) Why would this matter? While processing your code, LaTeX correctly inserts \comment at the start of an answer environment. LaTeX then looks ahead for \endcomment. However, as it is in verbatim mode, it doesn't realize that \end{answer} should trigger the insertion of an \endcomment instruction. LaTeX thus keeps on looking for \endcomment -- until it hits the end of the file.

The following answer works by modifying the \QA macro. It tests if hidetrafficlight is true or false; if -- and only if -- it's "false", \BA is executed next. Note that an answer environment no longer needs to be defined.

\documentclass {article}
\usepackage {fontspec}
\setmainfont {TeX Gyre Schola}

% set up a new boolean
\usepackage{etoolbox}   % for booleans
\newbool{hidetrafficlight}
\setbool{hidetrafficlight}{true}

% macro putting the question and answer together
\newcommand{\QA}[2]{%
\subsection{Question: \vskip.1in \noindent\normalsize #1?}
\ifbool{hidetrafficlight}{}{\BA{#2}}}

\begin{document}

\setbool{hidetrafficlight}{false} % or: "true"

\hsize6in
\hoffset-.5in
\noindent
\tableofcontents
\clearpage

\centerline{\huge{Questions for Jonathan Dough}}

\QA{Did the donuts really need to be made}{Yes. Because the donuts needed to be eaten, we had to make them}

\end{document}

• Awesome answer! Great explanation of the programming issues. – A Feldman Apr 8 '16 at 13:36

This is kind of a plain texish answer, that does not really solve all of the problems raised by the question regarding wrapping verbatim commands within an environment (I don't know if they can be solved), but sidesteps them, and it works. The toggle for "show answer" is \answertrue and the toggle for "do not show the answer" is \answerfalse. I would assume that if I used \newcommand the result would be similar.

\documentclass {article}
\usepackage {fontspec}
\setmainfont {TeX Gyre Schola}

%%%%%%% this is the toggle %%%%%%5
\else
\def\BA#1{}
\fi

\def\QA#1#2{%
\subsection{Question: \vskip.1in \noindent\normalsize #1?}
\BA{#2}

\begin{document}

\hsize6in
\hoffset-.5in
\noindent
\tableofcontents\eject

\centerline{\huge{Questions for Jonathan Dough}}

\QA{Did the donuts really need to be made}{Yes. Because the donuts needed to be eaten, we had to make them}

\end{document}


Using the original code, but substituting \nullfont for \comment in the answer environment allows it to work as follows:

\documentclass{article}
\usepackage{etoolbox}   % for booleans
\usepackage{verbatim}   % for the comment environment

% setup a new boolean
\newbool{hidetrofficlight}
\setbool{hidetrofficlight}{true}
%%%%If the boolean is set to false
%%%%the \nullfont environment is not implemented
%%%%and this code compiles fine, when set to "true"

% new environment

% set conditional behavior of environment

% macro for the answer using the boolean environment

% macro putting the question and answer together
\newcommand{\QA}[2]{%
\subsection{Question: \vskip.1in \noindent\normalsize #1?}
\BA{#2}

\begin{document}

\hsize6in
\hoffset-.5in
\noindent
\tableofcontents\eject
\centerline{\huge{Questions for Jonathan Dough}}

\QA{Did the donuts really need to be made}%
{Yes. Because the donuts needed to be eaten, we had to make them}%
And something typed outside the effect of the{\ttfamily$\backslash$QA} command.

\QA{Is it really time to make the donuts}%
{Not making the donuts cannot be put off for any reason whatsoever}

\end{document}


If \setbool{hidetrofficlight}{false} than this is the result: If \setbool{hidetrofficlight}{true} than this is the result:

• If anybody knows why the \subsection*{Answer} ends up printing when the boolean is set to true in my second example, I was wondering about that. – A Feldman Apr 9 '16 at 5:21