我正在课堂笔记中写一些问题的解决方案,遇到了问题和答案计数器不同步的问题。我的意思是:
为了解决这个问题(它说的是答案 1.2 而不是 1.3),我刚刚添加了额外的行
\setcounter{answer}{2}
,但当我重新排列问题并必须手动设置许多答案编号时,这有点麻烦。有解决办法吗?作为参考,这是我的 LaTeX 代码:
\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}
\theoremstyle{definition}
\declaretheorem[name=Problem,numberwithin=section]{prob}
\declaretheorem[name=Answer,numberwithin=section]{answer}
\newenvironment{solution}{\begin{trivlist}\item[]{\textbf{Solution.}}}{\qed \end{trivlist}}
\begin{document}
\section{Section Title}
\begin{prob}
Problem text.
\end{prob}
\begin{answer}
Answer.
\end{answer}
\begin{solution}
Solution text.
\end{solution}
\begin{prob}
This is a proof problem.
\end{prob}
\begin{proof}
Proof text.
\end{proof}
\begin{prob}
Problem text.
\end{prob}
\begin{answer}
Oh no, the answer numbering is bad.
\end{answer}
\end{document}
答案1
这是一个使用amsthm
环境的解决方案,但从中定义其他环境来调整调用问题和答案时的计数器。
\documentclass{article}
\usepackage{amsmath,amsthm}
\theoremstyle{definition}
\newtheorem{prob}{Problem}
\newtheorem{ans}{Answer}
\numberwithin{prob}{section}
\numberwithin{ans}{section}
\newenvironment{problem}{%
\addtocounter{ans}{1}
\begin{prob}
}{%
\end{prob}
}
\newenvironment{answer}{%
\addtocounter{ans}{-1}
\begin{ans}
}{%
\end{ans}
}
\begin{document}
\section{First section}
\begin{problem}
Problem text.
\end{problem}
\begin{answer}
Answer.
\end{answer}
\begin{problem}
Proof problem without answer.
\end{problem}
\begin{problem}
Problem text.
\end{problem}
\begin{answer}
Yeah, the answer number is right!
\end{answer}
\end{document}