2

I'm using the class book and for each chapter, section, subsection, etc. the numbering starts this way:

Chapter 1

Section 1.1

Section 1.2

Section 1.3 . . .

I would like to have instead something like this:

Chapter 1

Section 1.0

Section 1.1

Section 1.2

Any suggestions? Thanks a lot for your help.

1
  • 1
    Welcome to TeX.SX! Do you really want to start the section counter with 0?
    – user31729
    Mar 25, 2016 at 18:37

3 Answers 3

2

You can redefine \thesection so that it issues the current number minus one.

\documentclass{book}

\makeatletter
\renewcommand{\thesection}{%
  \thechapter.\@arabic{\numexpr\c@section-1}%
}
\makeatother

\begin{document}
\tableofcontents

\chapter{First chapter}

\section{First section}
\section{Second section}
\section{Third section}

\chapter{Second chapter}

\section{First section}
\section{Second section}
\section{Third section}

\end{document}

enter image description here

0
5

\chapter calls \@chapter, which does \refstepcounter (in mainmatter mode) -- this means that any counter on the reset list of the chapter counter is reset to zero, this is true for the section counter of course.

Either \@chapter is redefined or one can add some additional code, that sets the section counter to -1 after the refstepping has be done.

Please note: This does not set the subsection counters to -1 etc.

\documentclass{book}
\usepackage{xpatch}

\makeatletter
\AtBeginDocument{%
\xpatchcmd{\@chapter}{%
  \refstepcounter{chapter}%
}{%
  \refstepcounter{chapter}%
  \setcounter{section}{-1}%
}{\typeout{Success}}{}
}
\makeatother

\begin{document}
\tableofcontents

\chapter{First chapter}

\section{First section}

\section{Second section}
\section{Third section}

\chapter{Second chapter}

\section{First section}

\section{Second section}

\section{Third section}


\end{document}

enter image description here

Here's a version that cascades down to \subparagraph:

\documentclass{book}
\usepackage{xpatch}

\makeatletter
\xpatchcmd{\@sect}{%
  \refstepcounter{#1}%
}{%
  \refstepcounter{#1}%
  % Now use the \@elt - trick to set all depending counters to -1 (well even that one that shouldn't, most likely :-()
  \def\@elt##1{\setcounter{##1}{-1}}
  \csname cl@#1\endcsname%
}{}{}


\AtBeginDocument{%
  \xpatchcmd{\@chapter}{%
    \refstepcounter{chapter}%
  }{%
    \refstepcounter{chapter}%
    \setcounter{section}{-1}%
   }{\typeout{Success}}{\typeout{Failed!}}
}
\makeatother

\setcounter{tocdepth}{5}
\setcounter{secnumdepth}{5}
\begin{document}
\tableofcontents

\chapter{First chapter}

\section{First section}
\subsection{First subsection}
\subsubsection{First subsubsection}
\paragraph{First paragraph}
\subparagraph{First subparagraph}


\section{Second section}
\subsection{First subsection}
\subsubsection{First subsubsection}
\paragraph{First paragraph}
\subparagraph{First subparagraph}

\section{Third section}

\chapter{Second chapter}

\section{First section}

\section{Second section}

\section{Third section}


\end{document}
4
  • Thanks so much, but was enough to write \setcounter{section}{-1} for each section! :)
    – AdN
    Mar 25, 2016 at 18:49
  • I think it's better with {}{\errmessage{patch fail}} rather than {\typeout{Success}}{} no?
    – touhami
    Mar 25, 2016 at 18:56
  • @touhami: In a critical environment: Yes, but not for a simple document -- you would notice that the requested feature does not appear, wouldn't you? ;-)
    – user31729
    Mar 25, 2016 at 19:10
  • you're right, but I find it a good practice :-)
    – touhami
    Mar 25, 2016 at 19:13
-1

You could do something like this for each section:

\setcounter{section}{-1}

Note: I originally wrote \setcounter{section}{0} when answering.

4
  • For each section? The section counter is zero right after \chapter. The next \section call will step the section counter, i.e. it's having the value of 1 then, not zero. If this should work, \setcounter{section}{-1} would be the more successful way
    – user31729
    Mar 25, 2016 at 18:45
  • 4
    Sorry but this is not really an answer, can you please expan it?
    – touhami
    Mar 25, 2016 at 18:49
  • 1
    Do you mean \setcounter{section}{-1} after each \chapter{...} ? If not, than your answer is nonsense. However, this is already proposed @ChristianHupfer answer (on more sophisticate way).
    – Zarko
    Mar 25, 2016 at 19:35
  • Christian Hupfer, you're right absolutely right, I did mean -1 instead of 0, wasn't thinking. You could add it in the code after you begin each section. Zarko, noted, but I had already answered previously, similar to how Christian had already mentioned exactly what you just did. His answer is far more sophisticated as well, and may be excessive depending on the particular use in my opinion. Since the OP is using the book class to begin with though, he is probably working on something longer and the accepted answer probably is best, although he went with what I said minus the error.
    – Tristen
    Mar 25, 2016 at 19:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .