我使用该exam
课程进行考试。当我在环境中使用方程式时solution
,即使未打印解决方案,方程式编号的计数器也会更新。当未打印解决方案时,是否可以不考虑解决方案环境中的计数器?
下面是一个 MWE。它产生了错误的方程编号。对于打印解决方案的情况,编号是正确的。
%\documentclass[11pt,answers]{exam}
\documentclass[11pt]{exam}
\usepackage{parskip}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{microtype}
\usepackage{mathtools}
\usepackage{amsmath}
\usepackage{hyperref}
\begin{document}
\centerline{\bf Exam Class: Numbering Equations, MWE}
\begin{questions}
\question Answer the following questions.
\begin{parts}
\part Let $ a=3 $ and $ b=4 $. Consider the equation
\begin{equation}
a^{2} + b^{2} = c^{2},
\end{equation}
where all values are positive. Calculate the value of $ c $.
\begin{solution}
We have, given that $ c>0 $,
\begin{equation}
c^{2} = 3^{2} + 4^{2} \Rightarrow c = 5.
\end{equation}
\end{solution}
\part Let $ a=5 $. The variable $ b $ is given by
\begin{equation}
b = a^{2}.
\end{equation}
Calculate the value of $ b $.
\begin{solution}
We have
\begin{equation}
b = 5^{2} \Rightarrow b = 25.
\end{equation}
\end{solution}
\end{parts}
\end{questions}
\end{document}
答案1
一种快速解决方法是根据选项使用\begin{equation*}
和\end{equation*}
或标准方程环境answers
。因此我们定义一个新的环境equationsolution
:
\newenvironment{equationsolution}{%
\ifprintanswers\begin{equation}\else\begin{equation*}\fi
}{%
\ifprintanswers\end{equation}\else\end{equation*}\fi
}
梅威瑟:
%\documentclass[11pt,answers]{exam}
\documentclass[11pt]{exam}
\usepackage{parskip}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{microtype}
\usepackage{mathtools}
\usepackage{amsmath}
\usepackage{hyperref}
\newenvironment{equationsolution}{%
\ifprintanswers\begin{equation}\else\begin{equation*}\fi
}{%
\ifprintanswers\end{equation}\else\end{equation*}\fi
}
\begin{document}
\centerline{\bf Exam Class: Numbering Equations, MWE}
\begin{questions}
\question Answer the following questions.
\begin{parts}
\part Let $ a=3 $ and $ b=4 $. Consider the equation
\begin{equation}
a^{2} + b^{2} = c^{2},
\end{equation}
where all values are positive. Calculate the value of $ c $.
\begin{solution}
We have, given that $ c>0 $,
\begin{equationsolution}
c^{2} = 3^{2} + 4^{2} \Rightarrow c = 5.
\end{equationsolution}
\end{solution}
\part Let $ a=5 $. The variable $ b $ is given by
\begin{equation}
b = a^{2}.
\end{equation}
Calculate the value of $ b $.
\begin{solution}
We have
\begin{equationsolution}
b = 5^{2} \Rightarrow b = 25.
\end{equationsolution}
\end{solution}
\end{parts}
\end{questions}
\begin{equation}
c = \sqrt{a^2+b^2}
\end{equation}
\end{document}
请注意,学生看到的考试和带有答案的考试之间可能会有不一致。这就是为什么我总是把答案放在考试题目后面。
答案2
我找到了一个使用新计数器和 的解决方案ifprintanswers
。新计数器存储方程计数器的值,并在打印答案时使用它。计数器的解决方案基于 的解决方案这问题。
这是新的 MWE:
%\documentclass[11pt,answers]{exam}
\documentclass[11pt]{exam}
\usepackage{parskip}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{microtype}
\usepackage{mathtools}
\usepackage{amsmath}
\usepackage{hyperref}
\newcounter{eqBeforeSolution} % New counter
\begin{document}
\centerline{\bf Exam Class: Numbering Equations, MWE}
\begin{questions}
\question Answer the following questions.
\begin{parts}
\part Let $ a=3 $ and $ b=4 $. Consider the equation
\begin{equation}
a^{2} + b^{2} = c^{2},
\end{equation}
where all values are positive. Calculate the value of $ c $.
\setcounter{eqBeforeSolution}{\value{equation}} % Store current equation counter value
\begin{solution}
We have, given that $ c>0 $,
\begin{equation}\label{eq10}
c^{2} = 3^{2} + 4^{2} \Rightarrow c = 5.
\end{equation}
Equation \eqref{eq10} shows the solution.
\end{solution}
\ifprintanswers
\else
% Uses the value of the new counter to reset the equation counter
\setcounter{equation}{\value{eqBeforeSolution}}
\fi
\part Let $ a=5 $. The variable $ b $ is given by
\begin{equation}
b = a^{2}.
\end{equation}
Calculate the value of $ b $.
\begin{solution}
We have
\begin{equation}
b = 5^{2} \Rightarrow b = 25.
\end{equation}
\end{solution}
\end{parts}
\end{questions}
\end{document}