I would like to use section numbers within equation reference counters.
This typically requires \numberwithin
from the amsmath
package.
I would like to change the section number depth of these references mid-document.
This instead requires the \counterwithin
commands from the chngcntr
package.
Problem:
I would like to change the syntax of the \counterwithin
counter output, such that the symbol past the deepest section depth is a hyphen rather than a period:
- Actual: [Section] . [Subsection] . [Subsubsection] . [EquationNumber]
- Desired: [Section] . [Subsection] . [Subsubsection] – [EquationNumber]
Important Note:
It is intended that the section depth of the equation numbering scheme changes accordingly with the depth of the level. Thus, I plan to have something like:
- Equation 1.1-1
- Equation 1.2-1
- Equation 1.3.1-1
- Equation 1.4-1
- Equation 2-1
- Equation 3.1-1
Checking the documentation:
The chngcntr
package documentation is pretty weak.
The amsmath
documentation [pg.9] states that simply renewing the counter code:
\renewcommand{\theequation}{\thesection–\arabic{equation}}
creates problems by not resetting. Additionally, this code is not a catch all, as section depth changes throughout the document, making a catch-all type of command much more difficult.
MWE Description:
In the code, the code within the user-created command \equationNumbering
does not seem to have an easy method to remove the .
after \thesubsubsection
(or whichever section is deepest) and add a hyphen in its place.
MWE, Updated with Local Answer from Hupfen and Answer from Werner from Here:
\documentclass{scrartcl}
\usepackage{geometry} % margin/page layout settings
\usepackage{scrlayer-scrpage} % improved header commands. [supercedes `fancyhdr' package].
\usepackage{mathtools} % includes amsmath, supplements it.
\usepackage{chngcntr} % allows changing equation section depth mid-document
\usepackage{etoolbox}
% Margin Settings:
\KOMAoptions{fontsize = 12pt ,
parskip = half- ,
headheight = 1.000em ,
footheight = 2.700em ,
DIV = current }
\geometry{letterpaper ,
hmargin = 0.750in ,
tmargin = 0.750in ,
bmargin = 0.750in ,
headsep = 1.000em ,
footskip = 3.700em } % [ = Footheight + Footsep]
%Initialize headers and footers
\chead{\normalfont Header 1 \\ Header 2}
\cfoot{\normalfont Footer 1 \\ Footer 2}
\ofoot{\normalfont Page \thepage}
% Section numbering: Format \paragraph like \subsection
\newcommand{ \subsubsubsection} [1] { \paragraph{#1} }
\newcommand{\subsubsubsubsection} [1] { \subparagraph{#1} }
\setcounter{secnumdepth}{5}
\makeatletter
\renewcommand{\paragraph} %
{\@startsection{paragraph}{4}{\z@} %
{-2.5ex\@plus -1ex \@minus -.25ex} %
{1.25ex \@plus .25ex} %
{\normalfont\sffamily\normalsize\bfseries} }
\renewcommand{\subparagraph} %
{\@startsection{subparagraph}{4}{\z@} %
{-2.5ex\@plus -1ex \@minus -.25ex} %
{1.25ex \@plus .25ex} %
{\normalfont\sffamily\normalsize\bfseries} }
\makeatother
\let\origtheequation\theequation
% Equation Numbering
\counterwithin*{equation}{section} % Reset equation at \section
\counterwithin*{equation}{subsection} % Reset equation at \subsection
\counterwithin*{equation}{subsubsection} % Reset equation at \subsubsection
\counterwithin*{equation}{paragraph} % Reset equation at \paragraph
\counterwithin*{equation}{subparagraph} % Reset equation at \subparagraph
\newcommand{\xequationFormat} {\determineSection-\arabic{equation}}
\newcommand{\determineSection}{
\ifnum\value{subsection} > 0
\ifnum\value{subsubsection} > 0
\ifnum\value{paragraph} > 0
\ifnum\value{subparagraph} > 0 \thesubparagraph
\else \theparagraph \fi
\else \thesubsubsection \fi
\else \thesubsection \fi
\else \thesection \fi
}
\begin{document}
\setcounter{section}{-1}
\let\theequation\origtheequation
\section{Level 1}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsection{Level 2}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsubsection{Level 3}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsubsubsection{Level 4}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subparagraph{Level 5}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\clearpage
\let\theequation\xequationFormat
\section{Level 1}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsection{Level 2}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsubsection{Level 3}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsubsubsection{Level 4}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subparagraph{Level 5}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\clearpage
\section{Level 1}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsection{Level 2}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsubsection{Level 3}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsubsubsection{Level 4}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subparagraph{Level 5}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\end{document}
Original MWE:
\documentclass{scrartcl}
\usepackage{geometry} % margin/page layout settings
\usepackage{scrlayer-scrpage} % improved header commands. [supercedes `fancyhdr' package].
\usepackage{mathtools} % includes amsmath, supplements it.
\usepackage{chngcntr} % allows changing equation section depth mid-document
% Margin Settings:
\newlength{\xhmargin } \setlength{\xhmargin }{0.750in}
\newlength{\xtmargin } \setlength{\xtmargin }{0.750in}
\newlength{\xbmargin } \setlength{\xbmargin }{0.750in}
\newlength{\xheadheight} \setlength{\xheadheight}{2.700em}
\newlength{\xheadsep } \setlength{\xheadsep }{1.000em}
\newlength{\xfootheight} \setlength{\xfootheight}{2.700em}
\newlength{\xfootskip } \setlength{\xfootskip }{3.700em} % [ = Footheight + Footsep]
\KOMAoptions{fontsize = 12pt ,
parskip = half- ,
headheight = \xheadheight ,
footheight = \xfootheight ,
DIV = current }
\geometry{letterpaper ,
hmargin = \xhmargin ,
tmargin = \xtmargin ,
bmargin = \xbmargin ,
headsep = \xheadsep ,
footskip = \xfootskip }
%Initialize headers and footers
\chead{\normalfont Header 1 \\ Header 2}
\cfoot{\normalfont Footer 1 \\ Footer 2}
\ofoot{\normalfont Page \thepage}
\counterwithin{equation}{section}
\newcommand{\equationNumbering} [2] { \counterwithout{equation}{#1}\counterwithin{equation}{#2} }
% syntax: \equationNumbering{oldSectionDepth}{newSectionDepth}
% Section numbering: Section depth
\setcounter{secnumdepth}{5}
% Section numbering: Format \paragraph like \subsection
\newcommand{\subsubsubsection} [1] { \paragraph{#1} }
\makeatletter
\renewcommand{\paragraph} %
{\@startsection{paragraph}{4}{\z@} %
{-2.5ex\@plus -1ex \@minus -.25ex} %
{1.25ex \@plus .25ex} %
{\normalfont\sffamily\normalsize\bfseries} }
\makeatother
\begin{document}
\section{Item I}
\equationNumbering{section}{subsubsection}
\clearpage
\subsection{A}
\subsubsection{x}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsubsection{y}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsubsection{z}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\clearpage
\subsection{B}
\subsubsection{x}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsubsection{y}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsubsection{z}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\equationNumbering{subsubsection}{section}
\clearpage
\section{Item II}
\equationNumbering{section}{paragraph}
\clearpage
\subsection{Item IIa}
\clearpage
\subsubsection{A}
\subsubsubsection{x}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsubsubsection{y}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsubsubsection{z}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\clearpage
\subsubsection{B}
\subsubsubsection{x}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsubsubsection{y}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsubsubsection{z}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\clearpage
\subsubsection{C}
\subsubsubsection{x}
\subsubsubsection{y}
\subsubsubsection{z}
\clearpage
\subsection{Item IIb}
\clearpage
\subsubsection{A}
\subsubsubsection{x}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsubsubsection{y}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsubsubsection{z}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\clearpage
\subsubsection{B}
\subsubsubsection{x}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsubsubsection{y}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsubsubsection{z}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\clearpage
\subsubsection{C}
\subsubsubsection{x}
\subsubsubsection{y}
\subsubsubsection{z}
\equationNumbering{paragraph}{section}
\clearpage
\section{Item IV}
\equationNumbering{section}{subsection}
\clearpage
\subsection{x}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsection{y}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\subsection{z}
\begin{equation} x \end{equation}
\begin{equation} x \end{equation}
\end{document}
Best Answer
I've redefined the
\theequation
with some query about the value of the counters:I changed the resetting of the equation counter to
subsubsection
.This won't work if a
\subsubsection
appears without\subsection
before.Here is the full code
Edit: New version
The following code uses a
toggle
and a command\toggleformat
to switch between the both formats, i.e. the standard as\origtheequation
provides and the user defined, hidden in a wrapper command named\myequationformat
.The resetting is switched back to standard
section
level too.Here's a rather lengthy testing code: