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

\begin{document}

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

\newpage

\newcounter{inclPDFpage}

\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?

• 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 Dec 22 '17 at 21:54

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}