2
\documentclass{article}
\usepackage{pdfpages}
\usepackage{hyperref}

\begin{document}

\nameref{mypdflabel} on page~\ref{mypdflabel}

\newpage

\newcounter{inclPDFpage}
\includepdf[pages=-,link,linkname="This is the title I want",pagecommand={\refstepcounter{inclPDFpage}\label{mypdflabel}}]{mypdf.pdf}

\end{document}

The output of the above code is simply "on page 2" but I want it to show "This is the title I want on page 2", which is what I thought nameref would do.

Any ideas on how I can get this to work?

1
  • No, this is some kind of misunderstanding. linkname just changes the name of the hyper anchor, but not the title of the link itself. Also you're inserting the same label again since in case of there should be more than one page in your mypdf.pdf
    – user31729
    Commented Dec 22, 2017 at 21:54

1 Answer 1

5

linkname does not provide a name for a page etc, it changes only the hyper anchor name, e.g linkname=foo would give foo.1 etc. where 1 is a page number.

The following example does provide the correct \@currentlabelname stored by \label, which is then extracted with \nameref later on, but please note that in case of having more than page in an included .pdf file, LaTeX will complain about multiple labels with the same name.

\documentclass{article}
\usepackage{pdfpages}
\usepackage{hyperref}

\makeatletter
\newcommand{\providename}[2]{%
  \edef\@currentlabelname{#1}%
  \label{#2}%
}
\makeatother

\begin{document}

\nameref{mypdflabel} on page~\ref{mypdflabel}



\newpage

\newcounter{inclPDFpage}
\includepdf[pages=-,link,pagecommand={\refstepcounter{inclPDFpage}\providename{This is the title I want}{mypdflabel}}]{7.pdf}

\end{document}
1
  • @user31729 you're a genius! Saved my day, thank you very much.. Commented Sep 14, 2022 at 17:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .