3

I'd like to define an email macro that does the following:

Input: \domainemail{Jack Johnson}

Output: Jack Johnson

The output should be a highlighted hyperlink that reads "mailto:[email protected]".

What I have is

\usepackage[colorlinks=true,allcolors=DarkBlue]{hyperref}
\newcommand{\domainemail}[1]{\href{mailto:#[email protected]}{#1}}

which produces the link "mailto:Jack [email protected]"

How can I replace the space with a period and upper- with lowercase initials? I tried enclosing #1 with \lowercase but that seems to break the hyperlink.

Bonus

Extra points if \domainemail can handle umlauts (including ß) and multiple first and last names, e.g.

Input: \domainemail{Johnson-Löffler, Jack Björn}

Output: Johnson-Löffler, Jack Björn with hyperlink "mailto:[email protected]"

2 Answers 2

2

Some parts of the code are based on another answer of mine. With \addtranslations you set up the “translation” you wish for the Unicode characters you need. Anyway, for very strange names, you can use the optional argument to \domainemail.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xparse}
\usepackage{hyperref}

\ExplSyntaxOn

\NewDocumentCommand{\addtranslations}{m}
 {
  \clist_map_inline:nn { #1 }
   {
    \seq_gput_right:Nn \g_casimir_search_replace_seq { ##1 }
   }
 }

\NewDocumentCommand{\domainemail}{om}
 {
  \IfNoValueTF { #1 }
   {
    \casimir_domainemail:n { #2 }
   }
   {
    \casimir_href:nn { #1 } { #2 }
   }
 }

\seq_new:N \g_casimir_search_replace_seq
\tl_new:N \l_casimir_input_tl
\cs_generate_variant:Nn \tl_lower_case:n { V }

\cs_new_protected:Nn \casimir_href:nn { \href{mailto:#[email protected]}{#2} }
\cs_generate_variant:Nn \casimir_href:nn { V }

\cs_new_protected:Nn \casimir_domainemail:n
 {
  \seq_set_from_clist:Nn \l__casimir_domainemail_temp_seq { #1 }
  \int_compare:nTF { \seq_count:N \l__casimir_domainemail_temp_seq > 1 }
   {
    \tl_set:Nx \l_casimir_input_tl
     {
      \seq_item:Nn \l__casimir_domainemail_temp_seq { 2 }
      .
      \seq_item:Nn \l__casimir_domainemail_temp_seq { 1 }
     }
    \tl_remove_all:Nn \l_casimir_input_tl { ~ }
   }
   {
    \seq_set_split:Nnn \l__casimir_domainemail_temp_seq { ~ } { #1 }
    \tl_set:Nx \l_casimir_input_tl
     {
      \seq_use:Nn \l__casimir_domainemail_temp_seq { . }
     }
   }
  \tl_set:Nx \l_casimir_input_tl { \tl_lower_case:V \l_casimir_input_tl }
  \seq_map_inline:Nn \g_casimir_search_replace_seq
   {
    \tl_replace_all:Nnn \l_casimir_input_tl ##1
   }
  \casimir_href:Vn \l_casimir_input_tl { #1 }
 }
\ExplSyntaxOff

% set up the translations

\addtranslations{
  {æ}{ae},
  {ä}{ae},
  {ö}{oe},
  {ü}{ue},
  {ß}{ss},
  {ñ}{n},
  {é}{e},
}



\begin{document}

\domainemail{Jack Johnson}

\domainemail{Johnson-Löffler, Jack Björn}

\domainemail{Äöß, Reñé Joseph}

\domainemail[strange]{Some strange name}

\end{document}

enter image description here

4

Adding the period is simple with \add@period#1 #2{#1.#2} using space as a delimiter of the first and the second name, typing it with a . then.

Transferring the names into lower case is more difficult, in my point of view, but I used \text_lowercase:n from expl3 which provides this facility.

\documentclass{article}
\usepackage[svgnames,dvipsnames,x11names]{xcolor}
\usepackage[colorlinks=true,allcolors=DarkBlue]{hyperref}
\usepackage{expl3}

\makeatletter
\def\add@period#1 #2{#1.#2}%
\ExplSyntaxOn
\newcommand{\addperiod}[1]{%
  \text_lowercase:n{\add@period#1}% Expandable lower case
}
\ExplSyntaxOff
\makeatother


\newcommand{\domainemail}[1]{%
  \href{mailto:\addperiod{#1}@domain.org}{#1}%
}


\begin{document}
\domainemail{Jack Johnson}
\end{document}
5
  • A screen shot is difficult here....
    – user31729
    Sep 28, 2017 at 7:54
  • 1
    @Rmano: No, you are right: I forgot to remove it from a temporary solution (or a trial of a solution ;-)). Thanks!
    – user31729
    Sep 28, 2017 at 7:57
  • Great answer. Might I alert you to my bonus question in case you're interested in tackling that as well?
    – Janosh
    Sep 28, 2017 at 9:48
  • @Casimir: No, because you changed the question which is not how TeX.SE works
    – user31729
    Sep 28, 2017 at 9:49
  • Sorry, didn't think of it before.
    – Janosh
    Sep 28, 2017 at 10:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .