Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.  

share|improve this question
    
Have you made two runs after the change? –  Stephan Lehmke May 4 '12 at 12:46
    
Please post exactly, what pdflatex reports on this error. –  Keks Dose May 4 '12 at 12:48
    
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 '12 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 '12 at 13:38

3 Answers 3

up vote 7 down vote accepted

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.

share|improve this answer
    
see the revised question. –  pythonee May 4 '12 at 13:12
    
@pythonee The example works fine. –  egreg May 4 '12 at 14:00
    
@pythonee you should try to clean (remove) all the auxiliary files, especially .aux and .out –  tohecz May 4 '12 at 21:51

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).

share|improve this answer

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*.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.