[Tex/LaTex] Error on linking email addresses inside elsarticle template

elsarticlehyperref

I am perplexed by the error:

 Use of \@uad doesn't match its definition.
\kernel@ifnextchar ...rved@d =#1\def \reserved@a {
                                                  #2}\def \reserved@b {#3}\f...
l.11 ...iversity.edu}{mailto:a2@myuniversity.edu}}

Arising in the following minimal tex file using the standard elsarticle class:

\documentclass{elsarticle}
\usepackage{hyperref}
\begin{document}
\begin{frontmatter}
\title{Remarks on something interesting}

\author[myu]{Author 1}
\ead{a1@myuniversity.edu}

\author[myu]{Author 2 \corref{cor1}}
\ead{\href{mailto:a2@myuniversity.edu}{mailto:a2@myuniversity.edu}}

\address[myu]{My Department, My Building, My University in My City}

\cortext[cor1]{Corresponding author}
\end{frontmatter}

\maketitle

A \href{http://google.com}{link to google.com}

\end{document}

It looks as though the class file doesn't support \href arguments here, but it is not obvious to me why that is. So, I'm left with several questions:

  1. What does this error mean?

  2. How I might go about modifying either the .tex or the .cls file so that the above can be compiled successfully?

I've taken a look at where \ead is defined in the class field but lack the tex-foo to figure out what's going on. I believe the relevant lines are:

\def\ead{\@ifnextchar[{\@uad}{\@ead}}
\gdef\@ead#1{\bgroup\def\_{\string\_}\def\{{\string\{}%
   \def\}{\string\}}%
   \edef\tmp{\the\@eadauthor}
   \immediate\write\@auxout{\string\emailauthor
     {#1}{\expandafter\strip@prefix\meaning\tmp}}%
  \egroup
}

The error seems like it might be related to how and where tex defines macros, but that is a bit over my head.

Background: Not directly relevant to this question, but sometimes context is useful. I encounter this when trying to use the elsarticle template in RStudio's rmarkdown, which uses pandoc with the option autolink_bare_uris which apparently cannot be turned off. This forces pandoc to transform bare emails into href'd ones, rather annoyingly breaking this template.

Best Answer

The \ead command in elsarticle writes the \emailauthor command to the aux file with the passed email address and the associated author as arguments. On subsequent runs, the aux file is read and the \emailauthor command is executed. The issue with adding \href as you have tried is that the \immediate\write\@auxout tries to expand it prior to writing the contents to the aux file, resulting in the error. As we want \href to be written to the aux file, you can do what you are after by prefacing \href with \string or \noexpand. For example:

\ead{\string\href{mailto:a2@myuniversity.edu}{a2@myuniversity.edu}}

You can check the aux file to see the following line (as expected):

\emailauthor{\href{mailto:a2@myuniversity.edu}{a2@myuniversity.edu}}{Author 2 \corref {cor1}}

Additionally, you can automate hyperlinking for all author emails and urls by modifying \@ead and \@uad, repsectively, with the following:

\usepackage{etoolbox}
\makeatletter%placed AFTER \usepackage{hyperref}, if to be used
    \newif\ifhyperrefloaded\hyperrefloadedfalse
    \@ifpackageloaded{hyperref}{\hyperrefloadedtrue}{\hyperrefloadedfalse}%check if hyperref is loaded
    \ifhyperrefloaded%
        \patchcmd{\@ead}{#1}{\string\href{mailto:#1}{#1}}{}{}
        \patchcmd{\@uad}{#2}{\string\url{#2}}{}{}\fi
\makeatother

The \ifhyperrefloaded is used to so that the \patchcmd calls are in the global scope and only used if the hyperref package is loaded.

Finally, I took the liberty of removing the mailto: from the displayed portion of \href and the unnecessary \maketitle from your MWE.

The result:

\documentclass{elsarticle}
\usepackage{hyperref}
\usepackage{etoolbox}

\makeatletter%placed AFTER \usepackage{hyperref}, if to be used
    \newif\ifhyperrefloaded\hyperrefloadedfalse
    \@ifpackageloaded{hyperref}{\hyperrefloadedtrue}{\hyperrefloadedfalse}%check if hyperref is loaded
    \ifhyperrefloaded%
        \patchcmd{\@ead}{#1}{\string\href{mailto:#1}{#1}}{}{}
        \patchcmd{\@uad}{#2}{\string\url{#2}}{}{}\fi
\makeatother
\begin{document}
\begin{frontmatter}
\title{Remarks on something interesting}

\author[myu]{Author 1}
\ead{a1@myuniversity.edu}

\author[myu]{Author 2 \corref{cor1}}
\ead{a2@myuniversity.edu}
\ead[url]{www.google.com}

\address[myu]{My Department, My Building, My University in My City}

\cortext[cor1]{Corresponding author}
\end{frontmatter}

A \href{http://google.com}{link to google.com}

\end{document}

linked email and url

Related Question