12

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?

Michiel Borkent
  • 345
  • 4
  • 10

4 Answers4

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

Gonzalo Medina
  • 505,128
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.

Werner
  • 603,163
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!

Schweinebacke
  • 26,336
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.

Michiel Borkent
  • 345
  • 4
  • 10
  • 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