我正在尝试使用 .adr 文件来存储信件的地址。
我在这里找到了非常有力的答案: 使用 adr 文件,如何改变地址的格式?
它效果很好,我甚至能够使它适应我的用例:
\documentclass{scrlttr2}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\usepackage{xpatch}
%%% Patch the relevant scrlttr macros
\makeatletter
% at the end of \begin{letter}{<name>} store the <name>
\xapptocmd\letter{\gdef\this@letter@to{#2}}{}{}
% this seems complicated, but is needed to strip off
% the backslash from the argument to `\begin{letter}`
% Don't use `\makeopening` if the argument to `\begin{letter}`
% is not obtained via the `.adr` file
% we need to get \opening@MEIER from the fact that
% \this@letter@to expands to \MEIER so we first expand
% \this@letter@to, then \string, so that \@gobble will
% remove the backslash
\newcommand\makeopening{%
\@nameuse{%
opening@%
\expandafter\expandafter\expandafter
\@gobble\expandafter\string\this@letter@to}}
% modify \addrentry to define an opening and
% the address lines as desired
\renewcommand\addrentry[9]{%
\def\@tempa{#1}%
\ifx\@tempa\@empty
\else
\@namedef{opening@#9}{\opening{#5}}% % define opening phrase
\@namedef{#9}{#2 #1\\#3}% % re-define the address field (because of renewcommand)
\fi
}
\makeatother
% global koma vars
\setkomavar{fromname}{Max Myname}
\setkomavar{backaddress}{Max Myname, Some Street 12, 012345 Somewhere}
\setkomavar{fromaddress}{Some Street 12\\012345 Somewhere}
\begin{document}
\setkomavar{subject}{The subject}
\setkomavar{date}{\today}
\setkomavar{place}{Dresden}
\input{test_adressen.adr}
\begin{letter}{\MaxMustermann}
\makeopening
\lipsum[7]
\closing{Sincerely yours}
\end{letter}
\begin{letter}{\MariaMustermann}
\makeopening
\lipsum[7]
\closing{Sincerely yours}
\end{letter}
\end{document}
test_addressen.adr 为:
\addrentry{Mustermann}
{Max}
{\bigskip Musterstraße 12\\12345 Musterstadt}
{(1234)~1~23~45~67}
{Guten Tag Herr Mustermann,}
{frei2}
{frei3}
{default selection for address}
{MaxMustermann}
\addrentry{Mustermann}
{Maria}
{\bigskip Musterstraße 12\\12345 Musterstadt}
{(1234)~1~23~45~67}
{Sehr geehrte Frau Mustermann,}
{frei2}
{frei3}
{default selection for address}
{MariaMustermann}
我想在我的设置中使用它,但是由于\usepackage
我无法在 .lco 文件中使用它。有没有可能在没有xparse
和/或使用expl3
它的情况下实现这一点,我现在比对低级 latex2e 命令有了更多的了解,而这些命令\this@letter@to
对我来说仍然是个谜?
答案1
没有 你也可以做到xpatch
。
\documentclass{scrlttr2}
\usepackage{lipsum}
\ExplSyntaxOn
% at the end of \begin{letter}{<name>} store the <name>, which is #2,
% but without the backslash
\NewCommandCopy{\originalletter}\letter
\RenewDocumentCommand{\letter}{O{}m}
{
\originalletter[#1]{#2}
\tl_gset:Nx \g_mrcarnivore_name_tl { \cs_to_str:N #2 }
}
\tl_new:N \g_mrcarnivore_name_tl
% Don't use `\makeopening` if the argument to `\begin{letter}`
% is not obtained via the `.adr` file
\NewDocumentCommand{\makeopening}{}
{
\use:c { opening@ \tl_use:N \g_mrcarnivore_name_tl }
}
% modify \addrentry to define an opening and
% the address lines as desired
\renewcommand\addrentry[9]
{
\tl_if_blank:nF { #1 }
{
\cs_set:cpn {opening@#9}{\opening{#5}} % define opening phrase
\cs_set:cpn {#9}{#2~#1\\#3} % re-define the address field
}
}
\ExplSyntaxOff
% global koma vars
\setkomavar{fromname}{Max Myname}
\setkomavar{backaddress}{Max Myname, Some Street 12, 012345 Somewhere}
\setkomavar{fromaddress}{Some Street 12\\012345 Somewhere}
\begin{document}
\setkomavar{subject}{The subject}
\setkomavar{date}{\today}
\setkomavar{place}{Dresden}
\input{test_adressen.adr}
% first letter
\begin{letter}{\MaxMustermann}
\makeopening
\lipsum[7]
\closing{Sincerely yours}
\end{letter}
% second letter
\begin{letter}{\MariaMustermann}
\makeopening
\lipsum[7]
\closing{Sincerely yours}
\end{letter}
\end{document}