# Reset a counter after another one is increased

I have the following piece of code that implements 2 counters. Currently both counters are increased equally but I would like the second to reset every time the first one is increased automatically.

Currently the counter works like this:

00001.001

00002.002

00003.003

I want it to work like this:

00001.0001

00001.0002

00001.0003

00002.0001

00002.0002

00002.0003

00003.0001

00003.0002

00003.0003

Also I don't know if there is a better implementation besides the two counters placed side by side with a dot in between, but if there is I would like you to mention it.

I would like to mention that the code for the counters in not mine. It was submitted as an answer in a question of mine in here.

\documentclass{article}
\newcounter{Counter}

\newcommand{\Counter}
{%
\textbf
{%
\ifnum\value{Counter}<10000 0\fi
\ifnum\value{Counter}<1000 0\fi
\ifnum\value{Counter}<100 0\fi
\ifnum\value{Counter}<10 0\fi
\arabic{Counter}%
}%
}

\newcounter{InCounter}

\newcommand{\InCounter}
{%
\textbf
{%
\ifnum\value{InCounter}<100 0\fi
\ifnum\value{InCounter}<10 0\fi
\arabic{InCounter}%
}%
}

\newcommand{\Num}
{
\refstepcounter{Counter}\Counter.\refstepcounter{InCounter}\InCounter
}

\begin{document}

\Num

Text

\Num

Text

\Num

Text

\end{document}

• Couldn't you add something to reset InCounter in the definition of the command \Counter? – anderstood Sep 6 '16 at 2:04
• @anderstood I don't know. :/ – Adam Sep 6 '16 at 2:07
• Which counter should be reset? What is the current implementation? That is, what does \Counter and \InCounter represent? – Werner Sep 6 '16 at 5:26
• Well, every occurrence of \Num would step both counters; if you reset InCounter when Counter is stepped, the end result would be always getting 1 as the value of InCounter: not really useful, is it? Could you provide a better example of use? – egreg Sep 6 '16 at 8:43
• @egreg Sure but I don't really understand what example you want. :/ – Adam Sep 6 '16 at 18:39

Define the second counter as bound to the first one and set properly its representation.

\documentclass{article}

\newcounter{maincounter}
\newcounter{childcounter}[maincounter]

\renewcommand{\themaincounter}{%
\ifnum\value{maincounter}<10000 0\fi
\ifnum\value{maincounter}<1000 0\fi
\ifnum\value{maincounter}<100 0\fi
\ifnum\value{maincounter}<10 0\fi
\arabic{maincounter}%
}
\renewcommand{\thechildcounter}{%
\themaincounter.%
\ifnum\value{childcounter}<1000 0\fi
\ifnum\value{childcounter}<100 0\fi
\ifnum\value{childcounter}<10 0\fi
\arabic{childcounter}
}

\newcommand{\stepmain}{\stepcounter{maincounter}}
\newcommand{\stepchild}{\refstepcounter{childcounter}}
\newcommand{\num}{\stepchild\thechildcounter}

\begin{document}

\stepmain

\num

\num

\num

\stepmain

\num

\num

\stepmain

\num

\num

\end{document}


At each \stepmain the main counter is stepped and the child counter is reset; \num will just step the child counter and print the value. I use \refstepcounter here so \label will point to the correct value.

The standard way is like this:

\newcounter {secondcounter} [firstcounter]


and secondcounter will automatically reset every time firstcounter is increased.

• It doesn't work. It only increases the second counter. – Adam Sep 6 '16 at 4:12
• Did you try the standard way? \newcounter {second} [first] – n.r. Sep 6 '16 at 4:31
• You should add the qualifier that secondcounter is reset only if firstcounter is incremented via \stepcounter or \refstepcounter. An operation such as \addtocounter{firstcounter}{1} does not reset secondcounter. – Mico Sep 6 '16 at 4:54