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.

I have encountered a problem in latex that I have not been able to solve. While the hyperref package generates clickable links in the table of contents, it does not link any subsection to the actual subsection but to the section that they are under. Is there anyway to change this behaviour so that it either links to the subsection itself or the page the subsection is on?

After looking into it a bit more, it seems that it is required that one disables section numbering and uses the titlesec package for this issue to show. Below is a sample of this issue:

\documentclass{report}
\usepackage[linktoc=all]{hyperref}
\setcounter{secnumdepth}{0}
\usepackage{titlesec}
\hypersetup{
    colorlinks,
    citecolor=black,
    filecolor=black,
    linkcolor=black,
    urlcolor=black
}

\begin{document}
\tableofcontents
\chapter{test}

Test text
\newpage
\section{A section}

Lorem Ipsum
\newpage
\subsection{This link is wrong}

\end{document}
share|improve this question
    
Thank you for adding the MWE. –  Gonzalo Medina Sep 2 '12 at 2:12
1  
The message Package hyperref Warning: The anchor of a bookmark and its parent's must not be the same. Added a new anchor on input line 20. gives you a hint to the anchor. The answer by Gonzalo Medina explains why. –  Kurt Sep 2 '12 at 2:20
2  
titlesec is not compatible with hyperref for unnumbered sections, see README of hyperref. –  Heiko Oberdiek Sep 2 '12 at 5:39
add comment

1 Answer

Since you are setting secnumdepth to zero, no anchors are generated for the hyperlinks and this produces the undesired effect mentioned; the following code works as expected:

\documentclass{report}
\usepackage[linktoc=all]{hyperref}
\usepackage{titlesec}
\hypersetup{
    colorlinks,
    citecolor=black,
    filecolor=black,
    linkcolor=black,
    urlcolor=black
}

\begin{document}
\tableofcontents
\chapter{test}

Test text
\newpage
\section{A section}

Lorem Ipsum
\newpage
\subsection{This link is correct}

\end{document}

If you want to have all your sections and subsections to be unnumbered but included in the ToC and stll generating the correct hyperlinks, one option would be to use the \phantomsection command to generate the anchors:

\documentclass{report}
\usepackage[linktoc=all]{hyperref}
\setcounter{secnumdepth}{0}
\usepackage{titlesec}
\hypersetup{
    colorlinks,
    citecolor=black,
    filecolor=black,
    linkcolor=black,
    urlcolor=black
}

\begin{document}
\tableofcontents
\chapter{test}

Test text
\newpage
\phantomsection
\section{A section}

Lorem Ipsum
\newpage
\phantomsection
\subsection{This link is correct}

\end{document}

Another option would be to use the titlesec and titletoc packages to remove the numbering from both the document body and the ToC:

\documentclass{report}
\usepackage[linktoc=all]{hyperref}
\usepackage{titlesec}
\usepackage{titletoc}

\titleformat{\section}
  {\normalfont\Large\bfseries}{}{1em}{}
\titleformat{\subsection}
  {\normalfont\large\bfseries}{}{1em}{}
\titlecontents{section}
  [1.5em] {}{}{}
  {\titlerule*[1em]{.}\contentspage}
\titlecontents{subsection}
  [3.8em] {}{}{}
  {\titlerule*[1em]{.}\contentspage}

\hypersetup{
    colorlinks,
    citecolor=black,
    filecolor=black,
    linkcolor=black,
    urlcolor=black
}

\begin{document}
\tableofcontents
\chapter{test}

Test text
\newpage
\section{A section}

Lorem Ipsum
\newpage
\subsection{This link is correct}

\end{document}

Yet another option would be to use the starred versions of the sectional units (i.e., \section*, \subsection*) and to use \addcontentsline to manually add the entries to the ToC:

\documentclass{report}
\usepackage[linktoc=all]{hyperref}

\hypersetup{
    colorlinks,
    citecolor=black,
    filecolor=black,
    linkcolor=black,
    urlcolor=black
}

\begin{document}
\tableofcontents
\chapter{test}

Test text
\newpage
\section*{A section}
\addcontentsline{toc}{section}{A section}

Lorem Ipsum
\newpage
\subsection*{This link is correct}
\addcontentsline{toc}{subsection}{This link is correct}

\end{document}
share|improve this answer
add comment

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.