0

I'm not sure what's going on here, but my PDF's ToC and bookmarks are pointing to the wrong places. Chapter references seem to work, but section and subsection references just go to the chapter.

For example, my tex file looks like this in one part:

\subsection{Visual Studio}
\label{visualstudio}

The generated .toc file has this line:

\contentsline {chapter}{\numberline {1}Performance Measurement and Tools}{1}{chapter.1}
\contentsline {section}{Choosing What to Measure}{1}{chapter.1}
...lots more sections with same issue
\contentsline {subsection}{Visual Studio}{10}{chapter.1}

Why is it doing this and how can I fix it? Here is a reproducible example (I hope). It's about 250 lines long.

\def\mytitle{My Title}

\documentclass[12pt,openright]{book} 
\usepackage{imakeidx}
\usepackage{listings}
\usepackage[pdftex,
            pdfauthor={Ben Watson},
            pdftitle={Test},
            pdfsubject={Programming/Microsoft/.NET},
            pdfkeywords={Microsoft, .NET, JIT, Garbage Collection},
            pdfproducer={pdflatex},
            pdfcreator={pdflatex}]{hyperref}
\usepackage{graphicx}
%\usepackage{courier}
\usepackage[paperwidth=7.5in, paperheight=9.25in, left=1.0in, right=0.75in, top=1.0in, bottom=1.0in,headheight=15pt, ]{geometry}
\usepackage[section]{placeins}
\usepackage{booktabs}
\usepackage[numbered]{bookmark}
\usepackage{pdfpages}
\usepackage{textcomp}
\usepackage{emptypage}
\usepackage{color}
\usepackage{xcolor}
\usepackage{titlesec}
\usepackage{amssymb}
\usepackage{etoolbox}
\usepackage[most]{tcolorbox}
\usepackage[htt]{hyphenat}
\usepackage[defaultlines=3,all]{nowidow}
\usepackage[font={it}, labelsep=period]{caption}
% Left-justify (ragged right edges) for paragraphs
\usepackage[document]{ragged2e}

% Setup Headings

\usepackage{fancyhdr}

\pagestyle{fancy}
\fancyhead[LE]{\leftmark}
\fancyhead[RE]{}
\fancyhead[LO]{}
\fancyhead[RO]{\rightmark}

\makeindex[columns=2, intoc]
\usepackage[totoc]{idxlayout}

\makeatletter
\newcommand{\enableopenany}{%
  \@openrightfalse%
}
\newcommand{\disableopenany}{%
  \@openrighttrue%
}
\makeatother

% List settings
% \setlist{noitemsep}
% \setlist[2]{nosep}

% customize bullet points
\renewcommand{\labelitemi}{$\bullet$}
\renewcommand{\labelitemii}{$\circ$}
\renewcommand{\labelitemiii}{$\cdot$}

\titleformat{\section}
  {\normalfont\Large\bfseries}{\thesection}{1em}{}[{\titlerule[0.8pt]}]

% Width of column separator
\setlength{\columnseprule}{0.2pt}

% == eBook settings ==
% PRINTTRUEFALSE should be replaced via preprocessor to be true or false
\newtoggle{print}
\togglefalse{print} 

\iftoggle{print}{
% print
\hypersetup{
    colorlinks,
    linkcolor=black,
    citecolor=black,
    urlcolor=black
}

}{ % end if
% ebook

\hypersetup{
    colorlinks,
    linkcolor={red!50!black},
    citecolor={blue!50!black},
    urlcolor={blue!80!black}
}
}%end else


% disable equations for Kindle editions
\newtoggle{kindle}
\togglefalse{kindle}

% Begin the document

\begin{document}

% Include graphic image for eBook PDF, but not print

\setcounter{secnumdepth}{0}

% Custom hyphenations
\hyphenation{Perf-View Text-Reader}

\frontmatter


 \enableopenany
\begin{titlepage}
    \centering
    \vspace{3cm}
    {\scshape\Huge Test Title\par}
    \vspace{3cm}
    {\Large\itshape Ben Watson\par}
    \vfill
\end{titlepage}

\thispagestyle{empty} 

Blah

\cleardoublepage
\hypertarget{tocpage}{}
\tableofcontents
\bookmark[dest=tocpage,level=0]{Contents} 

% set paragraph spacing
\setlength{\parskip}{3mm plus1mm minus1mm}


\chapter{Introduction}
\label{introduction}

 \fancyhead[LE]{ INTRODUCTION } 

\section{Purpose of this Book}
\label{purposeofthisbook}

Blah

\section{Why Should You Choose Managed Code?}
\label{whyshouldyouchoosemanagedcode}

Blah

\section{Is Managed Code Slower Than Native Code?}
\label{ismanagedcodeslowerthannativecode}

Blah

\section{Are The Costs Worth the Benefits?}
\label{arethecostsworththebenefits}

Blah

\section{Am I Giving Up Control?}
\label{amigivingupcontrol}

Blah

\section{Work With the CLR, Not Against It}
\label{workwiththeclrnotagainstit}

Blah

\section{Layers of Optimization}
\label{layersofoptimization}

Blah

\section{The Seductiveness of Simplicity}
\label{theseductivenessofsimplicity}

Blah

\section{.NET Performance Improvements Over Time}
\label{.netperformanceimprovementsovertime}

Blah

\section{.NET Core}
\label{.netcore}

Blah

\section{Sample Source Code}
\label{samplesourcecode}

Blah

\section{Why Gears?}
\label{whygears}

Blah

 \mainmatter 

 \fancyhead[LE]{\leftmark} 

\chapter{Performance Measurement and Tools}
\label{performancemeasurementandtools}

Blah

\section{Useful Tools}
\label{usefultools}

Blah

\subsection{Visual Studio}
\label{visualstudio}

Dummy text

\subsection{dummy1}
\label{dummy2}

Dummy text

\subsection{dummy3}
\label{dummy3}

Dummy text

\subsection{dummy4}
\label{dummy4}

Dummy text

\subsection{dummy5}
\label{dummy5}

Dummy text

\subsection{dummy6}
\label{dummy6}

Dummy text

\subsection{dummy7}
\label{dummy7}

Dummy text
 \end{document}
\end{document}

It produces this toc file, which seems to have broken references:

\contentsline {chapter}{Introduction}{iv}{chapter*.2}
\contentsline {section}{Purpose of this Book}{iv}{chapter*.2}
\contentsline {section}{Why Should You Choose Managed Code?}{iv}{chapter*.2}
\contentsline {section}{Is Managed Code Slower Than Native Code?}{iv}{chapter*.2}
\contentsline {section}{Are The Costs Worth the Benefits?}{iv}{chapter*.2}
\contentsline {section}{Am I Giving Up Control?}{iv}{chapter*.2}
\contentsline {section}{Work With the CLR, Not Against It}{v}{chapter*.2}
\contentsline {section}{Layers of Optimization}{v}{chapter*.2}
\contentsline {section}{The Seductiveness of Simplicity}{v}{chapter*.2}
\contentsline {section}{.NET Performance Improvements Over Time}{v}{chapter*.2}
\contentsline {section}{.NET Core}{v}{chapter*.2}
\contentsline {section}{Sample Source Code}{v}{chapter*.2}
\contentsline {section}{Why Gears?}{v}{chapter*.2}
\contentsline {chapter}{\numberline {1}Performance Measurement and Tools}{1}{chapter.1}
\contentsline {section}{Useful Tools}{1}{chapter.1}
\contentsline {subsection}{Visual Studio}{1}{chapter.1}
\contentsline {subsection}{dummy1}{1}{chapter.1}
\contentsline {subsection}{dummy3}{2}{chapter.1}
\contentsline {subsection}{dummy4}{2}{chapter.1}
\contentsline {subsection}{dummy5}{2}{chapter.1}
\contentsline {subsection}{dummy6}{2}{chapter.1}
\contentsline {subsection}{dummy7}{2}{chapter.1}
  • 2
    welcome to tex.sx. the structure of the toc is usually defined by the document class, and with different classes it may differ, so we can't even guess with the information you've given so far. you'll get much more help if you provide a small compilable example that demonstrates the problem. then potential helpers can copy-and-paste it so they can experiment. – barbara beeton Apr 30 '18 at 20:36
  • You're loading hyperref and bookmark packages way too early. Load them last, bookmark after hyperref. This might be of interest: Which packages should be loaded after hyperref instead of before? Welcome to TeX.SE! – Troy Apr 30 '18 at 22:00
  • @Troy, that was the issue. I just moved those two down and everything works. Thank you. – Ben Watson May 1 '18 at 4:00