从根据节或小节中的嵌套切换定理类环境的编号方法我得到了以下定理环境
\newtheorem{theorem}{Theorem}
\counterwithin*{theorem}{section}
\counterwithin*{theorem}{subsection}
\makeatletter
\renewcommand{\thetheorem}{%
\ifnum\value{subsection}=0
\thesection
\else
\thesubsection
\fi
.\arabic{theorem}%
}
如果没有子节,则按照节进行编号,如果有子节,则按照子节进行编号。当然,我希望所有其他环境都采用这种方式,所以我复制并粘贴,并将“定理”改为“命题”,然后改为“例子”等等。现在的问题是,这个环境只记住每个定义对象的编号,定理的编号方式与例子无关,等等。例如,我可以有这种情况
定理1.1
...
示例 1.1
...
定理1.2
...
定义1.1
等等。这确实不可取。有没有办法将这些环境的编号全球化,如上所定义?提前致谢。
\documentclass[11pt,a4paper]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, chngcntr}
\newtheorem{theorem}{Theorem}
\counterwithin*{theorem}{section}
\counterwithin*{theorem}{subsection}
\makeatletter
\renewcommand{\thetheorem}{%
\ifnum\value{subsection}=0
\thesection
\else
\thesubsection
\fi
.\arabic{theorem}%
}
\newtheorem{proposition}{Proposition}
\counterwithin*{proposition}{section}
\counterwithin*{proposition}{subsection}
\makeatletter
\renewcommand{\theproposition}{%
\ifnum\value{subsection}=0
\thesection
\else
\thesubsection
\fi
.\arabic{proposition}%
}
\begin{document}
\section{bla}
\begin{theorem}
\end{theorem}
\begin{proposition}
\end{proposition}
\begin{proposition}
\end{proposition}
\section{blabla}
\subsection{blablabla}
\begin{theorem}
\end{theorem}
\begin{theorem}
\end{theorem}
\begin{proposition}
\end{proposition}
\end{document}
答案1
使用在类定理环境之间共享计数器的标准方法:
\documentclass[11pt,a4paper]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, chngcntr}
% define the theorem-like environments we need
\newtheorem{theorem}{Theorem}
\newtheorem{proposition}[theorem]{Proposition}
% normalize the theorem counter
\counterwithin*{theorem}{section}
\counterwithin*{theorem}{subsection}
\renewcommand{\thetheorem}{%
\ifnum\value{subsection}=0
\thesection
\else
\thesubsection
\fi
.\arabic{theorem}%
}
请注意,你\makeatletter
错了(通常没有附带\makeatother
);由于代码不使用@
命令,因此甚至不需要它。