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.  

lockstep
  • 250,273
pythonee
  • 2,567

3 Answers3

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.

Martin
  • 919
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).

yannisl
  • 117,160
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*.

yannick
  • 813