[Tex/LaTex] Latex, pdflatex : problem with bookmark on PDF file

bookmarkshyperrefsectioning

I am encountering some difficulties:

After compilation of my document (complex), the bookmarks in pdf file of the sections do not point towards the corresponding section of their own chapter.
There are for moment 5 chapters, not numbered (\chapter*{...}).
The bookmark of section 1 of chapter number 2 points towards section 1 of chapter number 1, section 2 of chapter 2 points towards that of chapter 1, idem for all the other of the same sections hierarchy, they point towards that of chapter 1 when it exists in chapter 1.

Here, I add the exact preamble on my .tex file. I want unnumbering chapter, I want re-to initialize number section and footnotes per chapter… So, something wrong with bookmark. (?)

% !Mode:: "TeX:UTF-8"
%!TEX vérification orthographique = fr-FR
% ----------------------------------------------------
\documentclass[11pt,a4paper,openany]{book}% smfbook
\special{papersize=210mm,297mm}
% ---------------------------------------------------
% encodage caractères ; gestion des accents; rendu pdf
\usepackage{etex}%
\usepackage{ucs}%
\usepackage[T1]{fontenc}
\usepackage[utf8x]{inputenc}%
\usepackage{lmodern}
\usepackage{tipa}%
\usepackage{tipx}%
\usepackage{aeguill}
\usepackage{xspace}
\usepackage{relsize}
% ---------------------------------------------------
% style de mise en page général du document
% autres: Glenn,Lenny,Conny,Rejne et Bjarne, Bjornstrup
\usepackage[Sonny]{fncychap}% 
\usepackage[geometry,weather,misc,clock]{ifsym}%
% ---------------------------------------------------
% gestions des mathématiques : l'AMS + mathtools ; caractères
\usepackage{mathtools,amsmath,amssymb,amsfonts,mathrsfs,bm}
\usepackage{latexsym}
\usepackage{dcolumn}%
\usepackage[nice]{nicefrac}%
\allowdisplaybreaks
% Système international d'unité - l'indispensable 
\usepackage{siunitx}%
% ---------------------------------------------------
% Geometrie du document + Notes en marge
% ---------------------------------------------------
%\usepackage{layout}
\usepackage{geometry}%
\geometry{margin=22.5mm,%
includeheadfoot,footskip=32pt}
% réglages fin de la zone de texte imprimable
\textwidth 15.5cm%
\textheight 23cm%
\oddsidemargin 0pt%3mm%
\evensidemargin 0pt%3mm%
\topmargin -20pt
\headsep 20pt %4mm%
\headheight 20pt%8mm 
% ---------------------------------------------------
% Gestion des pieds de page
\usepackage[bottom]{footmisc}
% ---------------------------------------------------
% Francisation complète du document
% ---------------------------------------------------
\usepackage[french]{babel}
\DecimalMathComma %supprime l'espace fin après la virgule en mode math
\AddThinSpaceBeforeFootnotes
\frenchspacing% better looking spacing
\frenchbsetup{FrenchSuperscripts=true}
% ---------------------------------------------------
% Customize (header and) footer
% ---------------------------------------------------
\usepackage{fancyhdr}
\pagestyle{fancy}
\rhead{}
\chead{\footnotesize Cadran Info \No{}35 --- Mai 2017}% A modifier ici
\lhead{}
\cfoot{}
\rfoot{\footnotesize ~\\ \thepage}
\renewcommand{\headrulewidth}{0.0pt}% no bar on top of page
\renewcommand{\footrulewidth}{0.0pt}% bar on bottom of page
% ---------------------------------------------------
\usepackage[active]{srcltx}% pour passer du source au pdf et vice versa
% ---------------------------------------------------
% --- Liens Hypertextes%
\usepackage[pdftex,%
  plainpages={false},%
  breaklinks={true},%
  colorlinks={true},%
  urlcolor={blue},%
  citecolor={blue},%
  linkcolor={blue},%
  linktocpage={true},%
  filecolor={red},%
  bookmarks={true},%
  bookmarksopen={true},%
  bookmarksopenlevel={4},%
  bookmarksnumbered={true},%
  pdflang={fr},%
  pdftitle={Cadran Info num\'ero 35 - Mai 2017},%
  pdfauthor={Philippe Sauvageot, Dominique Collin},%
  pdfsubject={Bulletin de la Commission des Cadrans Solaires de la Soci\'et'e Astronomique de France},%
  pdfkeywords={cadran solaire \`a r\'eflexion;bloc gnomonique;histoire, \'Equation du temps;cadran en Corse;cadran crucifix;th\'eorie;r\'ealisation},%
 % pdfproducer{Commission des Cadrans Solaires},%
  pdfpagelabels,%
  pdfpagemode={FullScreen},%
  pdfview={Fit},%
  pdfstartview={Fit},%
  pdfpagelayout={SinglePage},%
  pdfdisplaydoctitle,%
  pdfpagetransition={Dissolve}%Glitter;Wipe;Split;Box;Blinds; 
]{hyperref}% Activate hyperlinks
% ---to ensure the hyperlinks actually jump to the right place
\makeatletter
\newcommand\org@hypertarget{}
\let\org@hypertarget\hypertarget
\renewcommand\hypertarget[2]{%
\Hy@raisedlink{\org@hypertarget{#1}{}}#2%
}
\makeatother
%
\usepackage[%
  open,%
  openlevel=2,%
  atend,%
  numbered%
]{bookmark}%
\bookmarksetup{%
  addtohook={%
    \ifnum\bookmarkget{level}<1 %
      \bookmarksetup{bold}%
    \fi}
}%
% --------
% supprime le numero de page sur la premiere page de chaque chapitre...
\makeatletter
\let\ps@plain=\ps@empty
\makeatother
%
\makeatletter
\renewcommand{\@chapapp}{}
\makeatother
% Profondeur de \subsubsection = 4
\setcounter{tocdepth}{4}     % Dans la table des matieres
\setcounter{secnumdepth}{4}  % Avec un numero.
% ---------------------------------------------------
% redefinition de l'index : index en Trois colonnes...
\makeatletter
\renewenvironment{theindex}{%
\protect
\begin{multicols}{3}[\chapter*{\indexname}][10\baselineskip]%
\addcontentsline{toc}{chapter}{\indexname}%
\setlength\parindent{0pt}\pagestyle{plain}\let\item\@idxitem}
{\end{multicols}}%
\protect
\makeatother
% -------
\makeindex % pour creer un index.
\thispagestyle{empty}
% --------------------------------------------------
\begin{document}
% --------------------------------------------------
\renewcommand{\bibname}{R\'ef\'erences}
\addtocontents{toc}{~\hfill\textbf{Page}\par}
% ------
\setcounter{page}{1} % forçage du compteur de page à 1
\pdfbookmark[1]{Contents}{table} 

\cleardoublepage
% Une table des matières...
\renewcommand{\contentsname}{Sommaire} % Dans le corps du document,avant la commande\tableofcontents.
{\relsize{-1}{\tableofcontents}} % Fr

% ------ pour re-initialiser la numérotation des chapitres et sections
\renewcommand{\thechapter}{}
\renewcommand{\thesection}{\arabic{section}}
% ------

\setcounter{section}{0}
\setcounter{figure}{0}%
\setcounter{table}{0}%
\setcounter{footnote}{0}% renouvelle la numérotation des notes de bas de page pour le chapitre.
% ------------------------
\phantomsection
\chapter*{\textbf{Premier chapitre\dots} par \textsc{toto 1}\label{toto1}}
\addcontentsline{toc}{chapter}{\textbf{Premier chapitre\dots} par \textsc{toto}}
% ------------------------
\hypertarget{test1}{~}
% ------------------------
\emph{Ceci constitue le premier chapitre}. 

Il est non numéroté.

Mais les sections le sont quand à elles.
%
% ------------------------
\section{Une section du chapitre 1}
bla , bla. 

Une note de bas de page\footnote{Test de note de bas de page}
\newpage
\subsection{Une sous section du chapitre 1}
Sans commentaire\ldots\footnote{Encore unee autre note de bas de page.}
% ------------------------
\setcounter{section}{0}
\setcounter{figure}{0}%
\setcounter{table}{0}%
\setcounter{footnote}{0}% renouvelle la numérotation des notes de bas de page pour le chapitre.
% ------------------------
\phantomsection
\chapter*{\textbf{Deuxième chapitre\dots} par \textsc{toto 2}\label{toto2}}
\addcontentsline{toc}{chapter}{\textbf{Deuxième chapitre\dots)} par \textsc{toto}}
% ------------------------
\hypertarget{test2}{~}
% ------------------------
\emph{Ceci constitue le 2\ième{} chapitre}\footnote{Test de note de bas de page. Les compteurs sont bien remis à zéro.}

Il est non numéroté...
% ------------------------
\section{Une section du chapitre 2}
bla , bla. 

Une note de bas de page\footnote{Test de note de bas de page}
\newpage

\subsection{Une sous section du chapitre 2}
Sans commentaire\ldots\footnote{Encore une autre note de bas de page}

\subsection{Une sous section du chapitre 2}
Allez, encore une autre\ldots\footnote{Encore une autre note de bas de page}
% --------------------------------------------------
 \end{document}
% --------------------------------------------------

for example: \section*{} inside each chapter have the same id number in .aux file. So, when in pdf file, I invoke the bookmark for section 2 of chapter 3 for example, this point to the section 2 of chapter 1… same for another chapter…

How to make so that the bookmark section 2 of chapter 2 points towards the good place?
How I can fix this bookmark problem? Problem occur when I use \chapter*{} in documentclass book, and the sections are automatic same numbering like {section.0.1} in .aux file.

I thank you for your assistance to find the way to correct bookmark adresses.

D. COLLIN

Best Answer

The problem is that the numbering for the sections (subsections, subsubsections) is restarting at each unnumbered chapter. Then, the section numbers are no longer unique. The numbers are used to construct anchor names, which must be unique.

Package hyperref already uses \theH<counter> if available to get a unique number for \the<counter>. The problem can be fixed by defining \theHchapter for the unnumbered chapters, e.g.:

\chapter*{Premiere chapitre}
\def\theHchapter{1}
...
\chapter*{Deuxième chapitre}
\def\theHchapter{2}
...

LaTeX expands the options for a package in a hard way that might break arbitrary TeX code as given in the text for the information entries. Therefore, they should be given after loading the package in \hypersetup. Also pdfencoding=auto or pdfencoding=unicode is recommended to enable Unicode in the bookmarks and PDF information entries.

\usepackage[pdfencoding=auto, ...]{hyperref}
\hypersetup{
  pdftitle={...},
  pdfauthor={...},
  % ...
}

Package geometry is specified several times. Then, use it and remove the low level stuff \special{papersize=...} (is done by geometry with the correct driver). Also, the settings \textwidth=..., ... can be replaced by options.

% More or less right after \documentclass, when
% the package is loaded the first time:
\usepackage[
  includefootheight,
  headheight=20pt,
  footskip=22pt,
  % ...
]{geometry}
Related Question