0

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!

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.

  • 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
  • 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. – JFernan Jul 5 '16 at 11:11
  • @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
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

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

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.