Discussion:
pgfplots: link to top of embedded page
Add Reply
Holger Schieferdecker
2024-09-23 11:35:25 UTC
Reply
Permalink
Hi,

I include single page PDF documents with \includepdf. They have the same
size and aspect ratio as the output document (A4). With the option
"addtotoc" I can add an entry to the tableofcontents and the bookmarks.
But clicking on that doesn't jump to the top of the included page.

It seems like the link target set by \includepdf is set with respect of
the page margins of the output document. I didn't find any hints in the
manual of pdfpages, besides that "addtotoc" is experimental. Is there a
way of positioning the link target at the top of the included pdf page?

In the example below the link to the first pdf jumps somewhere inside
the pdf. The link target of the second pdf is a bit above of the
embedded page.

Holger


%%%%%%%%%%%%%%%%%%
\documentclass{article}

\usepackage{graphicx}
\usepackage{pdfpages}
\usepackage{hyperref}

\begin{document}

\tableofcontents

\includepdf[landscape, scale=0.97,
addtotoc={1, section, 1, Example A, sp:ex-a}]{example-image-a}

\includepdf[scale=0.97,
addtotoc={1, section, 1, Example B, sp:ex-b}]{example-image-b}

\clearpage

Nothing here.

\end{document}
%%%%%%%%%%%%%%%%%%
Ulrich D i e z
2024-09-27 02:51:30 UTC
Reply
Permalink
Post by Holger Schieferdecker
I include single page PDF documents with \includepdf. They have the same
size and aspect ratio as the output document (A4). With the option
"addtotoc" I can add an entry to the tableofcontents and the bookmarks.
But clicking on that doesn't jump to the top of the included page.
It seems like the link target set by \includepdf is set with respect of
the page margins of the output document. I didn't find any hints in the
manual of pdfpages, besides that "addtotoc" is experimental. Is there a
way of positioning the link target at the top of the included pdf page?
I think there is no official interface but you can hook into
\includegraphics.



To test behavior with multiple pages I created testpictures.pdf from
testpictures.tex;

\documentclass[multi=true]{standalone}
\usepackage{graphicx}
\newenvironment{img}{}{}
\standaloneenv{img}
\begin{document}
\begin{img}%
\includegraphics[width=8cm]{example-image-a}%
\end{img}
\begin{img}%
\includegraphics[width=8cm]{example-image-b}%
\end{img}
\begin{img}%
\includegraphics[width=8cm]{example-image-c}%
\end{img}
\end{document}



This is how I hook into \includegraphics to get a bookmark and a
toc-entry for the first page of the included pdf-file:


\documentclass{article}

\usepackage{graphicx}
\usepackage{pdfpages}
\usepackage{hyperref}

\makeatletter
\newif\iffirstpicture
\newcommand\HookIntoIncludegraphics[4]{%
\let\includegraphicscopy=\includegraphics
\def\includegraphics##1##{\MyIncludegraphicsB{##1}}%
\firstpicturetrue
\def\MyIncludegraphicsB##1##2{%
\ifnum
\iffirstpicture\csname ***@includegraphics@status\endcsname
\else 0\fi
=1 %
\global\firstpicturefalse
\vbox{%
\***@landscape\else
\hbox{\kern-\baselineskip\phantomsection}%
\hrule height 0pt
\fi
\hbox{\includegraphicscopy##1{##2}}%
\***@landscape
\hrule height 0pt
\hbox{%
\def\HyperRaiseLinkDefault{-\baselineskip}%
\kern-\baselineskip
\phantomsection
\kern\baselineskip
}%
\fi
\addcontentsline{#1}{#2}{#3}%
\***@gettitle{#3}%
\label{#4}%
}%
\else
\includegraphicscopy##1{##2}%
\fi
}%
}%
\makeatother

\begin{document}

\tableofcontents

\bigskip

\noindent \verb|\nameref|-links:

\bigskip

\nameref{sp:ex-a}%

\medskip

\nameref{sp:ex-b}%

\medskip

\nameref{sp:ex-c}%

\begingroup
\HookIntoIncludegraphics{toc}{section}{Example A}{sp:ex-a}%
\includepdf[landscape, scale=0.97, pages=1]{testpictures.pdf}%
\endgroup

\begingroup
\HookIntoIncludegraphics{toc}{section}{Example A, B, C}{sp:ex-b}%
\includepdf[scale=0.97, pages=-]{testpictures.pdf}%
\endgroup

\begingroup
\HookIntoIncludegraphics{toc}{section}{Example B, C}{sp:ex-c}%
\includepdf[landscape, scale=0.97, pages=2-3]{testpictures.pdf}%
\endgroup

Nothing here.

\end{document}



Sincerely

Ulrich
Holger Schieferdecker
2024-09-27 12:50:02 UTC
Reply
Permalink
Post by Ulrich D i e z
Post by Holger Schieferdecker
I include single page PDF documents with \includepdf. They have the same
size and aspect ratio as the output document (A4). With the option
"addtotoc" I can add an entry to the tableofcontents and the bookmarks.
But clicking on that doesn't jump to the top of the included page.
It seems like the link target set by \includepdf is set with respect of
the page margins of the output document. I didn't find any hints in the
manual of pdfpages, besides that "addtotoc" is experimental. Is there a
way of positioning the link target at the top of the included pdf page?
I think there is no official interface but you can hook into
\includegraphics.
Many thanks for your help, that works. There is one difference in the
toc entries. These don't get a number. But I consider that even better.

In my case I have a section in an appendix where three pages are
included as subsection. There is no separate heading for each of them.
So numbers in the toc and bookmarks are not needed.

Holger
Ulrich D i e z
2024-09-28 00:35:29 UTC
Reply
Permalink
Post by Holger Schieferdecker
Post by Ulrich D i e z
Post by Holger Schieferdecker
I include single page PDF documents with \includepdf. They have the same
size and aspect ratio as the output document (A4). With the option
"addtotoc" I can add an entry to the tableofcontents and the bookmarks.
But clicking on that doesn't jump to the top of the included page.
It seems like the link target set by \includepdf is set with respect of
the page margins of the output document. I didn't find any hints in the
manual of pdfpages, besides that "addtotoc" is experimental. Is there a
way of positioning the link target at the top of the included pdf page?
I think there is no official interface but you can hook into
\includegraphics.
Many thanks for your help, that works. There is one difference in the
toc entries. These don't get a number. But I consider that even better.
If you wish numbered sections in the table of contents, then remove the
line

\addcontentsline{#1}{#2}{#3}%

right before

\***@gettitle{#3}%

and replace each call to

\phantomsection

by

\refstepcounter{#2}%
\addcontentsline{#1}{#2}{\protect\numberline{\@currentlabel}#3}%

.






\documentclass{article}

\usepackage{graphicx}
\usepackage{pdfpages}
\usepackage{hyperref}

\makeatletter
\newif\iffirstpicture
\newcommand\HookIntoIncludegraphics[4]{%
\let\includegraphicscopy=\includegraphics
\def\includegraphics##1##{\MyIncludegraphicsB{##1}}%
\firstpicturetrue
\def\MyIncludegraphicsB##1##2{%
\ifnum
\iffirstpicture\***@includegraphics@status
\else 0\fi
=1 %
\global\firstpicturefalse
\vbox{%
\***@landscape\else
\hbox{%
\kern-\baselineskip
\refstepcounter{#2}%
\addcontentsline{#1}{#2}{%
\protect\numberline{\@currentlabel}#3%
}%
}%
\hrule height 0pt
\fi
\hbox{\includegraphicscopy##1{##2}}%
\***@landscape
\hrule height 0pt
\hbox{%
\def\HyperRaiseLinkDefault{-\baselineskip}%
\kern-\baselineskip
\refstepcounter{#2}%
\addcontentsline{#1}{#2}{%
\protect\numberline{\@currentlabel}#3%
}%
\kern\baselineskip
}%
\fi
\***@gettitle{#3}%
\label{#4}%
}%
\else
\includegraphicscopy##1{##2}%
\fi
}%
}%
\makeatother

\begin{document}

\tableofcontents

\bigskip

\noindent \verb|\nameref|-links:

\bigskip

\nameref{sp:ex-a}%

\medskip

\nameref{sp:ex-b}%

\medskip

\nameref{sp:ex-c}%

\begingroup
\HookIntoIncludegraphics{toc}{section}{Example A}{sp:ex-a}%
\includepdf[landscape, scale=0.97, pages=1]{testpictures.pdf}%
\endgroup

\begingroup
\HookIntoIncludegraphics{toc}{section}{Example A, B, C}{sp:ex-b}%
\includepdf[scale=0.97, pages=-]{testpictures.pdf}%
\endgroup

\begingroup
\HookIntoIncludegraphics{toc}{section}{Example B, C}{sp:ex-c}%
\includepdf[landscape, scale=0.97, pages=2-3]{testpictures.pdf}%
\endgroup

Nothing here.

\end{document}



Sincerely

Ulrich
Holger Schieferdecker
2024-09-30 09:32:11 UTC
Reply
Permalink
Post by Ulrich D i e z
Post by Holger Schieferdecker
Post by Ulrich D i e z
Post by Holger Schieferdecker
I include single page PDF documents with \includepdf. They have the same
size and aspect ratio as the output document (A4). With the option
"addtotoc" I can add an entry to the tableofcontents and the bookmarks.
But clicking on that doesn't jump to the top of the included page.
It seems like the link target set by \includepdf is set with respect of
the page margins of the output document. I didn't find any hints in the
manual of pdfpages, besides that "addtotoc" is experimental. Is there a
way of positioning the link target at the top of the included pdf page?
I think there is no official interface but you can hook into
\includegraphics.
Many thanks for your help, that works. There is one difference in the
toc entries. These don't get a number. But I consider that even better.
If you wish numbered sections in the table of contents, then remove the
line
\addcontentsline{#1}{#2}{#3}%
right before
and replace each call to
\phantomsection
by
\refstepcounter{#2}%
Thank you very much. Now I have to decide which version I like better :-)

By the way, how fragile (or not fragile) is your solution? I mean, is it
likely to break with possible future changes of pdfpages or graphicx? Of
course one never knows what could change. But as far as I understand
your helper macro \HookIntoIncludegraphics I would assume it is rather
unlikely to break.

And finally I wonder why I wrote 'pgfplots' in the subject... :-/

Holger
Ulrich D i e z
2024-09-30 19:48:31 UTC
Reply
Permalink
Post by Holger Schieferdecker
Thank you very much. Now I have to decide which version I like better :-)
You are welcome.
Post by Holger Schieferdecker
By the way, how fragile (or not fragile) is your solution? I mean, is it
likely to break with possible future changes of pdfpages or graphicx? Of
course one never knows what could change. But as far as I understand
your helper macro \HookIntoIncludegraphics I would assume it is rather
unlikely to break.
I can't make any statements or predictions about what changes to the
pdfpages package are planned and how they will interfere with the macro
suggested by me. I also don't know what is planned for hyperref and the
LaTeX kernel - for example whether the current infrastructure for
cross-referencing and creating bookmarks will be retained in the
middle-term or long run.

However, since, when patching `\includegraphics` in the local scope,
only things are used that belong to the documented official interfaces,
I'm optimistic that changes under the hood on pdfpages/hyperref/the
LaTeX kernel in the near future won't affect the macro suggested by me.

But I would still like to say that my focus was more on a scenario where
a workaround is used to get a job done in the short term, not on a
permanent long-standing solution.

Sincerely

Ulrich
Holger Schieferdecker
2024-10-30 10:53:57 UTC
Reply
Permalink
Post by Ulrich D i e z
Post by Holger Schieferdecker
I include single page PDF documents with \includepdf. They have the same
size and aspect ratio as the output document (A4). With the option
"addtotoc" I can add an entry to the tableofcontents and the bookmarks.
But clicking on that doesn't jump to the top of the included page.
It seems like the link target set by \includepdf is set with respect of
the page margins of the output document. I didn't find any hints in the
manual of pdfpages, besides that "addtotoc" is experimental. Is there a
way of positioning the link target at the top of the included pdf page?
I think there is no official interface but you can hook into
\includegraphics.
In the meantime I wrote to the author of pdfpages and asked if your
workaround could be used to create a solution internally in the package.
Unfortunately he found out that your macro has a problem. For the
landscape version a negative y coordinate is created. Some PDF viewers
like Evince and Atril he used for his test jump to the end of the page.
I used SumatraPDF and Adobe Reader which jump to the beginning of the
page like it is desired.

He also mentioned that some people use the geometry package and
temporarily change the type area. His suggestion was to use something like

\newgeometry{top=0pt, headheight=0pt, headsep=0pt, vscale=1}
\includepdf{file.pdf}

In my tests I tried with \newgeometry{margin=0pt}.

In the example below there are 4 documents embedded. Example A shows the
problem with the negative y coordinate for some viewers. Example C1 and
C2 are created with the help of geometry. This works for the landscape
document on a landscape page (C1), but not for the landscape document on
a portrait page (C2). Of course this is obvious as the page has no
margins and the link then is at the very top.

Anyway, I can solve my limited use case. There are just landscape
documents on a landscape page.

Holger

%%%%%%%%%%%%%%
\documentclass{article}

\usepackage{geometry}
\usepackage{graphicx}
\usepackage{pdfpages}
\usepackage{hyperref}

%% Get links to top of images included with \includepdf
%From: Ulrich D i e z <***@web.de>
%Newsgroups: comp.text.tex
%Subject: Re: pgfplots: link to top of embedded page
%Date: Sat, 28 Sep 2024 02:35:29 +0200
%Message-ID: <vd7j0j$voaf$***@solani.org>
\makeatletter
\newif\iffirstpicture
\newcommand\HookIntoIncludegraphics[4]{%
\let\includegraphicscopy=\includegraphics
\def\includegraphics##1##{\MyIncludegraphicsB{##1}}%
\firstpicturetrue
\def\MyIncludegraphicsB##1##2{%
\ifnum
\iffirstpicture\csname ***@includegraphics@status\endcsname
\else 0\fi
=1 %
\global\firstpicturefalse
\vbox{%
\***@landscape\else
\hbox{%
\kern-\baselineskip
\refstepcounter{#2}%
\addcontentsline{#1}{#2}{%
\protect\numberline{\@currentlabel}#3%
}%
}%
\hrule height 0pt
\fi
\hbox{\includegraphicscopy##1{##2}}%
\***@landscape
\hrule height 0pt
\hbox{%
\def\HyperRaiseLinkDefault{-\baselineskip}%
\kern-\baselineskip
\refstepcounter{#2}%
\addcontentsline{#1}{#2}{%
\protect\numberline{\@currentlabel}#3%
}%
\kern\baselineskip
}%
\fi
\***@gettitle{#3}%
\label{#4}%
}%
\else
\includegraphicscopy##1{##2}%
\fi
}%
}%
\makeatother


\begin{document}

\tableofcontents

\begingroup
\HookIntoIncludegraphics{toc}{section}{Example A}{sp:ex-a}%
\includepdf[landscape, scale=0.97]{example-image-a}
\endgroup

\begingroup
\HookIntoIncludegraphics{toc}{section}{Example B}{sp:ex-b}%
\includepdf[scale=0.97]{example-image-b}
\endgroup

\begingroup
\newgeometry{margin=0pt}
\includepdf[landscape, scale=0.97,
addtotoc={1, section, 1, Example C1, sp:ex-c1}]{example-image-c}

\includepdf[scale=0.97,
addtotoc={1, section, 1, Example C2, sp:ex-c2}]{example-image-c}
\endgroup

\clearpage

Nothing here.

\end{document}
%%%%%%%%%%%%%%%

Loading...