In this answer I have been suggested to patch hyperref’s automatic pdfauthor metadata handling mechanism as follows. The goal is to have PDF metadata work with authblk and multiple authors.
\documentclass{article}
\usepackage[pdfusetitle]{hyperref}
\usepackage{authblk}
\usepackage{xpatch}
\newtoggle{patchhref}
\toggletrue{patchhref}
%\iftoggle{patchhref}{
\xpretocmd{\author}{\addhrauthor{#2}}{}{}
\newif\iffirstauthor
\firstauthortrue
\newcommand{\addhrauthor}[1]{%
\iffirstauthor%
\newcommand{\hrauthor}{#1}\firstauthorfalse%
\else%
\xapptocmd{\hrauthor}{, #1}{}{}%
\fi
}
\AtEndDocument{
\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}
\maketitle
Content.
\end{document}
I have added the iftoggle thing, it was not in the original answer. When not using iftoggle, it works. But, when using iftoggle (uncomment the three related lines to try it out), it fails when reaching the end of the document with Undefined control sequence. <argument> \hrauthor. As if the commands that define hrauthor had not been executed. But the part about \AtEndDocument does get executed. According to this comment, replacing iftoggle with legacy if construct works.
How can I make this patch work conditionally, using iftoggle, the preferred mechanism for conditional toggling?
#2(and if you had added e.g. a\failcommand you would have get a suitable error message\xpretocmd{\author}{\addhrauthor{#2}}{}{\fail}). – Ulrike Fischer Feb 18 '19 at 17:30