• pgfplots: link to top of embedded page

    From Holger Schieferdecker@21:1/5 to All on Mon Sep 23 13:35:25 2024
    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}
    %%%%%%%%%%%%%%%%%%

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Holger Schieferdecker@21:1/5 to All on Mon Sep 30 11:32:11 2024
    Am 28.09.2024 um 02:35 schrieb Ulrich D i e z:
    Holger Schieferdecker wrote:

    Am 27.09.2024 um 04:51 schrieb Ulrich D i e z:
    Holger Schieferdecker wrote:

    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

    \NR@gettitle{#3}%

    and replace each call to

    \phantomsection

    by

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

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ulrich D i e z@21:1/5 to Holger Schieferdecker on Mon Sep 30 21:48:31 2024
    Holger Schieferdecker wrote:

    Thank you very much. Now I have to decide which version I like better :-)

    You are welcome.

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ulrich D i e z@21:1/5 to Holger Schieferdecker on Fri Sep 27 04:51:30 2024
    Holger Schieferdecker wrote:

    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 pdfpages@includegraphics@status\endcsname
    \else 0\fi
    =1 %
    \global\firstpicturefalse
    \vbox{%
    \ifAM@landscape\else
    \hbox{\kern-\baselineskip\phantomsection}%
    \hrule height 0pt
    \fi
    \hbox{\includegraphicscopy##1{##2}}%
    \ifAM@landscape
    \hrule height 0pt
    \hbox{%
    \def\HyperRaiseLinkDefault{-\baselineskip}%
    \kern-\baselineskip
    \phantomsection
    \kern\baselineskip
    }%
    \fi
    \addcontentsline{#1}{#2}{#3}%
    \NR@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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Holger Schieferdecker@21:1/5 to All on Fri Sep 27 14:50:02 2024
    Am 27.09.2024 um 04:51 schrieb Ulrich D i e z:
    Holger Schieferdecker wrote:

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ulrich D i e z@21:1/5 to Holger Schieferdecker on Sat Sep 28 02:35:29 2024
    Holger Schieferdecker wrote:

    Am 27.09.2024 um 04:51 schrieb Ulrich D i e z:
    Holger Schieferdecker wrote:

    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

    \NR@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\pdfpages@includegraphics@status
    \else 0\fi
    =1 %
    \global\firstpicturefalse
    \vbox{%
    \ifAM@landscape\else
    \hbox{%
    \kern-\baselineskip
    \refstepcounter{#2}%
    \addcontentsline{#1}{#2}{%
    \protect\numberline{\@currentlabel}#3%
    }%
    }%
    \hrule height 0pt
    \fi
    \hbox{\includegraphicscopy##1{##2}}%
    \ifAM@landscape
    \hrule height 0pt
    \hbox{%
    \def\HyperRaiseLinkDefault{-\baselineskip}%
    \kern-\baselineskip
    \refstepcounter{#2}%
    \addcontentsline{#1}{#2}{%
    \protect\numberline{\@currentlabel}#3%
    }%
    \kern\baselineskip
    }%
    \fi
    \NR@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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Holger Schieferdecker@21:1/5 to All on Wed Oct 30 11:53:57 2024
    Am 27.09.2024 um 04:51 schrieb Ulrich D i e z:
    Holger Schieferdecker wrote:

    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 <ud.usenetcorrespondence@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$1@solani.org>
    \makeatletter
    \newif\iffirstpicture
    \newcommand\HookIntoIncludegraphics[4]{%
    \let\includegraphicscopy=\includegraphics
    \def\includegraphics##1##{\MyIncludegraphicsB{##1}}%
    \firstpicturetrue
    \def\MyIncludegraphicsB##1##2{%
    \ifnum
    \iffirstpicture\csname pdfpages@includegraphics@status\endcsname
    \else 0\fi
    =1 %
    \global\firstpicturefalse
    \vbox{%
    \ifAM@landscape\else
    \hbox{%
    \kern-\baselineskip
    \refstepcounter{#2}%
    \addcontentsline{#1}{#2}{%
    \protect\numberline{\@currentlabel}#3%
    }%
    }%
    \hrule height 0pt
    \fi
    \hbox{\includegraphicscopy##1{##2}}%
    \ifAM@landscape
    \hrule height 0pt
    \hbox{%
    \def\HyperRaiseLinkDefault{-\baselineskip}%
    \kern-\baselineskip
    \refstepcounter{#2}%
    \addcontentsline{#1}{#2}{%
    \protect\numberline{\@currentlabel}#3%
    }%
    \kern\baselineskip
    }%
    \fi
    \NR@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}
    %%%%%%%%%%%%%%%

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)