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.
\documentclass[12pt,a4paper]{mwrep}
\usepackage{lipsum}
\usepackage[nodisplayskipstretch]{setspace} \setstretch{1.3}
\usepackage[hidelinks,unicode,pdfencoding=auto]{hyperref}
\usepackage[all]{hypcap}
\begin{document}
\tableofcontents
\chapter{Chapter A}
\section{Section AA}
\lipsum
\section{Section AB}
\lipsum
Text

Text

Text

Text

Text

Text

\subsection{Subsection ABA}
\lipsum
\chapter{Chapter B}
\section{Section BA}
\lipsum
\subsection{Subsection BAA}
\lipsum
\end{document}

Try to click on PDF bookmark or hyperlink in table of contents (Section AB, Subsection ABA, Subsection BAA). It leads to the bottom of the previous page in case when appropriate [sub]section header is at the top of a page. This is annoying when reading PDF documents in "full page mode" as it goes to the wrong page (the previous one). For some reason the problem does not affect chapter headers. How can it be fixed?

share|improve this question
    
I suspect bad interaction between mwrep and hyperref. –  egreg Mar 8 '12 at 23:57
    
@egreg I have also encountered this issue with the article and the KOMA scrartcl classes –  ArTourter Mar 9 '12 at 0:05
1  
@ArTourter If I change the class to report and add a couple of lines before the subsection title so that it goes to the next page, the anchor is correct. –  egreg Mar 9 '12 at 0:11
1  
Here is a hack that seems to work: Use the needspace package and add \needspace{3\baselineskip} before \section -- the number needs to be determined, but this works for this example. This can be automated to be applied before each \section. –  Peter Grill Mar 10 '12 at 18:06
    
I think that is what @PeterGrill was suggesting by saying the solution can be 'automated'. –  jon Mar 11 '12 at 22:41
show 1 more comment

1 Answer

up vote 6 down vote accepted
+100

Here is an automated solution using the hack that I commented earlier using the needspace package. The \section command is redefined to insert \needspace{3\baselineskip} before each section. This ensures that there ar at least three lines available, otherwise the section is begun on a new page. I think three should be adequate, but if not it can be increased.

So adding this to the preamble fixes the problem with your MWE:

\usepackage{needspace}
\let\OldSection\section%
\renewcommand{\section}[1]{\needspace{3\baselineskip}\OldSection{#1}}%

Here is the complete MWE:

\documentclass[12pt,a4paper]{mwrep}
\usepackage{lipsum}
\usepackage[nodisplayskipstretch]{setspace} \setstretch{1.3}
\usepackage[hidelinks,unicode,pdfencoding=auto]{hyperref}
\usepackage[all]{hypcap}

\usepackage{needspace}
\let\OldSection\section%
\renewcommand{\section}[1]{\needspace{3\baselineskip}\OldSection{#1}}%

\begin{document}
\tableofcontents
\chapter{Chapter A}
\section{Section AA}
\lipsum
\section{Section AB}
\lipsum
Text

Text

Text

Text

Text

Text

\subsection{Subsection ABA}
\lipsum
\chapter{Chapter B}
\section{Section BA}
\lipsum
\subsection{Subsection BAA}
\lipsum
\end{document}
share|improve this answer
    
I didn't need 3 \baselineskips. 1 was enough. Thanks. –  Ichibann Mar 12 '12 at 17:45
    
@Ichibann: Yes 1 works in this case, but in other cases where there is a bit more room at the bottom of the page, but not enough to hold the section header and the first line, then a larger number may be required. So, just keep that in mind... –  Peter Grill Mar 12 '12 at 17:50
    
This is very useful. One thing though, for me, if I put the definition in the preamble, it gives an error after the table of content (which is a \section* by default in my class, ie. scrartcl) putting the definition after \tableofcontent fixes the problem. I also added a definition for subsection and subsubsection. –  ArTourter Apr 4 '12 at 1:25
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.