当我写练习清单时,我通常会在陈述之后写上解决方案,然后为了打印或不打印解决方案,我在序言中包含了以下内容。
\newif\ifsolns\solnstrue %include solutions
%\newif\ifsolns\solnsfalse %do not include solutions
我希望解决方案以蓝色显示,因此我包含了“颜色”包,我所做的是,
\ifsolns{\color{blue}
solution goes here
}\fi
我希望自动执行此操作,而不是每次解决方案启动时手动包含 \color{blue}。
谢谢
答案1
我建议在环境中编写解决方案,然后可以打开/关闭。这样做的原因是,您可以访问整个解决方案文本,并可以根据需要对其进行操作(例如添加颜色),包括对位置进行调节:
\documentclass{article}
\usepackage{environ,xcolor}
\newif\ifsolution
\solutiontrue % Print solutions
%\solutionfalse % Do not print solutions (default)
\NewEnviron{solution}{%
\ifsolution
\color{blue}\BODY
\fi
}
\begin{document}
\begin{enumerate}
\item
This is a problem that needs a solution.
\begin{solution}
This is the solution to the first problem.
\end{solution}
\item
This is another problem that doesn't require a solution.
\item
Here is a final problem requiring some sort of solution.
\begin{solution}
This is the solution to the third problem.
\end{solution}
\end{enumerate}
\end{document}
使用\solutionfalse
(默认):
和\solutiontrue
: