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'm trying to have links to lines in the table of contents, as suggested in, e.g., this question. It seems like the standard answer is to include lines like

\addtocontents{toc}{\protect\hypertarget{label}{}}

in the appropriate part of the document. This works great except that the table of contents doesn't seem willing to have page breaks between subsections that have this line between them, which leads to awkward spacing. I've posted an example below.

\documentclass[12pt,letterpaper]{article}
\usepackage[colorlinks,linkcolor=black]{hyperref}

\begin{document}

x\vspace{5.5in}
\tableofcontents

\section{A}
\section{B}
\addtocontents{toc}{\protect\hypertarget{moo}{}}
\subsection{BA}
\addtocontents{toc}{\protect\hypertarget{moo}{}}

\subsection{Pagebreak should go below here}
% but it doesn't if this is uncommented
%\addtocontents{toc}{\protect\hypertarget{moo}{}}

\subsection{BC}
\addtocontents{toc}{\protect\hypertarget{moo}{}}
\subsection{BD}
\addtocontents{toc}{\protect\hypertarget{moo}{}}
\section{C}


\end{document}

Why is this, and how can I specify that page breaks are OK?

share|improve this question
add comment

1 Answer 1

The answer to your question Why does adding hypertarget to TOC change pagination? is that by adding \addtocontents{toc}{\protect\hypertarget{moo}{}} will add a new line in the .aux file with \@writefile{toc}{\hypertarget {moo}{}}. On the next compilation, an additional empty line with invisible hyper-target will appear in the PDF, which seems to us that the pagination changed.

To overcome this situation, we need to tweak \section, \subsection and what ever section commands which require navigation as given in the below example:

\documentclass[12pt,twoside]{article}
\usepackage[bookmarks=false,colorlinks,linkcolor=black]{hyperref}
\usepackage{lipsum}

\makeatletter
\def\ps@myheading{%
   \def\@oddhead{\mbox{}\hfill\slshape\rightmark}%
   \if@twoside
     \def\@evenhead{\slshape\leftmark\hfill\mbox{}}%
   \else% 
    \let\@evenhead\@oddhead%
   \fi%
   \def\@oddfoot{\mbox{}\hfil\thepage\hfil\mbox{}}%
   \let\@evenfoot\@oddfoot%
}
\def\linksection#1{%
   \section[\string\smash{\string\raisebox{\baselineskip}{\string\hypertarget{moo.\thesection}{}}}#1]{#1}%
   \def\rightmark{\hyperlink{moo.\thesection}{\thesection.\quad #1}}%
   \let\leftmark\rightmark%
}
\def\linksubsection#1{%
   \subsection[\string\smash{\string\raisebox{\baselineskip}{\string\hypertarget{moo.\thesubsection}{}}}#1]{#1}%
   \def\rightmark{\hyperlink{moo.\thesubsection}{\thesubsection.\quad #1}}%
   \let\leftmark\rightmark%
}
\def\nolinksection#1{%
   \section{#1}%
   \def\rightmark{\thesection.\quad #1}%
   \let\leftmark\rightmark%
}
\def\nolinksubsection#1{%
   \subsection{#1}%
   \def\rightmark{\thesubsection.\quad #1}%
   \let\leftmark\rightmark%
}
\pagestyle{myheading}
\makeatother

\begin{document}

\mbox{}\vspace{5.5in}
\tableofcontents

\linksection{A}

\lipsum

\linksection{B}
\lipsum

\nolinksubsection{BA}
\lipsum

\nolinksubsection{Pagebreak should go below here}
\lipsum
% but it doesn't if this is un-commented

\linksubsection{BC}
\lipsum

\linksubsection{BD}
\lipsum

\nolinksection{C}
\lipsum

\end{document}

Defined a new pagestyle (header and footer) named myheading to put the navigation in headers which lead back to the ToC entry. Also, the commands \section, \subsection, etc changed to \linksection, \linksubsection respectively for those sections which required the navigation using hyper-linking. And those \section, \subsection which does not require navigation are defined as \nolinksection, \nolinksubsection respectively. These macros will define respective entries for \hypertarget in the ToC and \hyperlink in the headers.

Hope this helps.

share|improve this answer
    
Yes, it adds another line to the toc. The question is: why does that change the pagination? The visible text is identical; why does the linebreak become "non-pagebreaking" or whatever the term is? –  Eric Price Jul 31 '13 at 4:40
    
Please open and see the filename.toc file. You will find a new entry in the toc file. This will cause one empty line (invisible line) in the table of contents and these empty lines changes the pagination. –  Jagath AR Jul 31 '13 at 4:54
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.