第1部分

第1部分

因此我使用的amsthm定义如下:

\newtheorem{dfn}{Definition}
\makeatletter
\@addtoreset{dfn}{subsection}
\makeatother

这意味着,如果我在每个子节调用 dfn 环境,计数器将从 1 开始,这正是我想要的,但我发现在引用不同的(子)节时很难做到。因此,我想知道是否可以在 dfn 环境的原始计数器之前添加一个计数器,以便它的行为类似于以下内容:


第1部分

第1节

一个叫做 A 的东西

定义1.1一些定义

定义1.2还有一些其他的定义

第2节

一些其他文本,但本小节未调用 dfn。

第3节

定义2.1子节 3 的定义

定义2.2还有更多


我找到了一种解决方案,就是直接将子节的编号转储到 dfn 计数器之前,但这不是我想要的。我想要的可能是一个独立的计数器,每次我在不同的子节调用环境时,它都会增加。我该如何实现呢?

答案1

就我个人而言,我不希望在第三部分出现“定义 2.X”,因为我认为这会产生误导。

\documentclass{article}

\newcounter{mycounter} \stepcounter{mycounter} % Counter mycounter has now value 1.
\newif\ifdfnused\dfnusedfalse % Define \ifdfnused an set to false. (True, when a defn-environment was used in the current section. False otherwise.)

\newtheorem{dfn}{Definition} % Define dfn-environment.
\counterwithin{dfn}{mycounter} % Reset dfn-counter whenever mycounter is incremented.
\AfterEndEnvironment{dfn}{\dfnusedtrue} % When a dfn-environment was used, set dfnused to true.

\let\oldsection\section % Save old \section-command.
\def\section{% Define new section-command:
   \ifdfnused% If dfn-environment was used ...
   \stepcounter{mycounter}% ... then increment mycounter (this will set defn-counter to 0) ...
   \fi% ... otherwise do nothing.
   \dfnusedfalse% Set dfnused to false as we start a new section (of course, in this new section there was no defn-environment until now).
   \oldsection% Start new section.
}

%%% TEST %%%

\begin{document}
   \section{Firstsection}
   \begin{dfn}
      First definition in this section.
   \end{dfn}
   \begin{dfn}
      Second definition in this section.
   \end{dfn}
   \section{Secondsection}
   This is a section without definitions.
   \section{Thirdsection}
   \begin{dfn}
      First definition in this section.
   \end{dfn}
\end{document}

在此处输入图片描述

答案2

这实现了你的要求,但编号是非常很奇怪。人们怎么知道定义 7.1 在第 3.4 小节中,或者定义 3.1 在第 2 节中?

\documentclass{article}

\newcounter{predfn}
\setcounter{predfn}{1}
\newtheorem{dfn}{Definition}[predfn]

\NewCommandCopy{\latexsection}{\section}
\NewCommandCopy{\latexsubsection}{\subsection}

\RenewDocumentCommand{\section}{sO{#3}m}{%
  \IfBooleanTF{#1}
   {% starred section, just don't worry
    \latexsection*{#3}%
   }
   {% numbered section
    \ifnum\value{dfn}>0 \stepcounter{predfn}\fi
    \latexsection[#2]{#3}%
   }%
}
\RenewDocumentCommand{\subsection}{sO{#3}m}{%
  \IfBooleanTF{#1}
   {% starred section, just don't worry
    \latexsubsection*{#3}%
   }
   {% numbered section
    \ifnum\value{dfn}>0 \ifnum\value{subsection}>0 \stepcounter{predfn}\fi\fi
    \latexsubsection[#2]{#3}%
   }%
}

\begin{document}

\section{First section}

\subsection{First sub}

\begin{dfn}
First definition in this section.
\end{dfn}

\begin{dfn}
Second definition in this subsection.
\end{dfn}

\subsection{Second sub}

This is a section without definitions.

\subsection{Third sub}

\begin{dfn}
First definition in this subsection.
\end{dfn}

\section{Second section}

\begin{dfn}
Definition in the second section.
\end{dfn}

\subsection{Fourth sub}

No definitions here.

\subsection{Fifth sub}

\begin{dfn}
A definition here.
\end{dfn}

\section{Third section}

\subsection{Sixth sub}

\begin{dfn}
A definition here.
\end{dfn}

\subsection{Seventh sub}

\begin{dfn}
A definition here.
\end{dfn}

\subsection{Eigth sub}

No definition here.

\subsection{Ninth sub}

\begin{dfn}
A definition here.
\end{dfn}

\end{document}

在此处输入图片描述

相关内容