jgm / pandoc

Universal markup converter
https://pandoc.org
Other
34.54k stars 3.38k forks source link

auto_identifiers generates hypertarget that is unbound to target chapter in PDF rendering #5626

Open Ricq opened 5 years ago

Ricq commented 5 years ago

As described in this issue for the Eisvogel template I'm having issues with the way auto_identifiers generates hypertargets for chapters. The hypertarget is generated as an element in Latex just before the chapter element, but in the PDF conversion this hypertarget becomes linked to the paragraph before the chapter instead of the heading itself. In this way, hyperlinks to chapters are always targeting the page before, or in my two-sides book case, frequently one-and-a-half page before the chapter. This is quite confusing and annoying for the reader clicking the link.

To repeat what I wrote there: I'm building a book with multiple chapters, where I use Pandoc auto_identifiers function to be able to reference headings. I'm creating a two-sided book, where each chapter is on a new blank right page.

For example, a heading target in my markdown is:

# Scope and boundaries (0002)
text here

I reference that chapter somewhere further in the book, like:

See chapter [Scope](#scope-and-boundaries-0002).

When I build a PDF (via Latex and Eisvogel), open the PDF and click the link, the link resolves to the previous page (with the blank left page that may be inserted because of the two-sided book, even the before last page). It seems that the hyperlink target is actually bound to the last line of text before the chapter heading. This is quite confusing for the reader.

If we look at the intermediate Latex file, this is what is generated:

...
\begin{figure}
\centering
\includegraphics[width=0.85\textwidth,height=\textheight]{./images/mygraph.png}
\caption{MY GRAPH}
\end{figure}

\hypertarget{scope-and-boundaries-0002}{%
\chapter{Scope and boundaries (0002)}\label{scope-and-boundaries-0002}}
...

It seems to me that the target is not linked to the chapter, but considered a seperate element in the layout, and is thereby not bound to the chapter text, but typeset before the blank page of the chapter.

Would it be possible in some way to make the hypertarget bind to the actual position of the heading in the resulting PDF?

Example: clicking the link currently navigates me to the blue arrow, while I want to be directed to the red arrow.

image

The Eisvogel template author referred me here, and I guess I agree with him. Would it be possible for Pandoc's auto_identifiers extension to create a hypertarget that is tied to the actual heading?

P.S.: I'm using Pandoc 2.7.2.

jgm commented 5 years ago

This is mostly a tex question. I would have thought that the syntax pandoc is generated would bind the hypertarget to the chapter heading. If there's a better way to do it, we can adjust the latex writer accordingly.

owickstrom commented 5 years ago

Having the same issue, but with the default template. If I render to an intermediate .tex file and manually edit that, moving the \hypertarget after the \chapter directive, the link instead points to just below the heading.

Instead of:

\hypertarget{section-1}{%
\chapter{Section 1}\label{section-1}}

I change it to:

\chapter{Section 1}\hypertarget{section-1}{\label{section-1}}

This is perhaps not ideal, but better than ~1,5 page too early, as @Ricq also notes. But if it's possible to get it at the heading, that would be ideal. My LaTeX-foo isn't strong enough to help with that, I'm afraid.