
我定義了一個名為 的環境answer
,它顯示我應該準備的測驗問題的答案。
這個想法是編譯一個顯示答案的版本和一個不顯示答案的版本(為了測試學生)。
有沒有一種優雅的方法來禁用環境?
當然可以定義一個指令\ifanswer
然後寫\ifanswer{\begin{answer}foo\end{answer}}
。對於每個環境。然而這種方法的問題是,每次都需要記住並插入這個命令,當然一次忘記這個命令會很尷尬。
微量元素:
\documentclass{article}
\newenvironment{answer}{\textit{Answer} }{\textit{the end.}}
\begin{document}
Question: Bla
\begin{answer}
Foo
\end{answer}
\end{document}
答案1
正如大衛所說,你可以使用comment
包:
\documentclass{article}
\usepackage{comment}
\newenvironment{answer}{\textit{Answer} }{\textit{the end.}}
\includecomment{answer} %%% comment out to show the answer
\begin{document}
Question: Bla
\begin{answer}
Foo answer
\end{answer}
\end{document}
如果你想用困難的方式做到這一點 -
\documentclass{article}
\usepackage{environ}
\usepackage{ifthen}
\newboolean{answers}
\setboolean{answers}{true} %%% uncomment to show answers properly
%\setboolean{answers}{false} %%% comment to show answers properly
\ifthenelse{\boolean{answers}}%
{%
\NewEnviron{answer}
{%
\textit{Answer}
\BODY{}
\textit{the end.}
}%
}%
{\NewEnviron{answer}
{%
\textit{Answer}
\textit{the end.}
}%
}%
\begin{document}
Question: Bla
\begin{answer}
Foo answer
\end{answer}
\end{document}