我正在尝试使用两个定理环境,一个用于练习,另一个用于解决方案。解决方案的计数器应与相关练习的计数器相同。
一个例子:
Exercise 1.1. What is 2+2?
Exercise 1.2. What is 3+5?
Solution to Exercise 1.1. 2+2=4.
Solution to Exercise 1.2. 3+5=8.
应为输出:
\begin{exercise}\label{ex: 22} What is 2+2?\end{exercise}
\begin{exercise}\label{ex: 35} What is 3+5?\end{exercise}
\begin{solution}[ex: 22] 2+2=4.\end{solution}
\begin{solution}[ex: 35] 3+5=8.\end{solution}
使用 amsthm 包可以实现这一点吗?
答案1
我认为一个计数器和标签引用机制就足够了:
\documentclass{article}
\usepackage{amsthm}
\newcounter{exercise}
\counterwithin{exercise}{section}
\NewDocumentEnvironment{exercise}{ m +b }{%
\noindent
Exercise~\refstepcounter{exercise}\theexercise\label{#1}. #2}{\par\medskip}
\newenvironment{solution}[1]{%
\begin{proof}[Solution to Exercise~\ref{#1}]}%
{\end{proof}}
\begin{document}
\section{AAAAA}
\begin{exercise}{ex:22} What is 2+2?\end{exercise}
\begin{exercise}{ex:35} What is 3+5?\end{exercise}
\begin{solution}{ex:22} 2+2=4.\end{solution}
\begin{solution}{ex:35} 3+5=8.\end{solution}
\end{document}
您还可以使用 amsthm 来定义exercise
环境,这会使代码更简单一些:
\documentclass{article}
\usepackage{amsthm}
\newtheorem{exercise}{Exercise}[section]
\newenvironment{solution}[1]{%
\begin{proof}[Solution to Exercise~\ref{#1}]}%
{\end{proof}}
\begin{document}
\section{AAAAA}
\begin{exercise}\label{ex:22} What is 2+2?\end{exercise}
\begin{exercise}\label{ex:35} What is 3+5?\end{exercise}
\begin{solution}{ex:22} 2+2=4.\end{solution}
\begin{solution}{ex:35} 3+5=8.\end{solution}
\end{document}