9

I defined a counter like this:

\newcounter{my_steps}[subsubsection]

and I would like to make it start with 1 instead of 0 after each subsubsection. How?

2
  • I guess you can wrap the subsubsection command and \setcounter{counter}{value} into a new command. – percusse Nov 22 '11 at 12:16
  • good idea, I'll go for that one – Michiel Borkent Nov 22 '11 at 12:50
5

Using the etoolbox package you can patch the \subsubsection command so that the counter is set to 1 with every use of \subsubsection:

\documentclass{article}
\usepackage{etoolbox}

\newcounter{myc}[subsubsection]
\patchcmd{\subsubsection}{\bfseries}{\bfseries\setcounter{myc}{1}}{}{}

\begin{document}

\subsubsection{Test}
\themyc
\subsubsection{Test}
\themyc

\end{document}

enter image description here

3

If the counter mycount (say) is only used for display purposes, you could also just modify the display part of it - \themycount:

enter image description here

\documentclass{article}
\newcounter{mycount}[subsubsection]% mycount is slave to master subsubsection
\renewcommand{\themycount}{\number\numexpr\value{mycount}+1\relax}% Display mycount+1
\begin{document}
\noindent \themycount \par
\noindent \stepcounter{mycount}\themycount
\subsubsection{A subsubsection}
\noindent \themycount \par
\noindent \stepcounter{mycount}\themycount
\subsubsection{Another subsubsection}
\noindent \themycount
\end{document}

As mentioned, this is only functional in its display, since using \setcounter{mycount}{10} (say), will print 11 when using \themycount.

2

I kind of ugly trick, but maybe usefull my be the manipulation of the clear list of counter subsubsection:

\documentclass{article}
\newcounter{mysteps}[subsubsection]
\makeatletter
\g@addto@macro{\cl@subsubsection}{\stepcounter{mysteps}}% clear list manipulation
\makeatother
\begin{document}
\section{Section}
\subsection{Subsection}
\subsubsection{Subsubsection}
mysteps is \themysteps.
Adding 10 to mysteps: \addtocounter{mysteps}{10}.
mysteps is \themysteps.

\subsubsection{Subsubsection}
mysteps is \themysteps.

\end{document}

Caveat: If you're using package chngcntr you have to do the manipulation of the clear list after all \counterwithin and \counterwithout!

1

After reading percusse's comment I decided to go with the following:

\newcounter{stappen}
\newcommand{\stepreset}{\setcounter{stappen}{0}}
\newcommand{\step}{\stepcounter{stappen} \textbf{Stap \arabic{stappen}} }

I re-use the counter during the document and reset it when needed with \stepreset. This is more flexible than bind it to the subsubsection counter. Every time I need a step printed I just type: \step.

1
  • You could also use \refstepcounter{stappen} instead of \stepcounter{stappen} if you want to be able to reference your steps (using \label and \ref). – Werner Dec 10 '11 at 21:04

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.