3

I created a counter, call it ncfor newcounter:

\newcounter{nc}

I want to create a "subcounter", let me call it snc for sub new counter, whose behavior mimics the way subsection is a "subcounter" to section. How do I do this?

If it is not clear what I am asking, here is an example: if the value of nc is 4, then I want the value of snc to be 4.0, and the next call to snc to be 4.1, etc.

This is surely well known but I searched briefly and couldn't find a clear answer.

  • The term subcounter isn't really correct. section is the resetting counter of subsection. – user31729 Mar 13 '17 at 18:27
4

Counters that should be reset from other counters are usually defined with \newcounter{snc}[nc] where nc is the resetting counter.

However, this does not provide the requested format, i.e. nc.snc, so \renewcommand{\thesnc}{\thenc.\arabic{snc} is needed.

Counters itself can't have a floating point value like 1.1 -- their output is a combination of some counter values which is defined in the relevant \the... macro.

\documentclass{article}

\newcounter{nc}
\newcounter{snc}[nc]

\renewcommand{\thesnc}{\thenc.\arabic{snc}}

\begin{document}

\setcounter{nc}{4}

\thenc\ and \thesnc

\stepcounter{snc} 

Now snc has the value \thesnc. 

Let's step nc and look what happens:\stepcounter{nc}

Now snc has the value \thesnc.
\end{document}

With the chngcntr package this can be simplified a little bit, i.e. for example if [nc] has been forgotten in the definition of the snc counter, this and the formatting change can be done with the command \counterwithin{snc}{nc}. However, \newcounter{scn} is still necessary.

\documentclass{article}

\usepackage{chngcntr}

\newcounter{nc}
\newcounter{snc}

\counterwithin{snc}{nc}

\begin{document}

\setcounter{nc}{4}

\thenc\ and \thesnc

\stepcounter{snc} 

Now snc has the value \thesnc. 

Let's step nc and look what happens:\stepcounter{nc}

Now snc has the value \thesnc.
\end{document}

Both styles give following output:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.