[Tex/LaTex] Problem with \ead instruction in elsarticle document class

elsarticlefront-matter

I have two identical TeX files written under elsarticle template, with the exception that the names of authors are different. This small difference makes an error for the second code below while for the first one no error appears. In the first code, I have written \author{someone} and \ead{someone@gmail.com} in corresponding places in template, whereas in the second code, I've used another name, say \author{Abbas Karamali Ravandi}, and another email address, say \ead{abbas_karamali@gmail.com}.

Please run each code at least twice to see the error.

First Code:

\documentclass[preprint,12pt]{elsarticle}
\usepackage{epsfig}
\usepackage{subcaption}
\captionsetup{compatibility=false}
%% The amssymb package provides various useful mathematical symbols
\usepackage{amssymb}
\usepackage{mathptmx} 
\usepackage{amsmath}
\newtheorem{thm}{Theorem}
\newproof{pf}{Proof}
\journal{X}

\begin{document}
\begin{frontmatter}
\title{Something as a title}
\author{Someone}
\ead{someone@gmail.com}
\address{}

\begin{abstract}
%% Text of abstract
\end{abstract}

\begin{keyword}
%% keywords here, in the form: keyword \sep keyword
Keyword1 \sep Keyword2 \sep Keyword3 \sep Keyword4
%% PACS codes here, in the form: \PACS code \sep code

%% MSC codes here, in the form: \MSC code \sep code
%% or \MSC[2008] code \sep code (2000 is the default)

\end{keyword}

\end{frontmatter}

%% main text
\section{Introduction}
\label{}
Here goes introduction of the paper

\bibliographystyle{elsarticle-num} 
\bibliography{Bibiliography}

\end{document}
\endinput
%% End of file `elsarticle-template-num.tex'.

and the Second

\documentclass[preprint,12pt]{elsarticle}
\usepackage{epsfig}
\usepackage{subcaption}
\captionsetup{compatibility=false}
%% The amssymb package provides various useful mathematical symbols
\usepackage{amssymb}
\usepackage{mathptmx} 
\usepackage{amsmath}
\newtheorem{thm}{Theorem}
\newproof{pf}{Proof}
\journal{X}

\begin{document}
\begin{frontmatter}
\title{Something as a title}
\author{Abbas Karamali Ravandi}
\ead{abbas_karamali@gmail.com}
\address{}

\begin{abstract}
%% Text of abstract
\end{abstract}

\begin{keyword}
%% keywords here, in the form: keyword \sep keyword
Keyword1 \sep Keyword2 \sep Keyword3 \sep Keyword4
%% PACS codes here, in the form: \PACS code \sep code

%% MSC codes here, in the form: \MSC code \sep code
%% or \MSC[2008] code \sep code (2000 is the default)

\end{keyword}

\end{frontmatter}

%% \linenumbers

%% main text
\section{Introduction}
\label{}
 Here goes introduction of the paper

\bibliographystyle{elsarticle-num} 
\bibliography{Bibiliography}

\end{document}

Best Answer

It is instructive to examine the \ead macro, which is defined by the elsarticle document class:

\def\ead{\@ifnextchar[{\@uad}{\@ead}}
\gdef\@ead#1{\bgroup%
   \def\_{\string\underscorechar\space}%  % <--- crucial
   \def\{{\string\lbracechar\space}%
   \def~{\hashchar\space}%
   \def\}{\string\rbracechar\space}%
   \edef\tmp{\the\@eadauthor}
   \immediate\write\@auxout{\string\emailauthor
     {#1}{\expandafter\strip@prefix\meaning\tmp}}%
  \egroup
}
\newcounter{ead}
\gdef\emailauthor#1#2{\stepcounter{ead}%
      \g@addto@macro\@elseads{\raggedright%
      \let\corref\@gobble
      \eadsep\texttt{#1} (#2)%
      \def\eadsep{\unskip,\space}}%
}

Some of the code is there to deal with the possibility that the document contains more than one \ead instruction. For your document, the crucial thing to recognize is that the \ead macro expects underscores, left curly braces, and right curly braces to be entered as \_, \{, and \}, respectively, i.e., to be "escaped" in (La)TeX jargon. (The ~ character, an "active character" in LaTeX jargon, also gets redefined.)

Thus, you need to enter the instruction as

\ead{abbas\_karamali@gmail.com}

to avoid getting a (nearly incomprehensible) error message.