根据布尔值选择源代码

根据布尔值选择源代码

有没有办法根据布尔值选择源代码,而不仅仅是选择要显示的文本。

\documentclass{exam}
\usepackage{ifthen}
\newif\ifanswers
\answerstrue
\begin{document}
\begin{questions}
    \question This is the first question \\
        {\ifanswers 
            The first answer includes a graph. \\
            \begin{tikzpicture}
                \draw circle (1);
            \end{tikzpicture}
        }
    \question This is the second question \\
        {\ifanswers 
            This is the second answer is just words.
        }
\end{questions}
\end{document}

答案1

\if\fi即使不需要\else分支也必须以此结尾:

\documentclass{exam}
\usepackage{tikz}
\newif\ifanswers
\answerstrue
\begin{document}
\begin{questions}
    \question This is the first question \\
        {\ifanswers 
            The first answer includes a graph. \\
            \begin{tikzpicture}
                \draw circle (1);
            \end{tikzpicture}\fi
        }
    \question This is the second question \\
        {\ifanswers 
            This is the second answer is just words.\fi
        }
\end{questions}
\end{document}

请注意,此代码不需要ifthen,但它确实依赖于tikz。另外,以 结尾行通常不是一个好主意\\。但是,我exam对该类不够了解,无法判断是否questions是可以接受这种情况的环境,因此我没有在上面的代码中对其进行更改。

答案

相关内容