3

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
\newenvironment{answer}{}{}

% set conditional behavior of environment
\ifbool{hidetrofficlight}{\AtBeginEnvironment{answer}{\comment}%
\AtEndEnvironment{answer}{\endcomment}}{}

% macro for the answer using the boolean environment
\newcommand{\BA}[1]{\begin{answer} 
    \subsection*{Answer}#1.
                     \end{answer}}

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

    \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: enter image description here enter image description here

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

2

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 for the answer
\newcommand\BA[1]{\subsection*{Answer}#1\par}

% macro putting the question and answer together
\newcommand{\QA}[2]{%
    \subsection{Question: \vskip.1in \noindent\normalsize #1?}
    \addtocontents{toc}{\protect\vskip5pt}
    \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
1

First Answer

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}

\newif\ifanswer
%%%%%%% this is the toggle %%%%%%5 
\answerfalse
%%%%%%%%%% \answertrue will show the Answers
\ifanswer
\def\BA#1{\subsection*{Answer}#1.}
\else
\def\BA#1{}
\fi

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

\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}

Second Answer

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"
%%%%it reveals the answers

% new environment
\newenvironment{answer}{}{}

% set conditional behavior of environment
\ifbool{hidetrofficlight}{\AtBeginEnvironment{answer}{\nullfont}%
\AtEndEnvironment{answer}{\normalfont}}{}

% macro for the answer using the boolean environment
\newcommand{\BA}[1]{\begin{answer} 
    \subsection*{Answer}#1.
\end{answer}}

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

\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: enter image description here If \setbool{hidetrofficlight}{true} than this is the result: enter image description here

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.