hyperref’s pdfusetitle option fails with authblk

I would like to use the functionalities provided by the package authblk and get automatically the right author metadata in my PDF. Normally, the pdfusetitle option of hyperref sets the right title and author metadata. With authblk, however, this works for the title but not for authors. I suppose it’s because authblk changes the “author” mechanism.

Can I patch this easily? Or is there a package similar to authblk that is compatible with this functionality of hyperref?

Even better if the patch also works when multiple authors are given, in which case the pdf metadata should show the author names separated by commas.

Here is a minimal example that illustrates the incompatibility. If authblk is loaded before hyperref, and with pdfusetitle, loading hyperref fails with Illegal parameter number in definition of \Hy@author. If loading authblk after hyperref (as illustrated here), the document gets compiled, but contains no author metadata. When uncommenting the hypersetup block, the author metadata reads: immediate LaTeX Warning: No given..

\documentclass{scrartcl}
\usepackage[pdfusetitle]{hyperref}
\usepackage{authblk}

%\makeatletter
%\hypersetup{pdfauthor={\@author}}
%\makeatother

\begin{document}
\title{The title}
\author{Myself}

Content.

\end{document}


Here is an example of my current usage. I would like to get rid of the redundant \hypersetup part.

\title{The title}
\author{Firstname Lastname}
\author{Second author}
\affil{First affiliation\\
\href{mailto:firstname.fastname@affiliation}{firstname.fastname@affiliation}
}
\author{Name3}
\affil{Second affiliation}
\makeatletter
\hypersetup{
pdfauthor={Firstname Lastname, Second author, Name3}
}
\makeatother
\maketitle


The following is an xpatch solution. The list of authors is stored in a separate macro which can be used by \hypersetup. This list is constructed by adding a call to a helper macro at the start of the \author command from authblk. This uses argument #2 because #1 is used for the optional footnote marker.

A conditional is used to check if the current author is the first in the list (then the list is defined as the current name) or a further entry (add a comma and the current entry to the list).

If you put the \hypersetup in the preamble then the list is not yet constructed, and the pdf author metadata will be empty. However, following https://tex.stackexchange.com/a/254516/ you can delay the execution of the \hypersetup command until the end of the document with \AtEndDocument.

MWE:

\documentclass{article}
\usepackage[pdfusetitle]{hyperref}
\usepackage{authblk}
\usepackage{xpatch}
% add a macro call to the start of \author
% define boolean for list construction and set to true
\newif\iffirstauthor
\firstauthortrue
% first entry: author list is defined as the current author, set boolean to false
\iffirstauthor%
\newcommand{\hrauthor}{#1}\firstauthorfalse%
% further entries: add to list
\else%
\xapptocmd{\hrauthor}{, #1}{}{}%
\fi
}
\AtEndDocument{ % delayed call for \hypersetup in preamble
\hypersetup{pdfauthor={\hrauthor}}
}
\begin{document}
\title{The title}
\author{Firstname 1 Lastname 1}
\author{Second author}
\affil{First affiliation\\
\href{mailto:firstname.fastname@affiliation}{firstname.fastname@affiliation}
}
\author{Name3}
\affil{Second affiliation}

%\hypersetup{pdfauthor={\hrauthor}} % direct call after setting the author names

\maketitle
Content.

\end{document}


Result:

• Thanks! One problem: when I put the patch commands in a block (inside an \iftoggle), they do not work. I’d like to patch only conditionally, because I do not necessarily need authblk. Any idea? Also, I’d like to hide all of this in the preamble (thus avoid hypersetup after the titling part), is it possible? Jan 27, 2019 at 19:47
• @OlivierCailloux the only thing the patch does is to store the list of authors in a separate macro. Therefore you can just leave it in when you don't use authblk, then it will patch the regular \author command instead (without any effects on the command itself). Feb 11, 2019 at 10:20
• See edit for the issue of putting commands in the preamble. Feb 11, 2019 at 10:32
• Indeed it does not work conditionally with a toggle (I don't know why) but it does work with a classic if-else-fi (similar to the \iffirstauthor construct). Feb 11, 2019 at 10:46