Amsthm 编号取决于截面深度

Amsthm 编号取决于截面深度

我正在使用amsthmthmtools定义自定义定理环境、定义环境等。所有这些环境共享相同的计数器。

是否可以根据截面深度改变编号格式?我想要实现以下编号格式:

1 节

1.1 定理

1.2 定义

1.1 小节

1.1.1 定理

1.1.2 引理

答案1

您可以在定理计数器前加上特定部分的编号方案:

在此处输入图片描述

\documentclass{article}
\usepackage{amsthm,etoolbox}
\newtheorem{theorem}{Theorem}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{lemma}[theorem]{Lemma}

\makeatletter
\@addtoreset{theorem}{section}% Reset theorem counter with every section
\@addtoreset{theorem}{subsection}
\@addtoreset{theorem}{subsubsection}
\newcommand{\theoremprefix}{}
\let\thetheoremsaved\thetheorem
\renewcommand{\thetheorem}{\theoremprefix\thetheoremsaved}
\let\sectionsaved\section
\patchcmd{\@startsection}{\par}{\renewcommand{\theoremprefix}{\csname the#1\endcsname.}}{}{}
\makeatother

\begin{document}

\section{Section}
\begin{theorem}
Th 1
\end{theorem}
\begin{definition}
Df 1
\end{definition}
\subsection{Subsection}
\begin{theorem}
Th 2
\end{theorem}
\begin{lemma}
Lm 1
\end{lemma}
\section{Two}
\begin{theorem}
Th 3
\end{theorem}
\begin{lemma}
Lm 2
\end{lemma}
\subsection{Other}
\begin{theorem}
Th 4
\end{theorem}
\subsection{Other 2}
\begin{definition}
Df 2
\end{definition}

答案2

我不确定你是否想要这样的东西:

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\swapnumbers
\newtheorem{theorem}{Theorem}[section]
\newtheorem{definition}[theorem]{Definition}
\newtheorem{lemma}[theorem]{Lemma}
\usepackage{thmtools}

\usepackage{xpatch}
\makeatletter
\newif\ifsection
\newif\ifsubsection
\preto\section{\sectiontrue\subsectionfalse}
\preto\subsection{\sectionfalse\subsectiontrue}
\patchcmd{\@xsect}% <cmd>
  {\ignorespaces}% <search>
  {\ifsection
    \numberwithin{theorem}{section}
    \else
    \numberwithin{theorem}{subsection}
    \fi
    \setcounter{theorem}{0}\relax\ignorespaces}% <replace>
  {}{}% <success><failure>
\makeatother
\begin{document}

\section{Section}
\begin{theorem}
Th 1
\end{theorem}
\begin{definition}
Df 1
\end{definition}
\subsection{Subsection}
\begin{theorem}
Th 2
\end{theorem}
\begin{lemma}
Lm 1
\end{lemma}
\section{Two}
\begin{theorem}
Th 3
\end{theorem}
\begin{lemma}
Lm 2
\end{lemma}
\subsection{Other}
\begin{theorem}
Th 4
\end{theorem}
\subsection{Other 2}
\begin{definition}
Df 2
\end{definition}
\end{document}

在此处输入图片描述

相关内容