我正在制作一个问题/答案页面,答案紧跟问题。问题被列举出来,而答案则没有。问题使用一种颜色,而答案使用另一种颜色。
为了暂停计数,我使用了提供的解决方案来这问题。因此,我的代码通常如下所示:
\documentclass[english]{article}
...
\newcounter{savedenum}
\newcommand*{\saveenum}{\setcounter{savedenum}{\theenumi}}
\newcommand*{\resume}{\setcounter{enumi}{\thesavedenum}}
...
\usepackage{parskip}
\usepackage{color}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{enumitem}
...
\begin{document}
...
\begin{enumerate}
\item \color{black}{Question text goes here.}
\saveenum
\end{enumerate}
\begin{itemize}
\item[] \color{NavyBlue}{Answer text goes here.}
\end{itemize}
\begin{enumerate}
\resume
\item \color{black}{Question text goes here.}
\saveenum
\end{enumerate}
\begin{itemize}
\item[] \color{NavyBlue}{Answer text goes here.}
\end{itemize}
...
\end{document}
由于有大量的代码重用,我正在寻找一个更好的解决方案(也许是某种循环?),如果可能的话,它可以替代块的复制和粘贴\begin{...} ... \end{...}
。
答案1
我建议采取一种不同的方法:
\documentclass{article}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\newif\ifsolutions
\solutionstrue
\newenvironment{exercises}
{\begin{enumerate}}
{\end{enumerate}}
\newenvironment{question}
{\item}
{}
\ifsolutions
\newenvironment{solution}
{\par\nopagebreak\begingroup\color{NavyBlue}}
{\endgroup}
\else
\usepackage{comment}
\excludecomment{solution}
\fi
\begin{document}
\begin{exercises}
\begin{question}
Question one text goes here.
\end{question}
\begin{solution}
Answer one text goes here.
\end{solution}
\begin{question}
Question two text goes here.
\end{question}
\begin{solution}
Answer two text goes here.
\end{solution}
\end{exercises}
\end{document}
标记可能看起来过多,但它提供了更大的灵活性:您可以exercises
使用enumitem
功能进行自定义,也可以question
使用和solution
。我添加了一个可能性:如果您评论
\solutionstrue
行,则根本不会打印解决方案。
解决方案活跃
解决方案被抑制
答案2
由于您已经加载enumitem
包,下面是使用该resume
功能的解决方案:
\documentclass{article}
\usepackage{enumitem}
\usepackage{parskip}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\newenvironment{question}{\enumerate[resume]\item}{\endenumerate}
\newenvironment{solution}{\itemize\item[]\begingroup\color{NavyBlue}}{\endgroup\enditemize}
\begin{document}
\begin{question}
Question text goes here.
\end{question}
\begin{solution}
Answer text goes here.
\end{solution}
\begin{question}
Question text goes here.
\end{question}
\begin{solution}
Answer text goes here.
\end{solution}
\end{document}
如果你希望拥有自己的环境list
(也许你正在使用enumerate
此任务之外的环境),那么你可以使用newlist
\newlist{myenumerate}{enumerate}{5}
\setlist[myenumerate]{label=\arabic*.,resume}
\newenvironment{question}{\myenumerate\item}{\endmyenumerate}
最后,如果你想要抑制环境solution
,或者将其输出到单独的文件,我建议你查看answers
包;您可以answers
使用以下方式在文档的主要部分打开和关闭
% solutions written to file (NOT to main part of document)
\usepackage{answers}
% solutions NOT written to file (written to main part of document)
\usepackage[nosolutionfiles]{answers}
完成 MWE
\documentclass{article}
\usepackage{enumitem}
\usepackage{parskip}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{answers}
%\usepackage[nosolutionfiles]{answers}
\newlist{myenumerate}{enumerate}{5}
\setlist[myenumerate]{label=\arabic*.,resume}
\newenvironment{question}{\myenumerate\item}{\endmyenumerate}
% open the answer file
\Opensolutionfile{shortsolutions}
\Newassociation{solution}{ShortSoln}{shortsolutions}
\begin{document}
\begin{question}
Question text goes here.
\begin{solution}
Answer text goes here.
\end{solution}
\end{question}
\begin{question}
Question text goes here.
\begin{solution}
Answer text goes here.
\end{solution}
\end{question}
% close the solutions files
\Closesolutionfile{shortsolutions}
\clearpage
% this just makes the displayed solutions use the itemize
% environment- makes the dispaly better
\renewenvironment{ShortSoln}[1]{%
\itemize\item[{\bfseries(#1)}]%
}%
{\enditemize}
% input the answers file
\section*{Answers}
\IfFileExists{shortsolutions.tex}{\input{shortsolutions.tex}}{}
\end{document}