2

I have created the following counters.

\documentclass{article}

\newcounter{CounterOne}

\newcounter{CounterTwo}[CounterOne]

\setcounter{CounterTwo}{1}

\newcommand{\One}{\stepcounter{CounterOne}{\textbf{\theCounterOne.\theCounterTwo}}}

\newcommand{\Two}{\stepcounter{CounterTwo}{\textbf{\theCounterOne.\theCounterTwo}}}

\begin{document}

\One

\One

\Two

\Two

\end{document}

Everything works fine except that the second counter doesn't start from 1 as I have set. Why is that? How can I fix it?

3

You probably don't understand the meaning of \newcounter then. It has the following interface (see source2e, section 21.1 Environment Counter Macros, p 133):

\newcounter{<newctr>}[<oldctr>]
Defines <newctr> to be a counter, which is reset when counter <oldctr> is stepped.

The important part here is that <newctr> is reset when <oldctr> is stepped. Just remove the optional <oldctr>:

1.1
2.1
2.2
2.3

\documentclass{article}

\newcounter{CounterOne}

\newcounter{CounterTwo}%[CounterOne]

\setcounter{CounterTwo}{1}

\newcommand{\One}{\stepcounter{CounterOne}{\textbf{\theCounterOne.\theCounterTwo}}}

\newcommand{\Two}{\stepcounter{CounterTwo}{\textbf{\theCounterOne.\theCounterTwo}}}

\begin{document}

\One

\One

\Two

\Two

\end{document}
1

Your \setcounter is just placed wrong. It should be in your \One after the \stepcounter for CounterOne, because you need to set it at every reset of your first counter (TeX starts at 0).

\documentclass{article}

\newcounter{CounterOne}

\newcounter{CounterTwo}[CounterOne]


\newcommand{\One}{\stepcounter{CounterOne}\setcounter{CounterTwo}{1}{\textbf{\theCounterOne.\theCounterTwo}}}

\newcommand{\Two}{\stepcounter{CounterTwo}{\textbf{\theCounterOne.\theCounterTwo}}}

\begin{document}

\One

\One

\Two

\Two

\end{document}

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.