我正在使用文章类。没有任何章节。它仅包含一些部分。
我的第一部分有五个小节。我想要按节列出的定理和定义编号,例如定理 1.2.4 或定义 1.5.2 等。
后面的部分没有小节。第二部分没有小节。所以这里我想要像定理 2.1 或定义 2.4 等没有小节编号的内容。
请建议合适的命令。
答案1
您可以定义一个新定理Definition
,其中计数器取决于和section
,并且只要计数器小于 1,subsection
计数器的输出\theDefinition
就会使用,否则:\thesection
subsection
\thesubsection
\documentclass{article}
\usepackage{ntheorem}
\usepackage{chngcntr}
\newtheorem{Definition}{Definition}[subsection]% Counter `Definition` depends on `subsection` …
\counterwithin*{Definition}{section}% … and also on `section`.
\renewcommand*{\theDefinition}{% Output of `Definition` counter:
\ifnum\value{subsection}<1 % if `subsection` counter is less than 1 (mostly `\subsection` not yet used in this `\section`)
\thesection% show \thesection, e.g., 1
\else% otherwise
\thesubsection% show \thesubsection, e.g., 1.1
\fi
.\arabic{Definition}% followed by "." and Arabic number of Definition
}
\usepackage{mwe}
\begin{document}
\section{Test section}
\begin{Definition}[Definition on section level]
\blindtext
\end{Definition}
\subsection{Test subsection}
\begin{Definition}[Definition in subsection level]
\blindtext
\end{Definition}
\section{Second test section}
\begin{Definition}[Second theorem on section level]
\blindtext
\end{Definition}
\subsection{Second test subsection}
\begin{Definition}[Second theorem in subsection level]
\blindtext
\end{Definition}
\end{document}
结果是:
答案2
这是该过程的抽象,如果您最终改变了对编号的想法,它将非常有用。
\documentclass{article}
\usepackage{amsthm}
\usepackage{xparse,chngcntr}
\ExplSyntaxOn
\NewDocumentCommand{\newtheorembysubsection}{mom}
{
\IfNoValueTF{#2}
{
\newtheorem{#1}{#3}[subsection]
\cs_set:cpn { the#1 } { \thesection. \deepesh_subsection: \arabic{#1} }
\counterwithin*{#1}{section}
}
{
\newtheorem{#1}[#2]{#3}
}
}
\cs_new:Nn \deepesh_subsection:
{
\int_compare:nT { \value{subsection}>0 } { \arabic{subsection}. }
}
\ExplSyntaxOff
\newtheorembysubsection{theorem}{Theorem}
\theoremstyle{definition}
\newtheorembysubsection{definition}[theorem]{Definition} % could be \newtheorem
\begin{document}
\section{Test section}
\begin{theorem}
A theorem, numbered 1.1
\end{theorem}
\begin{definition}
A definition, numbered 1.2
\end{definition}
\subsection{Test subsection}
\begin{definition}
A definition, numbered 1.1.1
\end{definition}
\begin{theorem}
A theorem, numbered 1.1.2
\end{theorem}
\section{Test second section}
\begin{theorem}
A theorem, numbered 2.1
\end{theorem}
\subsection{Test subsection}
\begin{theorem}
A theorem, numbered 2.1.1
\end{theorem}
\end{document}
如果您决定按部分对定理进行编号(应该如此,我发现这种方案对读者来说相当复杂),您只需更改\newtheorembysubsection
为\newtheorem
,可能需要在需要时添加尾随[section]
。