29

I have found this answer by Will Robertson on how to typeset an email address:

\documentclass{article}
\usepackage{color,hyperref}

\catcode`\_=11\relax
\newcommand\email[1]{\_email #1\q_nil}
\def\_email#1@#2\q_nil{%
  \href{mailto:#1@#2}{{\emailfont #1\emailampersat #2}}
}
\newcommand\emailfont{\sffamily}
\newcommand\emailampersat{{\color{red}\small@}}
catcode`\_=8\relax    

\begin{document}
\email{foobar@gmail.com}
\end{document}

The problem is that it doesn't work if there is an underscore in the address. How do I add such a feature?

22

The easiest way is to detokenize the text:

\documentclass{article}
\usepackage{color,hyperref}
    \catcode`\_=11\relax
    \newcommand\email[1]{\_email #1\q_nil}
    \def\_email#1@#2\q_nil{%
      \href{mailto:#1@#2}
        {{\emailfont\detokenize{#1}\emailampersat\detokenize{#2}}}%
    }
    \newcommand\emailfont{\sffamily}
    \newcommand\emailampersat{{\color{red}\small@}}
    \catcode`\_=8\relax   
\usepackage[T1]{fontenc}

\begin{document}
\email{foo_bar@gmail.com}
\end{document}
24

You can use url package for this.

\documentclass{article}
\usepackage[colorlinks]{hyperref}
\usepackage[T1]{fontenc}
\DeclareUrlCommand\email{\urlstyle{rm}}

\begin{document}
\email{foo_bar@gmail.com}
\end{document}

enter image description here

18

An expl3 implementation:

\documentclass{article}
%\usepackage[T1]{fontenc}
\usepackage{color,xparse,l3regex,hyperref}

\ExplSyntaxOn
\NewDocumentCommand{\email}{m}
 {
  \svend_email:n { #1 }
 }
\tl_new:N \l_svend_email_tl
\seq_new:N \l_svend_email_seq
\cs_generate_variant:Nn \seq_set_split:Nnn { NnV }
\cs_new_protected:Npn \svend_email:n #1
 {
  \tl_set:Nn \l_svend_email_tl { #1 }
  \regex_replace_all:nnN { \cD. } { \c{textunderscore} } \l_svend_email_tl
  \seq_set_split:NnV \l_svend_email_seq { @ } \l_svend_email_tl
  \href{ mailto:#1 }{ \emailfont \seq_use:Nn \l_svend_email_seq { \emailat } }
 }
\ExplSyntaxOff
\newcommand\emailfont{\sffamily}
\newcommand\emailat{{\color{red}\small @}}

\begin{document}
\email{foobar@gmail.com}
\email{foo_bar@gmail.com}
\end{document}

enter image description here

  • What it the difference between this and Will's approach? – Svend Tveskæg Aug 16 '13 at 10:38
  • 5
    @SvendTveskæg This will work also if the address has no @. It also improves on Joseph's as it doesn't force usage of the T1 encoding. – egreg Aug 16 '13 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.