我从头开始创建解决方案手册。我希望能够通过设置开关,只生成每个部分中奇数问题的解决方案,或生成所有问题的解决方案。
我更倾向于不使用其中一个答案包,因为我过去使用这些答案包并没有取得很大的成功。我首选的解决方案是修改分项列表宏,使它们在特定情况下不会产生任何输出。或者还有其他更好的方法吗?
我应该补充一点,练习将按章节.部分.# 进行编号,并且# 是偶数还是奇数应该决定包含的内容。
我想我没有提供足够的信息来给出一个像样的答案,但我不确定还能说些什么......
答案1
下面展示的可能是您正在寻找的概念证明。
使用的软件包包括
布尔值oddonly
用于确定是否仅打印奇数答案。使用将其设置为true
或(其中是或)。两个计数器分别用于对问题和答案进行编号。如上所述,定义环境(和)的原因是因为您可能希望在问题/答案中包含大量描述,而该包对此很有用。false
\oddonly<bool>
<bool>
true
false
question
answer
environ
\documentclass{book}
\usepackage{enumitem}% http://ctan.org/pkg/enumitem
\usepackage{environ}% http://ctan.org/pkg/environ
\begin{document}
\newif\ifoddonly% Boolean to print odd-only answers
\oddonlytrue% Odd only answers = TRUE
\newcounter{question}[section]% Question counter (within section)
\newcounter{answer}[section]% Answer counter (within section)
\NewEnviron{question}{\stepcounter{question}\item \BODY}% Question
\NewEnviron{answer}{% Answer
\stepcounter{answer}%
\ifodd\value{answer}% Condition on value of answer
\item \BODY% Print odd answer
\else\ifoddonly\else
\item \BODY% Print even answer
\fi\fi%
}
% QUESTIONS ===========
\chapter{Some chapter}
\section{Questions} \label{questions}
\begin{enumerate}[label=\arabic{chapter}.\arabic{section}.\arabic{question})]
\begin{question} Here is a question? \end{question}
\begin{question} Here is another question? \end{question}
\begin{question} This is yet another question? \end{question}
\begin{question} Oh, and another one? \end{question}
\begin{question} Here's the final question? \end{question}
\end{enumerate}
% ANSWERS =============
\section{Answers to Section~\ref{questions}}
\begin{enumerate}[label=\ref{questions}.\arabic{answer})]
\begin{answer} Answer to this question. \end{answer}
\begin{answer} Answer to that question. \end{answer}
\begin{answer} Answer to another question. \end{answer}
\begin{answer} Answer to fourth question. \end{answer}
\begin{answer} Final answer. \end{answer}
\end{enumerate}
\end{document}
\oddonlyfalse
在上面的例子中设置(默认值)会产生:
您可能想要尝试列表标签的对齐方式。但是,为此,我建议您查看enumitem
包装文档。
答案2
我知道您不想使用该answers
包来解决问题,但对于任何感兴趣的人,我在下面提供了一个解决方案,这是我用于此任务的精简版。
您可以使用以下开关显示/隐藏奇数(或偶数,或两者)编号的解决方案,设置为 1(显示)或 0(隐藏):
\setcounter{showodd}{1}
\setcounter{showeven}{1}
代码:
\documentclass{report}
\usepackage{ifthen} % conditionals
\usepackage{answers} % solutions to problems
\usepackage{xstring} % used to determine if problems are odd or even
% question environment
\newcounter{question}
\setcounter{question}{0}
\renewcommand{\thequestion}{\thesection.\arabic{question}}
\newenvironment{question}{\refstepcounter{question}\thequestion.}{}
% open the solutions file
\Opensolutionfile{shortsolutions}
\Newassociation{shortsolution}{shortSoln}{shortsolutions}
\setlength{\parindent}{0mm}
\begin{document}
\chapter{Some chapter}
\section{Questions}
\begin{question}
Here's a question.
\begin{shortsolution}
First answer.
\end{shortsolution}
\end{question}
\begin{question}
Another question.
\begin{shortsolution}
Second answer.
\end{shortsolution}
\end{question}
\begin{question}
Here's a question.
\begin{shortsolution}
Third answer.
\end{shortsolution}
\end{question}
\begin{question}
Another question.
\begin{shortsolution}
Fourth answer.
\end{shortsolution}
\end{question}
% close the solutions files
\Closesolutionfile{shortsolutions}
% set up switches to show odd or even problems
% 1: show
% 0: don't show (actually, anything other than 1 makes it not show)
\newcounter{showodd}
\newcounter{showeven}
\setcounter{showodd}{1}
\setcounter{showeven}{1}
% solution to problem (show only odd, even, all)
% Note: this renewenvironment needs to go here
% so that the answers package can still
% display correctly to the page if needed
\newcounter{oddproblemnumber}
\renewenvironment{shortSoln}[1]{%
% before the environment starts
\fullexpandarg % need this line so that '.' are counted
% numbers such as 1.3.1, 1.2.4, 7.5.6
\StrBehind{#1}{.}[\newbit]%
\StrBehind{\newbit}{.}[\problemnumber]%
% determine if the problem number is odd or even
% and depending on our choices above, display or not
\ifthenelse{\isodd{\problemnumber}}%
{%
% set a counter that says the problem number is odd (used later)
\setcounter{oddproblemnumber}{1}%
% display or not
\ifthenelse{\theshowodd=1}
{%
% if we want to show the odd problems
{\par\bfseries #1.}%
}%
{%
% otherwise don't show them!
\expandafter\comment%
}%
}%
{%
% even numbered problem, set the counter (used later)
\setcounter{oddproblemnumber}{0}%
\ifthenelse{\theshoweven=1}
{%
% if we want to show the even problems
{\par\itshape #1.}%
}%
{%
% otherwise don't show them!
\expandafter\comment%
}%
}%
}%
{%
% after the environment finishes
\ifthenelse{\theoddproblemnumber=1}%
{%
% odd numbered problems
\ifthenelse{\theshowodd=1}
{%
% if we want to show the odd problems
% then the environment is finished
}%
{%
% otherwise we need to finish the comment
\expandafter\endcomment%
}%
}%
{%
% even numbered problems
\ifthenelse{\theshoweven=1}
{%
% if we want to show the even problems
% then the environment is finished
}%
{%
% otherwise we need to finish the comment
\expandafter\endcomment%
}%
}%
}
\newpage
\section{Answers}
\IfFileExists{shortsolutions.tex}{\input{shortsolutions.tex}}{}
\end{document}
答案3
我不知道这是否对你有帮助,但我创建条件文档的方法是使用版本包。使用此包,您可以通过以下方式包含选定区域:
\begin{VERSIONfinal}
This text will only appear if VERSIONfinal is included
\end{VERSIONfinal}
在之后的某个时间,您必须使用或\begin{document}
来包含或排除特定版本。\includeversion{VERSIONfinal}
\excludeversion{VERSIONfinal}
注意:还有一个版本包,到目前为止我还没有使用它。