7

In my document I have boxes that cannot be broken across pages, and which therefore often leave blank space on the preceding pages. When I create a \phantomsection before one of these boxes, the \phantomsection/\label is left on the first page rather than being placed at the top of the new page. Is there a way to get it into the second page?

\documentclass{article}
\usepackage{lipsum,hyperref}
\begin{document}
\hyperref[target]{My link}
\lipsum[1-4]
\phantomsection\label{target} % this stays on page 1
\vbox{\lipsum[1]}
\end{document}
  • Add a \clearpage before \phantomsection – karlkoeller Dec 22 '14 at 8:01
  • That would indeed work for this particular instance, but I would like a more general solution if possible. I don't want to have to recheck it every time the pagination changes. – adam.baker Dec 22 '14 at 8:25
7

The cause for the possible page break is the \label command that inserts a whatsit between the anchor and the box.

If I compile the following example

\documentclass{article}
\usepackage{hyperref}
\begin{document}
\hyperref[target]{My link}   

\phantomsection\label{target}\nopagebreak% this stays on page 1
\vbox{bbb}
\showoutput
\end{document}

the relevant part of the log file is

....\pdfdest name{section*.1} xyz
....\penalty 10000
....\write1{\newlabel{target}{{}{\thepage }{}{section*.1}{}}}
....\penalty 10000
....\glue(\baselineskip) 3.11111
....\vbox(6.94444+0.0)x345.0

We see that an infinite penalty is added before the glue (that in your case will probably come from \lineskip, but this is irrelevant), thus inhibiting this glue to be used as page break point.

If the \nopagebreak is removed, we get

....\penalty 10000
....\write1{\newlabel{target}{{}{\thepage }{}{section*.1}{}}}
....\glue(\baselineskip) 3.11111
....\vbox(6.94444+0.0)x345.0

The whatsit (\write1) is not a discardable item, so the glue can be used as a page break point.

So

\phantomsection\label{target}\nopagebreak

should do.

5
\documentclass{article}
\usepackage{lipsum,hyperref}
\begin{document}
\hyperref[target]{My link}
\lipsum[1-4]
\filbreak\phantomsection\label{target}\nopagebreak % 
\vbox{\lipsum[1]}
\end{document}
4

You can put the \phantomsection inside the box to avoid that, i.e.

\vbox{\phantomsection\label{target}\lipsum[1]}

instead of

\phantomsection\label{target}
\vbox{\lipsum[1]}

MWE

\documentclass{article}
\usepackage{lipsum,hyperref}
\begin{document}
\hyperref[target]{My link}
\lipsum[1-4]
\vbox{\phantomsection\label{target}\lipsum[1]}
\end{document} 

Now the link points to the right place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.