Equation with sectioning numbering problem

I need special numbering for equations in my work, so I made it similar to figures numbering which egreg wrote here

This is my MWE:

\documentclass[12pt, a4paper, oneside, onecolumn]{article}
\usepackage{mathtools}
\renewcommand{\theequation}{%
\ifnum\value{subsection}=0
\thesection.\arabic{figure}%
\else
\ifnum\value{subsubsection}=0
\thesubsection.\arabic{figure}%
\else
\thesubsubsection.\arabic{figure}%
\fi
\fi
}
\begin{document}
\section{Section}
\begin{equation}
\begin{split}
R' &= R/255 \\
G' &= G/255 \\
B' &= B/255
\end{split}
\end{equation}

\subsection{Subsection}
\begin{equation}
\begin{split}
C_{max} &= max(R', G', B') \\
C_{min} &= min(R', G', B') \\
\Delta &= C_{max} - C_{min}
\end{split}
\end{equation}

\end{document}

It gives me this type of numbering, which is good, but the number of equation is wrong: If I use it in my document it looks like this: The 2.1.2. part is fine, that's the number of my subsubsection, but the rest is wrong. It should be 2.1.2.1 and 2.1.2.2 Could you tell me what have I done wrong here?

• you want {equation} not {figure} in each case, you are incrementing the equation counter then printing the figure one, change to print the equation counter – David Carlisle Sep 14 '13 at 18:33

There are two separate issues with the code in your MWE:

• You need to affix \arabic{equation}, not \arabic{figure}, to the various redefinitions of \theequation. This has already been pointed out by David Carlisle. (This is why the first couple of equation numbers terminate in .0. My guess is that your document has two figure environments prior to the second batch of equations you show; it's the most straightforward explanation of why the final digit of both equation numbers is stuck at .2.)

• You're missing the following statements (adapted from @egreg's earlier answer):

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

If you don't include these commands, the final digit of the composite equation "number" will happily increase from 1 to 2 to 3 and so on regardless of the sectioning level the equations happen to be in.

An off-topic comment: Don't typeset "max" and "min" in plain math-italic mode. Use \max and \min where they occur as math operators, and write C_{\textup{max}} and C_{\textup{min}} for the subscript instances. If you have a lot of those instances, you define some macros in the preamble, say \newcommand{\Cmax}{C_{\textup{max}}}, and use \Cmax throughout the document.