153

When trying to write an e-mail address, there are always problems with the @ sign.

Solutions I've seen suggested are using some verbatim environment (more difficult in footnotes) and using a math-mode @.

What is the correct way to do this?

Caramdir
  • 89,023
  • 26
  • 255
  • 291
Yuval Filmus
  • 1,805
  • 2
  • 13
  • 10

10 Answers10

169

Another thing one can to with the hyperref package is to use the href command

\href{mailto:me@example.com}{me@example.com}

which has the advantage that when clicked-on in an electronic document, it will (ideally) call up the mailer and cue up the e-mail address.

vanden
  • 30,891
  • 23
  • 67
  • 87
Willie Wong
  • 24,733
  • 8
  • 74
  • 106
  • 38
    I often define an \email command that expands to your \href, automatically prepending the mailto: part. – Damien Pollet Jul 27 '10 at 07:53
  • 7
    Good idea. One small caveat is that \email is already defined in certain document classes (for example amsart). – Willie Wong Jul 27 '10 at 13:45
  • how do I do it in TexWorks ? it does not have hyperef, right ? How do I install hyperef in TexWorks ? – user25957 Nov 29 '13 at 20:47
  • @user25957: I am not familiar with TexWorks, do this or other questions in the [tag:texworks] tag help? If not try asking a new question about it. – Willie Wong Dec 02 '13 at 09:15
  • 6
    @user25957 TeXWorks is an editor. It doesn't matter if you use TeXWorks, TeXShop, TeXMaker, TeXNicCenter, TeXLipse, Notepad, Emacs, Gedit, or whatever. It will always invoke the TeX distribution on your computer (TeX Live or possibly MiKTeX on Windows) which will most probably include hyperref. – marczellm Feb 04 '14 at 13:35
  • 1
    Thank you for sharing this, but I have always been wondering if there is a way to create something like "click and copy to clipboard". Because clicking on a \href link always redirects me to an email client, which I do not use! So it makes more sense for me if I can just click on the email address and it is copied to my clipboard! Is it even possible in LaTeX? Thanks! – zyy Oct 06 '19 at 01:01
  • 5
    For the lazy, @DamienPollet meant: use \newcommand{\email}[1]{\href{mailto:#1}{#1}} somewhere in your preamble, that'll do just fine. – Clément Mar 12 '21 at 17:40
  • sorry, and thanks ;-) – Damien Pollet Mar 15 '21 at 21:18
69

You can also combine the hyperref and url approaches to get a working link that is formatted like an URL:

\href{mailto:me@example.com}{\nolinkurl{me@example.com}}
Werner
  • 603,163
Caramdir
  • 89,023
  • 26
  • 255
  • 291
27

Just to add to Willie's good answer, in terms of the formatting (as the OP seems to be getting at) there isn't a "correct" way. Use \textsf or \texttt or whatever looks good for your particular document and use case and font choice. Also try the randtext package to attempt to obfuscate the email address inside the PDF to make it less susceptible to spammers (but note that — I think — it doesn't play nice with hyperref's \href).

If you want to "special-case" the @ sign without adding markup to your text, you could write something like this:

\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}

where you get the proper hyperlinking of the email address and you can customise the look of the email by changing \emailfont and the look of the ampersat (or arrobase, or @) by changing \emailampersat.

Werner
  • 603,163
  • 1
    Yeah.. mentioning randtext was exactly what I needed. Just curious... does your command still produce a working hyperlink if I wrap a randtext{} around it? I suppose not... – polynomial_donut Jun 04 '17 at 09:48
18

You can define \at command in the preamble:

\newcommand{\at}{\makeatletter @\makeatother}

and then use it as follows within the document:

myemail\at gmail.com
  • 7
    The catcode of @ in \at is the catcode that was set for @ at the time of the definition. If the catcode of @ should be "letter", then it is defined via \makeatletter\newcommand*{\at}{@}\makeatother. However, the catcode "letter" or "other" does not matter here, thus \newcommand*{\at}{@} would be enough or just using @. – Heiko Oberdiek Feb 04 '14 at 15:26
  • I can read this comment above twenty times and still be confused. Sounds like Alan Greenspan quote: I know that you think you know what I said. But I'm not sure whether you understood that what you heard is what I meant. – DrBeco Feb 05 '23 at 03:53
  • haha it's usual feeling when trying to read Tex manuals ;-) – Emiliano Poggi Feb 09 '23 at 18:59
17

Very simple solution from here:

\texttt{john@gmail.com}

0 _
  • 958
11

If you don't like how the at-symbol of the default font looks (I don't), you may borrow it from the Times font, like so: youraddres{\fontfamily{ptm}\selectfont @}example.com

4

For the @ character I prefer the \MVAt command from the marvosym package, since the cmss @ looks so different from other fonts I'm used to. For the full address I use the hyperref formatting suggested by Willie.

Marco Daniel
  • 95,681
craq
  • 371
3

I don't think that there is a unique correct way to handle this. As another answer says, you can use hyperref. But, the url package can also be used to render email addresses:

\documentclass{article}

\usepackage{url}

\begin{document}

\url{me@example.com}

\end{document}
vanden
  • 30,891
  • 23
  • 67
  • 87
  • 1
    You don't want to use \url because it will attempt to create a hyperlink to an actual URL with the email address you supply. See Caramdir's answer for a better solution. – Will Robertson Aug 18 '10 at 10:11
  • 1
    @WillRobertson: You sure about that? Note that this was \url from the url package, not from hyperref. – vanden Aug 18 '10 at 15:10
  • 4
    Oops; I meant only if you load hyperref as well -- it will transparently replace \url with the hyperlinked one. The point is that \url is not semantically designed for email addresses. – Will Robertson Aug 19 '10 at 03:32
  • Logical markup can be defined with package url via \DeclareUrlCommand\email{} and used as \email{me@example.com}. – Heiko Oberdiek Feb 04 '14 at 15:29
  • Would this logical markup works if both the url and hyperref packages are loaded, @HeikoOberdiek ? – DrBeco Feb 05 '23 at 03:43
  • @DrBeco The url package is loaded by hyperref and the \DeclareUrlCommand works. But the result is not a link. – Heiko Oberdiek Mar 16 '23 at 21:02
3

I defined a command, to avoid the redundancy of Willie Wong's and Caramdir's answers, and to allow protocols other than mailto:

\usepackage{hyperref}
\usepackage{xstring}

\newcommand{\thelink}{\@empty}
\newcommand{\link}[2]{%
  \IfSubStr{#1}{:}{\renewcommand\thelink{#1}}{\renewcommand\thelink{#1:#2}}%
  \href{\thelink}{\texttt{#2}}%
}

There's a long form, where the URL is different than the text, and the short form, where the protocol is catenated with the text to make up the URL:

Phone: \link{tel:1234567890}{+1 234-567-890} \\
E-mail: \link{mailto}{foo@bar.spam}

Produces this result:

latex rendered output

I use \texttt instead of \nolinkurl, because the latter removed spaces from the supplied text.

arekolek
  • 575
1

There are a number of things that you can try:

(with the hyperref package):

\url{email address}

or more simply (for monospace font):

\texttt{email address}

Moor Xu
  • 891
  • 2
  • 9
  • 10
  • 6
    You don't want to use \url because it will attempt to create a hyperlink to an actual URL with the email address you supply. See Caramdir's answer for a better solution. – Will Robertson Aug 18 '10 at 10:10