答案1
我假设你正在使用amsthm
来管理你的定理。在这种情况下,你可以将环境的父计数器设置solution
为你的定理的计数器,如下所示
\documentclass{article}
\usepackage{amsthm}
\newtheorem{thm}{Theorem}[subsection]
\newtheorem{solution}{Solution}[thm]
% ^^^ Note that as <parent counter> we are using
% the thm counter
\begin{document}
\section{Sec 1}
\subsection{Subsec 1}
\begin{thm}
Test
\end{thm}
\begin{solution}
Sol 1
\end{solution}
\begin{solution}
Sol 2
\end{solution}
\section{Sec 2}
\subsection{Subsec 2}
\begin{thm}
Test
\end{thm}
\begin{solution}
Sol 1
\end{solution}
\begin{solution}
Sol 2
\end{solution}
\end{document}
屈服
答案2
以下示例仅当同一问题有多个解决方案时才适用于对问题中的解决方案进行编号。它的基本工作原理是定义一个命令,该命令将给定问题的解决方案数量存储在文件中.aux
,然后重新定义命令\thesolution
以根据问题的解决方案数量显示解决方案的数量。这有点复杂,也许有更简单的方法可以做到这一点,但我想不出任何方法。
以下是示例:
\documentclass{article}
\usepackage{amsmath,amsthm}
\usepackage{etoolbox}
\newtheorem{problem}{Problem}[subsection]
\theoremstyle{remark}
\newtheorem{solution}{Solution}
\newcounter{numsolution}
\numberwithin{numsolution}{problem}
\makeatletter
\AtBeginEnvironment{problem}{%
\addtocounter{problem}{1}
\newcounter{solsofprob\the\value{section}.\the\value{subsection}.\the\value{problem}}
\addtocounter{problem}{-1}
}
\AtBeginEnvironment{solution}{%
\addtocounter{solsofprob\the\value{section}.\the\value{subsection}.\the\value{problem}}{1}
\immediate\write\@mainaux{\string\gdef\string\numsolsofprob\romannumeral\the\value{section}@\romannumeral\the\value{subsection}@\romannumeral\the\value{problem}{\the\value{solsofprob\the\value{section}.\the\value{subsection}.\the\value{problem}}}}%
}
\newcommand{\tempnum}{0}
\renewcommand{\thesolution}{%
\@ifundefined{numsolsofprob\romannumeral\the\value{section}@\romannumeral\the\value{subsection}@\romannumeral\the\value{problem}}%
{\def\tempnum{0}}%
{\edef\tempnum{\csname numsolsofprob\romannumeral\the\value{section}@\romannumeral\the\value{subsection}@\romannumeral\the\value{problem}\endcsname}}%
\ifnum \tempnum > 1
\stepcounter{numsolution}%
\arabic{section}.\arabic{subsection}.\arabic{problem}--\arabic{numsolution}%
\else
\arabic{section}.\arabic{subsection}.\arabic{problem}%
\fi%
}
\makeatother
\begin{document}
\section{First section}
\subsection{First subsection}
\begin{problem}
A problem.
\end{problem}
\begin{solution}
The solution.
\end{solution}
\begin{problem}
Another problem.
\end{problem}
\begin{solution}
A first solution.
\end{solution}
\begin{solution}
A second solution.
\end{solution}
\begin{problem}
Another problem.
\end{problem}
\begin{solution}
A solution.
\end{solution}
\subsection{Second subsection}
\begin{problem}
A problem.
\end{problem}
\begin{solution}
A solution.
\end{solution}
\section{Second section}
\subsection{A subsection}
\begin{problem}
A problem.
\end{problem}
\begin{solution}
A solution.
\end{solution}
\begin{solution}
Another solution.
\end{solution}
\end{document}
我还应该提到,由于此解决方案通过\thesolution
以复杂的方式重新定义命令来工作,因此无法将标签添加到解决方案中以供以后引用。如果我想到可以让标签起作用的方法,我会更新代码。