[Tex/LaTex] Reset section numbering between unnumbered chapters

numberingsectioningstarred-version

The document I'm writing using report class has 2 unnumbered chapters. The sections of the 2nd chapter are numbered following the numbering of the sections of the 1st chapter, like this:

MY FIRST UNNUMBERED CHAPTER
1 First Section
2 Second Section
3 Third Section

MY SECOND UNNUMBERED CHAPTER
4 First Section
5 Second Section

I want it to reset the section numbering every time a new unnumbered chapter is created:

MY FIRST UNNUMBERED CHAPTER
1 First Section
2 Second Section
3 Third Section

MY SECOND UNNUMBERED CHAPTER
1 First Section
2 Second Section

For this, I tried:

  • \@addtoreset{section}{chapter*}, it compiled, but sections were not correctly numbered and PDF bookmarks went crazy.
  • \setcounter{section}{0}, it correctly numbered the sections, but the PDF bookmarks of the sections of the 2nd chapter points to the first chapter.

So, how to reset the section numbers between unnumbered chapters without breaking the PDF bookmarks?

Best Answer

Package hyperref needs the counter values to generate unique anchor names. But there are counter values that are not unique. For example, you have several sections with number 1.

Because of this, hyperref has introduced \theH<counter> and prefers it over \the<counter> for use in anchor names. \the<counter> might contain weird or duplicate values. As long as \theH<counter> expands to a simple unique string, hyperref is happy.

The following example provides unique definitions for \theHsection:

\documentclass{report}
\usepackage{hyperref}
\usepackage{bookmark}
\bookmarksetup{
  numbered,
  open
}
\renewcommand*{\thesection}{\arabic{section}}

\begin{document}
\tableofcontents

\chapter{First numbered chapter}
\section{AB}
\section{BC}
\section{DE}

\chapter*{First unnumbered chapter}
\addcontentsline{toc}{chapter}{First unnumbered chapter}
\setcounter{section}{0}
\renewcommand*{\theHsection}{chX.\the\value{section}}
\section{FG}
\section{HI}
\section{IJ}

\chapter*{Second unnumbered chapter}
\addcontentsline{toc}{chapter}{Second unnumbered chapter}
\setcounter{section}{0}
\renewcommand*{\theHsection}{chY.\the\value{section}}
\section{KL}
\section{MN}
\section{NO}

\renewcommand*{\theHsection}{\theHchapter.\the\value{section}}

\chapter{Last numbered chapter}
\section{PQ}
\section{RS}
\section{TU}
\end{document}

Bookmarks