I've redefined the \theequation
with some query about the value of the counters:
\renewcommand{\theequation}{%
\ifnum\value{subsection} > 0
\ifnum\value{subsubsection} > 0
\thesubsubsection-\arabic{equation}
\else
\thesubsection-\arabic{equation}
\fi
\else
\thesection-\arabic{equation}
\fi
}
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
\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 ,
pars
kip = 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}{subsubsection} % Reset with \subsubsection
\let\origtheequation\theequation
%
\renewcommand{\theequation}{%
\ifnum\value{subsection} > 0
\ifnum\value{subsubsection} > 0
\thesubsubsection-\arabic{equation}
\else
\thesubsection-\arabic{equation}
\fi
\else
\thesection-\arabic{equation}
\fi
}
% Section numbering: Table of contents and 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}
\begin{equation}
x
\end{equation}
\begin{equation}
x
\end{equation}
\subsection{First sub}
\begin{equation}
x \label{first}
\end{equation}
\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}
\clearpage
\section{Item II}
\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}
\clearpage
\section{Item IV}
\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}
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.
\let\origtheequation\theequation
\newcommand{\formatsubstitute}[1]{%
\csname the#1\endcsname-\arabic{equation}%
}
\newtoggle{enableFormat}
\newcommand{\myequationformat}{%
\ifnum\value{subsection} > 0
\ifnum\value{subsubsection} > 0
\ifnum\value{paragraph} > 0
\ifnum\value{subparagraph} > 0
\formatsubstitute{subparagraph}
\else % it's paragraph
\formatsubstitute{paragraph}
\fi
\else % it's subsubsection
\textbf{\formatsubstitute{subsubsection}}
\fi
\else % it's subsection
\formatsubstitute{subsection}
\fi
\else % it's section
\textbf{\formatsubstitute{section}}
\fi
}
\newcommand{\toggleformat}{%
\iftoggle{enableFormat}{%
\counterwithin{equation}{section} % Reset with \subparagraph
\let\theequation\origtheequation%
\togglefalse{enableFormat}%
}{%
\counterwithin{equation}{subparagraph} % Reset with \subparagraph
\let\theequation\myequationformat%
\toggletrue{enableFormat}%
}%
}
Here's a rather lengthy testing code:
\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:
\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}
\let\origtheequation\theequation
\newcommand{\formatsubstitute}[1]{%
\csname the#1\endcsname-\arabic{equation}%
}
\newtoggle{enableFormat}
\newcommand{\myequationformat}{%
\ifnum\value{subsection} > 0
\ifnum\value{subsubsection} > 0
\ifnum\value{paragraph} > 0
\ifnum\value{subparagraph} > 0
\formatsubstitute{subparagraph}
\else % it's paragraph
\formatsubstitute{paragraph}
\fi
\else % it's subsubsection
\textbf{\formatsubstitute{subsubsection}}
\fi
\else % it's subsection
\formatsubstitute{subsection}
\fi
\else % it's section
\textbf{\formatsubstitute{section}}
\fi
}
\newcommand{\toggleformat}{%
\iftoggle{enableFormat}{%
\counterwithin{equation}{section} % Reset with \subparagraph
\let\theequation\origtheequation%
\togglefalse{enableFormat}%
}{%
\counterwithin{equation}{subparagraph} % Reset with \subparagraph
\let\theequation\myequationformat%
\toggletrue{enableFormat}%
}%
}
% Section numbering: Table of contents and 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 0}
\begin{equation}
x
\end{equation}
\begin{equation}
x
\end{equation}
\subsection{First sub}
\begin{equation}
x \label{first}
\end{equation}
\subsubsection{First subsub}
\begin{equation}
x \label{second}
\end{equation}
\subsubsubsection{First paragraph}
\begin{equation}
x \label{third}
\end{equation}
\subparagraph{First subparagraph}
\begin{equation}
x \label{fourth}
\end{equation}
\toggleformat
\section{Item I}
\begin{equation}
x
\end{equation}
\begin{equation}
x
\end{equation}
\subsection{First sub}
\begin{equation}
\end{equation}
\subsubsection{First subsub}
\begin{equation}
x
\end{equation}
\subsubsubsection{First paragraph}
\begin{equation}
x
\end{equation}
\subparagraph{First subparagraph}
\begin{equation}
x
\end{equation}
\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}
\clearpage
\section{Item II}
\begin{equation}
x \label{fifth}
\end{equation}
\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}
\clearpage
\toggleformat
\section{Item IV}
\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 would recommend using the
\tag
macro to directly insert the equation number.As per @barbarabeeton 's answer, the
'
should be\prime
as:Incidentally, the
\theequation
is the way to access directly the formattedequation
counter. This works with any counter you're using,\the<counter_name>
will call the formatted counter. This is the reason\theequation
resulted in1.1
and not just1
, which is the value of the counterequation
.