11

\numberwithin{equation}{subsection} is in the preamble of an article. Equations in sections before subsection 1 of that section begin numbering as x.0.y+1, where "y" is the number of the last equation of the last subsection of the previous section. (Numbers are OK in subsections, as 2.1.1, etc.) I can't see how such behavior would ever be desirable. Example:

\documentclass[12pt]{article}
\usepackage{amsmath}
\numberwithin{equation}{subsection}
\begin{document}
\section{1}
\begin{equation}
1
\end{equation}
\subsection{1}
\begin{equation}
1
\end{equation}
\subsection{1}
\begin{equation}
1
\end{equation}
\begin{equation}
1
\end{equation}
\section{1}
\begin{equation}
1
\end{equation}
\subsection{1}
\begin{equation}
1
\end{equation}
\section{1}
\begin{equation}
1
\end{equation}
\subsection{1}
\begin{equation}
1
\end{equation}
\end{document}

produces equations numbered (1.0.1), (1.1.1), (1.2.1), (1.2.2), (2.0.3), (2.1.1), (3.0.2), (3.1.1).

  • 1
    The counter is now reset each time a new subsection is used, but not if a new section is used ;-) If there is no subsection its value is 0 of course. – user31729 May 20 '15 at 12:07
7

Update

Since the release of LaTeX from 2018/04/01 the mentioned package chngcntr is now part of the LaTeX kernel -- it's not necessary to load it separately.

Using \numberwithin{equation}{subsection} changes the counter output format and shifts the resetting of the equation counter from section to subsection level (i.e. each time \stepcounter{subsection} is used. However, this means that an orphane equation after a \section, but before \subsection will just use the old counter value, from a previous equation.

This resets the counter of equation number each time a new \section is used, but will not change the \theequation command (being the counter formatting macro)

\makeatletter
\@addtoreset{equation}{section}
\makeatother

As an alternative to this low-level variant, it's possible to use

\usepackage{chngcntr}
\counterwithin{equation}{section}

in the preamble.

However, this will not solve the issue with trailing 0 if the subsection number is 0. This can be attacked with a conditional within \theequation or -- the better way -- avoid equations outside of \subsection if subsection is the driver counter.

\documentclass[12pt]{article}
\usepackage{amsmath}
\makeatletter
\@addtoreset{equation}{section}
\makeatother
\numberwithin{equation}{subsection}

\begin{document}
\section{1}
\begin{equation}
1
\end{equation}
\subsection{1}
\begin{equation}
1
\end{equation}
\subsection{1}
\begin{equation}
1
\end{equation}
\begin{equation}
1
\end{equation}
\section{1}
\begin{equation}
1
\end{equation}
\subsection{1}
\begin{equation}
1
\end{equation}
\section{1}
\begin{equation}
1
\end{equation}
\subsection{1}
\begin{equation}
1
\end{equation}
\end{document}

enter image description here

  • Using the chngcntr package is easier: \counterwithin*{equation}{section} and \counterwithin(equation}{subsection} is all you need. The first is starred since we don't want to perform the redefinition of \theequation that would be overridden with the next command. – egreg May 20 '15 at 16:41
  • @egreg: I mentioned the chngcntr package (however, it's only a small comment. Perhaps I'll change my answer later on, but thanks for notifying me – user31729 May 20 '15 at 17:49
6

I do not see the benefit of using such complicated equation numbers. Numbering equations within sections is more than enough IMHO.

Nevertheless, the additional level can be suppressed, if the subsection counter is zero.

Also \numberwithin does not define a transitive closure. Thus the equation number needs to be reset for each \section, too.

\documentclass[12pt]{article}
\usepackage{amsmath}
\numberwithin{equation}{section}% reset equation counter for sections
\numberwithin{equation}{subsection}
% Omit `.0` in equation numbers for non-existent subsections.
\renewcommand*{\theequation}{%
  \ifnum\value{subsection}=0 %
    \thesection
  \else
    \thesubsection
  \fi
  .\arabic{equation}%
}
\begin{document}
\section{1}
\begin{equation}
1
\end{equation}
\subsection{1}
\begin{equation}
1
\end{equation}
\subsection{1}
\begin{equation}
1
\end{equation}
\begin{equation}
1
\end{equation}
\section{1}
\begin{equation}
1
\end{equation}
\subsection{1}
\begin{equation}
1
\end{equation}
\section{1}
\begin{equation}
1
\end{equation}
\subsection{1}
\begin{equation}
1
\end{equation}
\end{document}

Result

0

This behaviour was actually changed somewhere back in 2014 (shortly before this question was asked, interestingly enough). When a counter is incremented, subordinate (“within”) counters are now reset recursively. (This is done by first setting the subordinate counters to −1 and then incrementing them.)

With an up to date version of the LaTeX kernel, the MWE in the question now produces equations tagged “(1.0.1)”, “(1.1.1)”, “(1.2.1)”, “(1.2.2)”, “(2.0.1)”, “(2.1.1)”, “(3.0.1)” and “(3.1.1)”, as one would likely expect.


The following was taking from the change log for LaTeX2ε:

24 Changes between LATEX releases 2014/05/01 and 2015/01/01

[…]

24.12 Within counters only reset next level down (pr4393)

This is actually implicitly documented behavior in the LATEX Manual that states that \stepcounter resets all counters marked “within”. However it means that if, for example, theorems are numbered within sections and you start a new chapter in a book, the section counter is reset to zero but the theorem counter is not until the first section appears. Thus a theorem directly within the chapter body (without a new section) would show an incremented number relative to the last theorem of the previous chapter. For this reason we are now resetting all levels of within in one go even if that means that some of these resets may happen several times unnecessarily.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.