如何分解@符号后面的电子邮件地址(scrlttr2)

如何分解@符号后面的电子邮件地址(scrlttr2)

我喜欢用 latex 写信,到目前为止一切都很好,我只是发现右上角的发件人信息有点“丑陋”。这是因为长电子邮件地址将整个区块推到了左边:

Firstname Lastname
Street 001
9999 Cityname
Telefon: +43 9999 999 2
Email: [email protected]

是否可以在@符号后强制换行,将地址与其余信息对齐,使其看起来如下所示?

Firstname Lastname
Street 001
9999 Cityname
Telefon: +43 9999 999 2
Email: first.lastname@
       alumni.university.edu

以下是 MWE:

\documentclass[version=last]{scrlttr2}
\usepackage[ngerman]{babel}
\usepackage{graphics}
\LoadLetterOption{DIN}
\usepackage{hyperref}
\KOMAoptions{
    paper=a4,
    pagenumber=botcenter,
    fromemail,
    backaddress,
    foldmarks,
    enlargefirstpage,
    foldmarks=true,
    fromrule=afteraddress,
    fromphone,fromemail,
}
\hypersetup{
    colorlinks=false,
    hidelinks=true,
}
\makeatletter\setkomavar{firsthead}{%
    \hspace*{\fill}\footnotesize
    \begin{tabular}{l@{}}
        \usekomavar{fromname}\\
        \usekomavar{fromaddress}
        \if@phone\\
        \usekomavar*{fromphone}\usekomavar{fromphone}\fi
        \if@fax\\
        \usekomavar*{fromfax}\usekomavar{fromfax}\fi
        \if@email\\
        \usekomavar*{fromemail}\usekomavar{fromemail}\fi
        \if@www\\
        \usekomavar*{fromurl}\usekomavar{fromurl}\fi
    \end{tabular}%
}\makeatother
\setkomavar{fromname}{Firstname Lastname}
\setkomavar{fromaddress}{Street 001\\9999 Cityname}
\setkomavar{fromphone}{+43\,9999\,999\,2}
\setkomavar{fromemail}{\href{mailto:[email protected]}{[email protected]}}
\begin{document}
\begin{letter}{%
Company\\
Mainstreet 001\\
1111 Cityname
}
\setkomavar{subject}{Subject}

\opening{Dear Mr. X,}
This is some text.
\closing{Cheers}
\end{letter}
\end{document}

先感谢您 :)

答案1

尝试这个:

\documentclass[version=last]{scrlttr2}
\usepackage[ngerman]{babel}
\usepackage{graphics}
\LoadLetterOption{DIN}
%\LoadLetterOption{Absender}
\usepackage{hyperref}
\KOMAoptions{
    paper=a4,
    pagenumber=botcenter,
    fromemail,
    backaddress,
    foldmarks,
    enlargefirstpage,
    foldmarks=true,
    fromrule=afteraddress,
    fromphone,fromemail,
}
\hypersetup{
    colorlinks=false,
    hidelinks=true,
}
\newsavebox{\mybox}
\newcommand{\myBrokenEmail}[2]{\savebox\mybox{\hbox{E-Mail:\space}}#1\\\hspace*{\wd\mybox}#2}

\setkomavar{fromname}{Firstname Lastname}
\setkomavar{fromaddress}{Street 001\\9999 Cityname}
\setkomavar{fromphone}{+43\,9999\,999\,2}
\setkomavar{fromemail}{\href{mailto:[email protected]}{\myBrokenEmail{first.lastname@}{alumni.university.edu}}}
\begin{document}
\begin{letter}{%
Company\\
Mainstreet 001\\
1111 Cityname
}
\setkomavar{subject}{Subject}

\opening{Dear Mr. X,}
This is some text.
\closing{Cheers}
\end{letter}
\end{document}

输出:

在此处输入图片描述

编辑:

如果确实使用表格,则第二种方法是:

\documentclass[version=last]{scrlttr2}
\usepackage[ngerman]{babel}
\usepackage{graphics}
\LoadLetterOption{DIN}
%\LoadLetterOption{Absender}
\usepackage{hyperref}
\KOMAoptions{
    paper=a4,
    pagenumber=botcenter,
    fromemail,
    backaddress,
    foldmarks,
    enlargefirstpage,
    foldmarks=true,
    fromrule=afteraddress,
    fromphone,fromemail,
}
\hypersetup{
    colorlinks=false,
    hidelinks=true,
}
% Here is the command to be used:
\newcommand{\myBrokenEmail}[2]{\begin{tabular}[t]{@{}l@{}}#1\\#2\end{tabular}\vspace{8pt}}

\setkomavar{fromname}{Firstname Lastname}
\setkomavar{fromaddress}{Street 001\\9999 Cityname}
\setkomavar{fromphone}{+43\,9999\,999\,2}
\setkomavar{fromemail}{\href{mailto:[email protected]}{\myBrokenEmail{first.lastname@}{alumni.university.edu}}}
\begin{document}
\begin{letter}{%
Company\\
Mainstreet 001\\
1111 Cityname
}
\setkomavar{subject}{Subject}

\opening{Dear Mr. X,}
This is some text.
\closing{Cheers}
\end{letter}
\end{document}

答案2

稍微修改一下答案您仍然可以用来\usekomavar{fromemail}不间断地获取电子邮件:

\documentclass[version=last]{scrlttr2}
\usepackage[ngerman]{babel}
\usepackage{graphics}
%\LoadLetterOption{DIN}% default
\usepackage{hyperref}
\KOMAoptions{
    %paper=a4,% default
    pagenumber=botcenter,
    %fromemail,% used twice
    %backaddress,% default
    %foldmarks,% default
    enlargefirstpage,
    %foldmarks=true,% default (and used twice)
    %fromrule=afteraddress,
    fromphone,fromemail
}
\hypersetup{
    %colorlinks=false,% not needed if hidelinks is used
    hidelinks%=true,% no key-value-option
}

\makeatletter\setkomavar{firsthead}{%
    \hspace*{\fill}\footnotesize
    \begin{tabular}{l@{}}
        \usekomavar{fromname}\\
        \usekomavar{fromaddress}
        \if@phone\\
        \usekomavar*{fromphone}\usekomavar{fromphone}\fi
        \if@fax\\
        \usekomavar*{fromfax}\usekomavar{fromfax}\fi
        \if@email\\
        \usekomavar*{fromemail}\usekomavar[\let\EmailFormat\EmailInHeader]{fromemail}\fi
        \if@www\\
        \usekomavar*{fromurl}\usekomavar{fromurl}\fi
    \end{tabular}%
}\makeatother
\setkomavar{fromname}{Firstname Lastname}
\setkomavar{fromaddress}{Street 001\\9999 Cityname}
\setkomavar{fromphone}{+43\,9999\,999\,2}
\setkomavar{fromemail}{\href{mailto:[email protected]}{\EmailFormat{first.lastname@}{alumni.university.edu}}}

\newcommand\EmailFormat[2]{#1#2}
\newcommand\EmailInHeader[2]{\begin{tabular}[t]{@{}l@{}}#1\\#2\end{tabular}}

\begin{document}
\begin{letter}{%
Company\\
Mainstreet 001\\
1111 Cityname
}
\setkomavar{subject}{Subject}

\opening{Dear Mr. X,}
This is some text.

The email in a single line:  \usekomavar{fromemail}
\closing{Cheers}
\end{letter}
\end{document}

结果:

在此处输入图片描述

相关内容