# Reset equation counters with unnumbered sections

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!

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

\newcommand{\sect}[1]{%
\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}%
}


will do.

• Yes, it was just switch \addtocounter{section}{1} with \stepcounter{section} what I did need. Now works how I want, thank you. :) – JFernan Jul 5 '16 at 10:06
• @JFernan That's a very wrong way to go. – egreg Jul 5 '16 at 11:13
• 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). – JFernan Jul 5 '16 at 11:15
• @JFernan It would be much better to open a new question, with a full specification of your needs. – egreg Jul 5 '16 at 11:19

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}

$$E=mc^2 \label{eqeinstein}$$

$$E=mc^2 \label{eqeinsteinother}$$

\clearpage
\section*{Foobar}

$$E=mc^2 \label{eqeinsteinagain}$$

$$E=mc^2 \label{eqyeteinstein}$$

\end{document}


• Personally, I would not use this approach .... – user31729 Jul 4 '16 at 21:30