6

I am dogmatic, and I believe that 0 is the very zerost number. Naturally, I would like all my counters to start at 0. Page counters, section counters, subsection counters, theorems, definitions -- everything! -- if it counts, it starts at 0.

I know how I can make a section counter start at 0: Getting section numbering to start at 0

But how do I efficiently satisfy my dogma without having to write many \setcounter{<...>}?

  • 1
    By default, a \newcounter command initalizes any counter with 0. The package totcount does initialize total counters however with -1. If somebody did not make errors in the class/package setup, this should not be a real issue. If there's a proper \newcounter{foo}[bar] statement, the counters on the reset list are set to zero as soon as the driver counter is increased with \stepcounter or \refstepcounter. – user31729 Aug 17 '16 at 21:19
  • 4
    You are wrong, I believe. The page number is zero until you look at the first page, which of course has page number 1. – egreg Aug 17 '16 at 21:20
  • @ChristianHupfer I am having a bit of trouble understanding your comment. The totcount package initializes total counters with -1, and if I understand correctly, the "total counters" are an addition of that package to count up the number of items of a thing? If yes, then what is the relevance/connection of total counters to counters, especially given the original question? – user89 Aug 17 '16 at 21:26
  • @user89: Just in case you're using the totcount package. – user31729 Aug 17 '16 at 21:29
  • The page counter starts at -1 because I set it to the list explicitly. – user31729 Aug 18 '16 at 8:03
9

enter image description here

\documentclass{article}
\makeatletter
\def\@arabic#1{\the\numexpr(#1)-1\relax} 
\def\@roman#1{\romannumeral\numexpr(#1)-1\relax}
\def\@Roman#1{\expandafter\@slowromancap\romannumeral\numexpr(#1)-1\relax @}
\makeatother

\begin{document}


    \section{Zero}
        \subsection{Zero.Zero}
            \subsubsection{Zero.Zero.Zero}

    \section{One}
        \subsection{One.Zero}
            \subsubsection{One.Zero.Zero}

    \section{Two}
        \subsection{Two.Zero}
            \subsubsection{Two.Zero.Zero}


\end{document}
  • What is this arcane magic? It works with page numbers too! It works with figures! It works with amsmath and amsthm things! – user89 Aug 18 '16 at 15:51
  • 1
    @user89 you said you wanted it to apply to all counters. (it just changes the counter print commands so a counter prints as one less than its value. ) – David Carlisle Aug 18 '16 at 15:53
  • David, yeah, it does exactly what I wanted :) – user89 Aug 18 '16 at 15:53
3

You can use \LoopResetCounters from xassoccnt which sets all counters given in the comma-separated list to zero.

See the update for grabbing all counters defined with \newcounter and put them on a list at the end of this answer!

\documentclass{article}


\usepackage{xassoccnt}

\begin{document}

\setcounter{figure}{17}

\thefigure

\LoopResetCounters{page,section,subsection,figure,equation,subsubsection,table}

\thefigure

\end{document}

Update

I've grabbed the comment by David Carlisle and exploited the \cl@@ckpt list in order to reset the all counters to -1, but omitted the page counter there!

In order to make the resetting list work, the \@stpelt list must be changed at all.

No additional package is needed at all!

This procedure does not influence the secnumdepth and tocdepth counters since those are not defined with \newcount.

\documentclass{article}



\makeatletter
\newif\if@resetall
\@resetallfalse

\let\latex@newcounter\newcounter
\newcommand{\ResetAllCounters}[1][-1]{%
  \EnableResetAll
  \def\@elt##1{%
    \def\@tmp@@a{##1}
    \def\@tmp@@b{page}
    \ifx\@tmp@@a\@tmp@@b % Check if it is the page counter
    \setcounter{##1}{\z@}%
    \else
    \setcounter{##1}{#1}%
    \fi
  }%
  \cl@@ckpt% Loop through the list of all counters defined with \newcounter
  \gdef\@@resetvalue{#1}% Store the reset value
}

% Redefine the reset stepper \@elt - list marker
\let\latex@@stpelt\@stpelt

\def\@stpelt#1{%
  \if@resetall%
  \global\csname c@#1\endcsname \numexpr\@@resetvalue-1\stepcounter{#1}
  \else
  \latex@@stpelt{#1}%
  \fi
}%

\newcommand{\DisableResetAll}{%
  \global\@resetallfalse
}

\newcommand{\EnableResetAll}{%
  \global\@resetalltrue%
}

\setcounter{secnumdepth}{5}
\setcounter{tocdepth}{5}

\makeatother

\begin{document}
\tableofcontents
\part{Foo}

\section{Foo}
\ResetAllCounters
\part{Foostuff}
\section{Foostuff}
\subsection{Foostuff subsection}
\subsubsection{Foostuff subsubsection}
\paragraph{Foostuff paragraph}
\subparagraph{Foostuff subparagraph}


\part{Foobar}
\section{Foobar}
\subsection{Foobar subsection}
\subsubsection{Foobar subsubsection}
\paragraph{Foobar paragraph}
\subparagraph{Foobar subparagraph}


\ResetAllCounters
\DisableResetAll

\part{Barstuff}
\section{Barstuff}
\subsection{Barstuff subsection}
\subsubsection{Barstuff subsubsection}
\paragraph{Barstuff paragraph}
\subparagraph{Barstuff subparagraph}


\end{document}

enter image description here

  • I would have to populate the comma separated list manually though? – user89 Aug 17 '16 at 21:17
  • @user89: Yes, at the current state of the package. I did explicitly no changes to the \newcounter command (I am the author of xassoccnt by the way) – user31729 Aug 17 '16 at 21:20
  • I noticed you're the author (I looked up the package)! Is there no way to change the default value at which counters are initialized to -1? – user89 Aug 17 '16 at 21:29
  • 1
    @user89: Either use \LoopSetCounter{foo,foobar,section}{-1} or change the 0 in my second answer! – user31729 Aug 17 '16 at 21:33
  • 2
    latex already has a list of all defined counters, you don't need to redefine \newcounter to grab that see \show\cl@@ckpt – David Carlisle Aug 17 '16 at 22:22

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.