2

The structure of my document is such that I generally want to have equation number counters both per section and subsection. Problem is I won't be having subsections inside a certain section and that will introduce extra zeros in the equation numbers within that section. I'm currently using the chngcntr package:

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

Is there anyway to disable equation number within a certain section?

2 Answers2

2

You should just place

\renewcommand{\theequation}{\thesection.\arabic{equation}}

at the start of that section and

\renewcommand{\theequation}{\thesubsection.\arabic{equation}}

before the next. There's no need to redefine whether it'll reset with a \subsection or not, since it's being reset with \section anyway.

Werner
  • 603,163
  • My only concern with this solution is that the numbering for numbers between 1 and 9 now begins with a 0 (eg A.03 rather than A.3). – MauOlivares Sep 19 '19 at 15:59
  • @MauOlivares: Why would that be? \arabic{equation} doesn't print a leading 0. – Werner Sep 19 '19 at 16:27
  • I think the reason for this had to do with the fact that I was combining it with \numberwithin but this is independent of what you stated. It works now but I'm still trying to figure out what went wrong when I was getting that leading 0. – MauOlivares Sep 19 '19 at 17:03
0

A hack with section-subsection redefining:

\documentclass{article}
\usepackage{amsmath}
\numberwithin{equation}{subsection}
\global\let\oldsection\section
\global\let\oldthesubsection\thesubsection
\xdef\oldeqnum{0}

\makeatletter
\def\section{%
\xdef\mdepth{1}
\gdef\thesubsection{\thesection}
\@ifstar{\@Starred}{\@nonStarred}%
}
\def\@Starred{%
\setcounter{equation}{\oldeqnum}
\@ifnextchar[%
{\GenericWarning{}{Warning: A starred section can not have parameters. I am going to ignore them!}\@StarredWith}%
{\@StarredWithout}%
}      
\def\@StarredWith[#1]#2{%
\oldsection*{#2}%
}
\def\@StarredWithout#1{
\oldsection*{#1}%
}
\def\@nonStarred{%
\@ifnextchar[%
{\@nonStarredWith}%
{\@nonStarredWithout}%
}
\def\@nonStarredWith[#1]#2{%
\oldsection[#1]{#2}%
}
\def\@nonStarredWithout#1{%
\oldsection{#1}%
}



\let\oldsubsection\subsection
\def\subsection{%
  \ifnum\mdepth=1
  \xdef\oldeqnum{\the\value{equation}}\fi
  \xdef\mdepth{2}
\gdef\thesubsection{\oldthesubsection}  
\@ifstar{\@Starreds}{\@nonStarreds}%
}
\def\@Starreds{%
\@ifnextchar[%
{\GenericWarning{}{Warning: A starred section can not have parameters. I am going to ignore them!}\@StarredWiths}%
{\@StarredWithouts}%
}      
\def\@StarredWiths[#1]#2{%
\oldsubsection*{#2}%
}
\def\@StarredWithouts#1{
\oldsubsection*{#1}%
}
\def\@nonStarreds{%
\@ifnextchar[%
{\@nonStarredWiths}%
{\@nonStarredWithouts}%
}
\def\@nonStarredWiths[#1]#2{%
\oldsubsection[#1]{#2}%
}
\def\@nonStarredWithouts#1{%
\oldsubsection{#1}%
}
\makeatother

\begin{document}
\section{No subsection here}
\begin{equation}
f(x)=0  
\end{equation}
\begin{equation}
g(x)=0  
\end{equation}
\section{With subsection here after first equation}
\begin{equation}
f(x)=0  
\end{equation}
\subsection{Test subsection 1}
\begin{equation}
g(x)=0  
\end{equation}
\section{With subsection here}
\begin{equation}
F(x)=0  
\end{equation}
\subsection{Test subsection 2}
\begin{equation}
f(x)=0  
\end{equation}
\begin{equation}
g(x)=0  
\end{equation}
\subsection{Test subsection 3}
\begin{equation}
F(x)=0  
\end{equation}
\begin{equation}
f(x)=0  
\end{equation}
\begin{equation}
g(x)=0  
\end{equation}
\begin{equation}
h(x)=0  
\end{equation}
\section*{Continue numbering as section 3}
\begin{equation}
f(x)=0  
\end{equation}
\begin{equation}
  g(x)=0
\end{equation}
\end{document}

Output:

enter image description here

As you can see you can use combinations and can get back to previous section numbering by a starred section

Used this answer of mine:

https://tex.stackexchange.com/a/380116/120578

koleygr
  • 20,105