我希望定理能够按深度进行编号,即:
第1章。
定理 1.1。
第 1.1 节。
定理 1.1.1。
1.1.1 小节。
定理1.1.1.1。
定理 1.1.1.2。
定理1.1.1.3。
我使用了不同类型的定理,例如
\newtheorem{th}{Theorem}[chapter]
\newtheorem{thh}{Theorem}[section]
\newtheorem{thhh}{Theorem}[subsection]
但是如果我将某个部分的一部分改为小节,我就必须改变其中的所有定理类型。
有什么办法可以统一这一点吗?
答案1
以下代码使用依赖于当前部分单元的前缀来更新计数器/环境\thetheorem
的计数器表示:theorem
\documentclass{report}
\newtheorem{theorem}{Theorem}
\makeatletter
\newcommand{\updatetheoremprefixfor}[1]{%
\expandafter\expandafter\expandafter\let\expandafter\csname old#1\expandafter\endcsname\csname #1\endcsname
\@namedef{#1}{\renewcommand{\thetheorem}{\csname the#1\endcsname.\arabic{theorem}}
\setcounter{theorem}{0}%
\csname old#1\endcsname}}
\makeatother
\updatetheoremprefixfor{chapter}
\updatetheoremprefixfor{section}
\updatetheoremprefixfor{subsection}
\updatetheoremprefixfor{subsubsection}
\begin{document}
\chapter{A chapter}
\begin{theorem}
This is a theorem within a \verb|\chapter|.
\end{theorem}
\section{A section}
\begin{theorem}
This is a theorem within a \verb|\section|.
\end{theorem}
\subsection{A subsection}
\begin{theorem}
This is a theorem within a \verb|\subsection|.
\end{theorem}
\begin{theorem}
This is a theorem within a \verb|\subsection|.
\end{theorem}
\begin{theorem}
This is a theorem within a \verb|\subsection|.
\end{theorem}
\section{A section}
\begin{theorem}
This is a theorem within a \verb|\section|.
\end{theorem}
\begin{theorem}
This is a theorem within a \verb|\section|.
\end{theorem}
\subsection{A subsection}
\begin{theorem}
This is a theorem within a \verb|\subsection|.
\end{theorem}
\end{document}