# \setcounter{section} problems with TOC hyperref links

In writing my thesis, I have two Parts each of them composed by several Chapters. I use \input to include the different Chapters files. At the beginning of each Part I want to write an introductory section, and I want it to appear in TOC as Section i.1 and Section i.2. Now the naming and the page numbering works fine. However, hyperref link from TOC points to the previous Section with the same number. It is more clear with this MWE:

The main file is

\documentclass [a4paper,12 pt]{book}
\usepackage{hyperref}
\usepackage{lipsum}

\begin{document}

\frontmatter
\tableofcontents
\clearpage{\pagestyle{empty}\cleardoublepage}

\mainmatter
\input{File1}
\clearpage{\pagestyle{empty}\cleardoublepage}

%%%%-----------------------%

\phantomsection
\input{File2}
\clearpage{\pagestyle{empty}\cleardoublepage}

\end{document}


File 1 is

\section{First section}
\lipsum

\section{Second section}
\lipsum


File 2 is

\thispagestyle{empty} \phantom{}

\vfill
\begin{center}
{\huge \scshape\bfseries Title Part I\\}
\end{center}
\vfill
\newpage

{\clearpage\phantomsection\renewcommand\thechapter{\rm S}
\setcounter{section}{0}
\label{sec:intro1}
\section{Section S1}
\lipsum}

% In the real file I have here many others Chapters and Sections.

{\clearpage\phantomsection\renewcommand\thechapter{\rm S}
\setcounter{section}{1}
\section{Section S2}
\label{sec:intro2}
\lipsum}


When now I compile, the clickable link of Section i1 points to First Section and Section i2 points to Second Section. How to have the links pointing to the right place?

• Welcome to TeX.SX! The size option is called 12pt; the fact that also 12 pt works in incidental. You can avoid the clumsy \clearpage\thispagestyle{empty}\cleardoublepage by just loading emptypage. – egreg Jun 10 '18 at 15:16

I'd define an environment in order to avoid too much repetitive code in the document body. The main trick is to save the value of the “part sections” and to locally redefine \theHsection so as to not confuse hyperref with double references to the same object.

\documentclass[a4paper,12pt]{book}
\usepackage{emptypage}
\usepackage{hyperref}
\usepackage{bookmark}

\usepackage{lipsum}

\newcounter{partsection}

\newenvironment{Part}[1]
{%
\cleardoublepage
\phantomsection
\setcounter{section}{\value{partsection}}%
\renewcommand{\thesection}{S\arabic{section}}%
\renewcommand{\theHsection}{S\arabic{section}}%
\thispagestyle{empty}
\vspace*{\fill}%
\begin{center}
\huge\scshape #1
\end{center}
\vspace*{\fill}
\clearpage
}
{%
\setcounter{partsection}{\value{section}}%
\cleardoublepage
}

\begin{document}

\frontmatter

\tableofcontents

\mainmatter

\chapter{First chapter}

\section{First section}
\lipsum

\section{Second section}
\lipsum

\begin{Part}{Title Part I}

\section{Section i1}
\label{sec:intro1}
\lipsum

\section{Section i2}
\label{sec:intro2}
\lipsum
\end{Part}

\chapter{Second chapter}

\section{First section}
\lipsum

\section{Second section}
\lipsum

\begin{Part}{Title Part II}

\section{Section i1}
\label{sec:intro3}
\lipsum

\section{Section i2}
\label{sec:intro4}
\lipsum
\end{Part}

\chapter{Third chapter}

\section{First section}
\lipsum

\section{Second section}
\lipsum

\ref{sec:intro1} \ref{sec:intro2}
\ref{sec:intro3} \ref{sec:intro4}

\end{document}


• Thanks for the idea of defining a new environment. Like this the code is surely more clean. However it does not solve my problem. The {\rm i} command for the section after the beginning of each part did not mean to stand for \roman. I changed it with {\rm S} in the main question. As you can see now, what I would like to have is that {\rm S} labels all initial sections independently from the Part. The first section of the first Part would be S.1, the first section of the second Part S.2, and so on. – Tommaso Jun 11 '18 at 17:32
• @Tommaso I don't understand: how should the second section of the first part be numbered? – egreg Jun 11 '18 at 17:36
• This should be the structure: 1. First Chapter 1.1 First Section -- Title Part I - S.1 Section S1 - S.2 Section S2 -- 2 Second Chapter - 2.1 First section - 2.2 Second section --- Title Part II - S.3 Section S3 - S.4 Section S4 -- 3 Third Chapter - 3.1 First section - 3.2 Second section – Tommaso Jun 12 '18 at 9:45
• @Tommaso This should now be what you like. – egreg Jun 12 '18 at 10:12
• Yes it works perfectly! I did not know about \newcounter{partsection}. This is the key command which makes it working. Thanks! – Tommaso Jun 12 '18 at 14:08