Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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, however, that it doesn't work if there is an underscore in the address; how do I add this feature?

share|improve this question

3 Answers 3

up vote 17 down vote accepted

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}
share|improve this answer

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

share|improve this answer

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

share|improve this answer
    
What it the difference between this and Will's approach? –  Svend Tveskæg Aug 16 '13 at 10:38
4  
@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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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