Reset equation counters with unnumbered sections
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}
Related videos on Youtube
jfernandz
Updated on July 05, 2020Comments
-
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 over 7 yearsPersonally, I would not use this approach ....
-
jfernandz over 7 yearsYes, it was just switch
\addtocounter{section}{1}
with\stepcounter{section}
what I did need. Now works how I want, thank you. :) -
jfernandz over 7 yearsMainly 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 over 7 years@JFernan That's a very wrong way to go.
-
jfernandz over 7 yearsWell, 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 over 7 years@JFernan It would be much better to open a new question, with a full specification of your needs.