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

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

我正在尝试在 \newcommand 语句中使用布尔环境来有选择地实现逐字环境。当我将布尔值设置为时,true它应该将“答案”放在verbatim环境中,从而有效地使其消失。相反,它会吐出错误!File ended while scanning use of \next。相关的布尔代码来自这个答案

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

使用布尔值设置进行双重 lualatex 编译可得到false所需的结果: enter image description here enter image description here

但是当使用true布尔值进行编译时,它不会注释掉“答案”部分,而是抛出错误。

答案1

您的原始代码的问题是 (i)answer环境出现在宏内 (ii) 环境answer反过来调用verbatim类似 - 的环境。(comment环境是一个特殊verbatim环境。)为什么这很重要?在处理您的代码时,LaTeX 正确地\comment在环境的开头插入answer。然后 LaTeX 向前查找\endcomment。但是,由于它处于逐字模式,它没有意识到应该\end{answer}触发插入\endcomment指令。因此,LaTeX 继续寻找\endcomment-- ,直到到达文件末尾。

以下答案通过修改\QA宏来实现。它测试hidetrafficlight是真还是假;如果(且仅当)它是“假”,则\BA接下来执行。请注意,answer不再需要定义环境。

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

答案2

第一个答案

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

相关内容