![排版电子邮件地址](https://linux22.com/image/273758/%E6%8E%92%E7%89%88%E7%94%B5%E5%AD%90%E9%82%AE%E4%BB%B6%E5%9C%B0%E5%9D%80.png)
我已经发现这个答案Will Robertson 就如何排版电子邮件地址发表了以下文章:
\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{[email protected]}
\end{document}
问题是如果地址中有下划线,它就不起作用。我该如何添加这样的功能?
答案1
最简单的方法是对文本进行去标记化:
\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{[email protected]}
\end{document}
答案2
您可以使用url
为此包。
\documentclass{article}
\usepackage[colorlinks]{hyperref}
\usepackage[T1]{fontenc}
\DeclareUrlCommand\email{\urlstyle{rm}}
\begin{document}
\email{[email protected]}
\end{document}
答案3
一个expl3
执行:
\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{[email protected]}
\email{[email protected]}
\end{document}