Generalproblem with table of contents when I restart chapters counter

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
javgarmar
Posts: 14
Joined: Sat Sep 26, 2020 7:02 pm

problem with table of contents when I restart chapters counter

Post by javgarmar »

Hello all!
I wonder if you have had this problem:

I have set the counter of chapters to 0 and now my table of contents is not working good. Is there any problem in setting that counter back to 0?
Before I had:
Part 1
Chapter 1
Chapter 2
Part 2
Chapter 3
Chapter 4
Chapter 5
And now I have:
Part 1
Chapter 1
Chapter 2
Part 2
Chapter 1
Chapter 2
Chapter 3
Still, when I click in the table of contents to chapter 2 in part 2 it sends me to chapter 2 in part 1.

Thank you very much in advance!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Bartman
Posts: 369
Joined: Fri Jan 03, 2020 2:39 pm

problem with table of contents when I restart chapters counter

Post by Bartman »

I suppose you want to tie the chapter counter to the respective part. If I am correct with this assumption, then you may be interested in the use of the \counterwithin command in your preamble.

A note on this command: In more recent TeX distributions the command is supported without having to load another package. You can read more in issue 28 of ltnews.
javgarmar
Posts: 14
Joined: Sat Sep 26, 2020 7:02 pm

problem with table of contents when I restart chapters counter

Post by javgarmar »

Hello Bartman,

Thank you very much for your answer. I have tried with \counterwithin{chapter}{part} but it does not work.

The thing is that since at the beggining of each chapter I am writing:

\newpage
\part*{PART TITLE}
\addcontentsline{toc}{part}{PART TITLE}
\setcounter{chapter}{0}


In the general table of contents when you click on chapter two of part two (for example) it goes to chapter two of part one (and the same with all the parts).
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

problem with table of contents when I restart chapters counter

Post by Ijon Tichy »

Numbered chapters below not numbered parts are always problematic. Resetting the chapter number with the part makes this problem even much more serious. hyperref uses the number to generate the link anchor. So, if there is no part number and but several chapters with the same number, the link anchors for these chapters are the same. Using a KOMA-Script class there would be a workaround for this, by emulating not numbered parts with numbered parts:

Code: Select all

\documentclass{scrreprt}

\usepackage{blindtext}
\usepackage{hyperref}

\counterwithin*{chapter}{part}
\renewcommand*{\partformat}{}% Don't print part numbers in part titles
\renewcommand*{\addparttocentry}[2]{\addtocentrydefault{part}{}{#2}}% Ignore part numbers at ToC entries

\begin{document}
\tableofcontents
\part{First Part}
\blinddocument
\blinddocument
\part{Second Part}
\blinddocument
\blinddocument
\end{document}
With the standard classes this is not such easy:

Code: Select all

\documentclass{report}

\usepackage{blindtext}
\usepackage{hyperref}

\counterwithin*{chapter}{part}
\newcommand*{\Part}[2][\partheading]{%
  \refstepcounter{part}%
  \def\partheading{#2}%
  \part*{#2}%
  \addcontentsline{toc}{part}{#1}%
}

\begin{document}
\tableofcontents
\Part{First Part}
\blinddocument
\blinddocument
\Part{Second Part}
\blinddocument
\blinddocument
\end{document}
However, you will still have the problem to reference, because if you say "see chapter 2" nobody will know, which chapter 2 you mean. And because the parts do not have a number, you cannot say "see chapter 2 in part 2", you always have to use the title of the part. So I would not recommend to combine resetting chapter numbers with ever part and not numbered parts:

Code: Select all

\documentclass{report}

\usepackage{blindtext}
\usepackage{hyperref}

\counterwithin*{chapter}{part}

\begin{document}
\tableofcontents
\part{First Part}
\blinddocument
\blinddocument
\part{Second Part}
\blinddocument
\blinddocument
\end{document}
works and so does:

Code: Select all

\documentclass{report}

\usepackage{blindtext}
\usepackage{hyperref}


\begin{document}
\tableofcontents
\part*{First Part}
\addcontentsline{toc}{part}{First Part}
\blinddocument
\blinddocument
\part*{Second Part}
\addcontentsline{toc}{part}{Seconde Part}
\blinddocument
\blinddocument
\end{document}
Note: If things are complicated in LaTeX they are often problematic for readers too.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
javgarmar
Posts: 14
Joined: Sat Sep 26, 2020 7:02 pm

problem with table of contents when I restart chapters counter

Post by javgarmar »

Thank you very much for your kind expanation Ijon. Since I am using class scrbook, I have tried to do it emulating not numbered parts with numbered parts (makes sense).

However I am having still troubles with the toc hiperlinks:

*EDITED: see next message*

I have commented de chapter counter resettings and the \addcontentsline, since I am using now parts instead of parts*. However, when I click on the TOC on chapter two of part two its still driving me to chapter 2 in part 1.

Also, I'm not getting the difference between \counterwithin and \counterwithin* but I have written it as you did in your message just in case.
Last edited by javgarmar on Thu Oct 01, 2020 1:43 pm, edited 1 time in total.
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

problem with table of contents when I restart chapters counter

Post by Ijon Tichy »

Please minimize your code, i. e. the document preamble.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
javgarmar
Posts: 14
Joined: Sat Sep 26, 2020 7:02 pm

problem with table of contents when I restart chapters counter

Post by javgarmar »

Sure, sorry for copy pasting direcly, I think this is the code in its minimum expression

Code: Select all

\documentclass{scrbook}
 
\usepackage{minitoc}
\usepackage{import}
\usepackage{hyperref}

\renewcommand{\mtctitle}{ÍNDICE}
\counterwithin*{chapter}{part}

% Don't print part numbers in part titles
\renewcommand*{\partformat}{}

% Ignore part numbers at ToC entries
\renewcommand*{\addparttocentry}[2]{\addtocentrydefault{part}{}{#2}}
 
\setcounter{tocdepth}{4}
\setcounter{secnumdepth}{4}
 
\title{TITLE}
\author{NAME}
 
\begin{document}
 
    \maketitle
 
    \dominitoc
 
    \newpage
    \tableofcontents 
 
    \newpage
    \part{TITLE PART 1}
    %\addcontentsline{toc}{part}{TITLE PART 1} 
    %\setcounter{chapter}{0}
 
        \import{PART 1/}{PART 1.tex}  
 
    \newpage
    \part{TITLE PART 2}
    %\addcontentsline{toc}{part}{TITLE PART 2}
    %\setcounter{chapter}{0}
 
        \import{PART 2/}{PART 2.tex}
 
\end{document}
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

problem with table of contents when I restart chapters counter

Post by Ijon Tichy »

If I add package blindtext and replace the import of files, which I do not have, by a simple \blinddocument I cannot reproduce your problem:

Code: Select all

\documentclass{scrbook}

\usepackage{minitoc}
\usepackage{import}
\usepackage{hyperref}

\renewcommand{\mtctitle}{ÍNDICE}
\counterwithin*{chapter}{part}

% Don't print part numbers in part titles
\renewcommand*{\partformat}{}

% Ignore part numbers at ToC entries
\renewcommand*{\addparttocentry}[2]{\addtocentrydefault{part}{}{#2}}

\setcounter{tocdepth}{4}
\setcounter{secnumdepth}{4}

\title{TITLE}
\author{NAME}

\usepackage{blindtext}

\begin{document}

\maketitle

\dominitoc

\newpage% <-- does not make any sense
\tableofcontents 

\newpage% <-- does not make any sense
\part{TITLE PART 1}
%\addcontentsline{toc}{part}{TITLE PART 1} 
%\setcounter{chapter}{0}

\blinddocument  

\newpage% <-- does not make any sense
\part{TITLE PART 2}
%\addcontentsline{toc}{part}{TITLE PART 2}
%\setcounter{chapter}{0}

\blinddocument

\end{document}
The link to the chapter in the firstpart goes to the chapter in the second part (page 7).
The link to the chapter in the second part goes to the chapter in the second part (page 13).

Do you really get another result with my code? Are you sure, you are using an up-to-date LaTeX, i. e. the current versions of KOMA-Script and hyperref?
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
javgarmar
Posts: 14
Joined: Sat Sep 26, 2020 7:02 pm

problem with table of contents when I restart chapters counter

Post by javgarmar »

Hello Ijon,

I am using overleaf currently but I usually also use winedt when I am offline.

The thing is that with your code, what I get is two parts with one chapter each. Howerver, it is true that in this case the toc its working, when I click on chapter one part 2 it doesn't go to the one in part 1.

So I have tried the following code:

Code: Select all

\documentclass{scrbook}
 
\usepackage{minitoc}
\usepackage{import}
\usepackage{hyperref}
 
\renewcommand{\mtctitle}{ÍNDICE}
\counterwithin*{chapter}{part}
 
% Don't print part numbers in part titles
\renewcommand*{\partformat}{}
 
% Ignore part numbers at ToC entries
\renewcommand*{\addparttocentry}[2]{\addtocentrydefault{part}{}{#2}}
 
\setcounter{tocdepth}{4}
\setcounter{secnumdepth}{4}
 
\title{TITLE}
\author{NAME}
 
\begin{document}
 
    \maketitle
 
    \dominitoc
 
    \newpage
    \tableofcontents 
 
    \newpage
    \part{TITLE PART 1}

        \chapter{CHAPTER 1}
                
            Hello
                
        \chapter{CHAPTER 2}
        
            Hello
        
    \newpage
    \part{TITLE PART 2}

        \chapter{CHAPTER 1}
                
            Hello
                
        \chapter{CHAPTER 2}
        
            Hello
 
\end{document}
And it has worked also.

So what I don't know is why my code, where the only difference is that I am using import, doesn't.

Thank you very much for your help.
Post Reply