Reset equation counters with unnumbered sections

1,019

Solution 1

Counters bound to other counters are reset when the master counter is “stepped” with \stepcounter. So I guess that

\newcommand{\sect}[1]{%
  \stepcounter{section}\addtocounter{section}{-1}%
  \section*{#1}%
  \addcontentsline{toc}{section}{#1}%
}

is what you really want. Unless you want that

\section{A}

\sect{B}

\section{C}

gives 3 as the section number for “C” like it seems your code does now. In that case,

\newcommand{\sect}[1]{%
  \stepcounter{section}%
  \section*{#1}%
  \addcontentsline{toc}{section}{#1}%
}

will do.

Solution 2

One can argue about this approach:

A reset equation in each \section* is without information which section is meant actually, so cross-referencing might become difficult, unless hyperref is used with correct linking.

In this solution I applied \xpretocmd for \section to reset the equation counter (which is probably done anyway), so both for \section and \section*.

The sectcont counter is just for making a unique hyperref anchor.

\documentclass{article}

\usepackage{chngcntr}

\counterwithout*{equation}{section}

\usepackage{xpatch}

\newcounter{sectcont}

\xpretocmd{\section}{%
  \stepcounter{sectcont}%
  \setcounter{equation}{0}%
}{}{}

\usepackage{hyperref}

\AtBeginDocument{%
  \renewcommand{\theHequation}{equation.\thesectcont.\theequation}
}

\begin{document}

In \ref{eqeinsteinagain} we see...

\clearpage

\section*{Foo}

\begin{equation}
  E=mc^2 \label{eqeinstein}
\end{equation}


\begin{equation}
  E=mc^2 \label{eqeinsteinother}
\end{equation}


\clearpage
\section*{Foobar}

\begin{equation}
  E=mc^2 \label{eqeinsteinagain}
\end{equation}


\begin{equation}
  E=mc^2 \label{eqyeteinstein}
\end{equation}


\end{document}

enter image description here

Share:
1,019

Related videos on Youtube

jfernandz
Author by

jfernandz

Updated on July 05, 2020

Comments

  • jfernandz
    jfernandz over 3 years

    I would like use:

    \counterwithin*{equation}{section}
    \counterwithin*{equation}{subsection}
    

    with \section*{} because if the section is not numbered \counterwithin*{equation}{section} doesn't work properly, in fact, I'm using this command to do unnumbered sections even in table of contents:

    \newcommand{\sect}[1] {\section*{#1}\addcontentsline{toc}{section}{#1}\addtocounter{section}{1}}
    

    But how I said ... I cannot number properly my equations because counters are not restored to a new unnumbered sections.

    So much thanks!

  • Admin
    Admin over 7 years
    Personally, I would not use this approach ....
  • jfernandz
    jfernandz over 7 years
    Yes, it was just switch \addtocounter{section}{1} with \stepcounter{section} what I did need. Now works how I want, thank you. :)
  • jfernandz
    jfernandz over 7 years
    Mainly because I use this \newcommand{\seclabel}[1] {\numberwithin{equation}{section}\label{#1}} \newcommand{\sseclabel}[1] {\numberwithin{equation}{subsection}\label{#1}} To tag equations what I want.
  • egreg
    egreg over 7 years
    @JFernan That's a very wrong way to go.
  • jfernandz
    jfernandz over 7 years
    Well, I don't want to enumerate all of my equations, only a few concretely. Only which are in cross-reference by labels, specifically. And this way allow me tag only a few equations with properly numeration (referencing its subsection and section even when section is unnumbered).
  • egreg
    egreg over 7 years
    @JFernan It would be much better to open a new question, with a full specification of your needs.