27

To get the output Toc or pdf bookmark below:


Part I
    Chapter I
    Chapter II
Part II
    Chapter I
    Chapter II

I need to reset the chapter counter. I google and get one method.


\makeatletter
\@addtoreset{chapter}{part}
\makeatother

Yes, It can output like this. But hyperref package can not link to right target for chapters after chapter I. That is said page ref link broken.

I like to define all style in style file. the mini-document:


\documentclass[]{book}
\usepackage[]{hyperref}
\usepackage{mini} % my own style file

\begin{document}
\tableofcontents
\part{Test}
\chapter{One}
\chapter{Two}
\part{Test}
\chapter{Three}
\chapter{Four}
\end{document} 

and the mini style file:


\renewcommand\part{%
  \if@openright
    \cleardoublepage
  \else
    \clearpage
  \fi
\setcounter{chapter}{0}} % I reset manually here.  

4
  • Have you made two runs after the change? May 4, 2012 at 12:46
  • Please post exactly, what pdflatex reports on this error.
    – Keks Dose
    May 4, 2012 at 12:48
  • 1
    have a look at AlanMunn's answer to how-can-i-reset-sectional-counters-in-memoir-class-to-avoid-breaking-toc-cross-r and consider accepting answers to your previous questions- 50% is very low
    – cmhughes
    May 4, 2012 at 12:51
  • 1
    By resetting the chapter counter to 0 every time you issue a \part command, you generate multiple instances of this counter taking on values 1, 2, etc. However, hyperref needs a unique hook for each hyperlink it's supposed to create. Would you be willing to consider numbering your chapters as \thepart\thechapter? This could be achieved by loading the chngcntr package and issuing the command \numberwithin{chapter}{part}.
    – Mico
    May 4, 2012 at 13:38

3 Answers 3

27

This small example is working fine for me:

\documentclass[]{book}
\usepackage[]{hyperref}

\makeatletter
\@addtoreset{chapter}{part}
\makeatother  

\begin{document}
\tableofcontents
\part{Test}
\chapter{One}
\chapter{Two}
\part{Test}
\chapter{Three}
\chapter{Four}
\end{document}

So please provide your minimal example showing the problem. Thank you.

3
  • see the revised question.
    – pythonee
    May 4, 2012 at 13:12
  • @pythonee The example works fine.
    – egreg
    May 4, 2012 at 14:00
  • 1
    @pythonee you should try to clean (remove) all the auxiliary files, especially .aux and .out
    – yo'
    May 4, 2012 at 21:51
8

The problem is not one of counters but your redefinition of part. Your command \part, just handles page opening then it zeroes the chapter. Since your counter is not a subsidiary counter of part the link for the first chapter 1 and the second chapter 1 is the same.

As a matter of fact pdfTeX issues a warning:

pdfTeX warning (ext4): destination with the same identifier (name{chapter.1}) 
has been  already used, duplicate ignored

Like web hyperlinks, you cannot have a link pointing to two different destinations.

To correct the issue you can add the chapter counter to the reset list,

   \makeatletter
   \@addtoreset{chapter}{part}
   \makeatother  

or save book.cls to mybook.cls and change the line \newcounter {chapter} to:

   \newcounter {chapter}[part]

This will work provided you have a proper definition of part (that increments a part counter). As a matter of interest your redefinition of \part does nothing really other than give you trouble. It accepts no parameters! (Have a look at book.cls for similar definitions).

1
  • It is worth to mention that \@addtoreset has to be issued after hyperref was loaded.
    – Skillmon
    Mar 28, 2020 at 18:15
6

You should use the chngcntr package, which in essence is equivalent to \@addtoreset{chapter}{part}. But it's cleaner to use a package isn't it ;)

Also, notice that it should be loaded after the hyperref and bookmark packages to ensure the links are correct.

\documentclass{book}

\usepackage{hyperref}
\usepackage{chngcntr}
\counterwithin{chapter}{part}

\begin{document}
\tableofcontents

\part{Test}
\chapter{One}

\part{Test2}
\chapter{Two}

\end{document}

In addition, \counterwithin will rename all chapter/section/etc. labels and prepend the part to it, e.g. section 2.2. becomes IV.2.2 or whatever. If you don't want this, use \counterwithin*.

You must log in to answer this question.

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