8

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{<...>}?

bzm3r
  • 3,196
  • 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. –  Aug 17 '16 at 21:19
  • 5
    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? – bzm3r Aug 17 '16 at 21:26
  • @user89: Just in case you're using the totcount package. –  Aug 17 '16 at 21:29
  • The page counter starts at -1 because I set it to the list explicitly. –  Aug 18 '16 at 08:03

2 Answers2

10

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}\label{ooo}
    \subsection{One.Zero}
        \subsubsection{One.Zero.Zero}\label{aaa}

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


See sections \ref{ooo}, \ref{aaa} and \ref{bbb}. \end{document}

David Carlisle
  • 757,742
  • What is this arcane magic? It works with page numbers too! It works with figures! It works with amsmath and amsthm things! – bzm3r 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 :) – bzm3r Aug 18 '16 at 15:53
  • I've found a minor issue with this. The reference list is printed starting from zero, which is nice, but along the text, the citations aren't synced: for instance, what is listed as [2] in the bibliography is mentioned as [3] along the text. – Renan Mezabarba Aug 19 '23 at 00:13
  • @RenanMezabarba ref works, I added an example. If \cite doesn't I think your bibstyle is doing something wrong, you could ask a new question. – David Carlisle Aug 19 '23 at 00:26
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? – bzm3r 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) –  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? – bzm3r Aug 17 '16 at 21:29
  • @user89: Why should they are be initialized to -1 now? You asked for 0. And see the update please –  Aug 17 '16 at 21:30
  • Sorry, I might be confusing myself. According to this answer here: http://tex.stackexchange.com/questions/107470/getting-section-numbering-to-start-at-0, if I want section numbering to start at $0$, I should initialize the counter at $-1$. That way, when it gets incremented during the zerost ("first") use, it will use the value $-1 + 1 = 0$. By default, the counters are initialized at $0$, so that at the first use, they are incremented so that $0 + 1 = 1$? – bzm3r Aug 17 '16 at 21:32
  • I just noticed your edits too, so I will try to read the latest and understand that; you may have already answered my question! – bzm3r Aug 17 '16 at 21:33
  • 1
    @user89: Either use \LoopSetCounter{foo,foobar,section}{-1} or change the 0 in my second answer! –  Aug 17 '16 at 21:33
  • Christian, thanks in advance for your patience here. I played around with the second example in your answer (one change I made was that I removed the repetition of \blinddocument, and I simply have \showallcounters \resetallcounters \blinddocument within the \begin{document} ... \end{document}), and set the counters initially to -1; however this only changes the page number counter, and not the section/subsection counter. Do you notice the same thing? We are getting very close though! – bzm3r Aug 17 '16 at 21:42
  • I included the edited version of your second example, which I am working with, in my original question, so that you can compare easily! – bzm3r Aug 17 '16 at 21:44
  • I'll take a look later on –  Aug 17 '16 at 22:04
  • 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
  • @user89: The main problem is not my code actually but your feature request -- the reset counter list sets the counters explicitly to 0 and not to -1. This can be changed, but I don't see an advantage in this at all –  Aug 18 '16 at 08:02
  • @user89: I've updated my answer, but I don't think that your request is really well... –  Aug 18 '16 at 08:40
  • And of course, you will get into troubles with counter formatters with \alph etc. –  Aug 18 '16 at 08:46
  • @ChristianHupfer I accepted David's answer above, because it cleanl does exactly what I want, everywhere! – bzm3r Aug 18 '16 at 15:53
  • @user89: Your decision then -- his solution changes the counter output stuff, not the counters itself. –  Aug 18 '16 at 16:44