I have \pagerfer pointing to the code listing on the next page. The listing is equipped with \label. When I generate PDF from the TeX file using

latexmk -pdflatex='pdflatex -shell-escape -interaction nonstopmode' -pdf -bibtex -f %f

command, the reference shows correct page number, but when clicked jumps to the wrong page. My initial problem was that the code listing didn't fit on the remaining space of the page, and so I added \newpage before it. This fixed the number in the \pageref, but not the behavior.

Below are the "interesting" bits, which I think are relevant to the problem:

\subsection{Larger example}


Whoa, this was a lot of code (see page \pageref{if_pressed}) [...]


if_pressed(Button, modifiers(Playback, Position, Content, Mode), NextState):-
    ( ... ).
  • 1
    Have you tried inserting the instruction \phantomsection between \newpage and \begin{minted}? – Mico Sep 25 '14 at 16:45
  • When asking questions it is better to provide a full minimal working example (MWE) both in order to demonstrate what you are trying to do and to help others help you. The MWE should like like \documentclass...\begin{document}...\end{document}, it should compile and contain close to the minimal amount of code needed to explain/demonstrate what you are asking. This saves a lot of time for everyone. – user30471 Sep 25 '14 at 21:05
  • It might help to look at the aux file and see what the line for if_pressed contains. – John Kormylo Sep 26 '14 at 0:08
  • Sorry, took me a while to come back to this. Nope, I didn't try the \phantomsection, but that did help! Didn't know about such thing. Please, @Mico, feel free to post it as an answer. – wvxvw Sep 26 '14 at 21:33

I suggest you insert the instruction


between \newpage and \begin{minted}. The purpose of this instruction is to provide an "anchor" of sorts for hyperref, so that a cross-reference to an object that doesn't have an automatic counter associated with it will still take you to the intended location within the document.

  • Having the same problem, I tried inserting '\phantomsection' right before '\label' and it worked. So I thought of redefining '\label' but... is there a reason that \label does not automatically insert itself a '\phantomsection'? – A.G. Oct 16 '14 at 19:18
  • @A.G. - The macro \label has been around far longer than the hyperref package, and it has to work regardless of whether hyperref is loaded or not. If \label is associated with an entity -- e.g. \section and \caption -- or environment -- e.g., equation, theorem, etc -- that increments an associated encounter, the instruction \phantomsection is redundant. The only time you really need \phantomsection is if you want to create a hyperlink to an unnumbered \sectioning header or to an environment (say, a minted environment) that's not numbered. – Mico Oct 16 '14 at 19:45

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.