5

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.

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

1 Answer 1

7

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

2
  • 1
    what about when using totcount packages to get the total of subcounters ? It isn't supported FTM...
    – 3isenHeim
    Aug 17, 2020 at 13:16
  • Note: Package chngcntr is no longer needed. \counterwithin and \counterwithout and the star forms \counterwithin* and \counterwithout* are provided the by LaTeX kernel from 2018-04-01. See ltnews28.pdf.
    – cabohah
    Jan 18 at 7:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .