我准备使用 LaTeX 来写信、发票和其他东西,并且我几乎已经完成了使用 KOMA 类设置模板的工作scrlttr2
。还有一件事...
我有一个地址文件,我将我的联系人/客户放在其中,如下所示:
\adrentry{name}{surname}{address}{phone}{F1}{F2}{comment}{LABELNAME}
然后我在主文件中使用它.tex
:
...
\input{contacts.adr}
\begin{letter}{\LABELNAME}
....
这个方法效果很好,但我想使用一种特殊的格式。根据 F1,我输入联系人的性别,我想在姓名前加上“先生”或“女士”,如果没有,则加上“公司”。我不知道该怎么做。我读了很多关于如何生成批量信件的例子,但无法将其转换为我仅格式化的情况一地址,而不是全部。
但也许这可以为别人指明正确的方向,以便他可以帮助我?
\renewcommand*{\adrentry}[8]{
\begin{letter}{#2 #1\\#3}
\if #5m \opening{Dear Mr. #1,} \fi
\if #5w \opening{Dear Mrs. #1,} \fi
Some text in which the arguments can be used as well.
\closing{Bye,}
\end{letter}
}
所以我想要实现的是可能看起来像这样(我知道它不起作用):
\renewcommand*{\adrentry}[8]{
\if #5m Mr. #2 #1\\ \fi
\if #5w Mrs. #2 #1\\ \fi
\if #5c #2\\#6\\ \fi % It's a company, #6 states the contact person
#3
}
有什么想法、链接等吗?
哦天哪,我希望你能关注我,提前谢谢您!
编辑:
它遵循一个最小的工作示例,展示了我所拥有的状态,不包括上面描述的功能(因为我不知道如何做到这一点;)
包含个人和公司地址文件(contacts.adr):
\adrentry{Cool Company Inc.}{}{Companystreet 21\\12345 Elsewhere}{}{c}{Mr. Tim Tailor}{}{CCI}
\adrentry{Meier}{Michael}{Meierstreet 34\\45636 Nowhere}{}{m}{}{}{MEIER}
使用scrlttr2的tex文件:
\documentclass[
paper=a4,
version=last,
enlargefirstpage,
fontsize=11pt]{scrlttr2}
\usepackage[english,ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
% global koma vars
\setkomavar{fromname}{Max Myname}
\setkomavar{fromphone}{+49\,123\,45678910}
\setkomavar{fromemail}{[email protected]}
\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}
\setkomavar{yourmail}{1.1.2013}
\setkomavar{yourref}{76/54/321}
\setkomavar{myref}{123/456/789}
\input{contacts.adr}
\begin{letter}{\MEIER}
\opening{Dear Mr. Meier}
\lipsum[7]
\closing{Sincerely yours}
\end{letter}
\end{document}
以上产生
Michael Meier
Meierstreet 34
45636 Nowhere
和
Cool Company Inc.
Companystreet 21
12345 Elsewhere
但我希望它能产生
Mr.
Michael Meier
Meierstreet 34
45636 Nowhere
和
Cool Company Inc.
Mr. Tim Tailor
Companystreet 21
12345 Elsewhere
答案1
好问题;我通过改变\addrentry
和添加一些无害的东西来解决这个问题\begin{letter}
,如果后续\makeopening
解决这个问题,如果没有
在通常的\addentry
命令中,最后一个参数用于构建存储地址行的控制序列。我还添加了开头行的构建,该开头行MEIER
存储在 中\opening@MEIER
。此命令由 调用\makeopening
(使用之前添加的钩子)。
\documentclass[
paper=a4,
version=last,
enlargefirstpage,
fontsize=11pt]{scrlttr2}
\usepackage[english,ngerman]{babel}
\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
\newif\if@company
\renewcommand\addrentry[9]{%
\def\@tempa{#1}%
\ifx\@tempa\@empty
\else
\@companyfalse
\if#5m%
\@namedef{opening@#9}{\opening{Dear Mr.\ #1,}}%
\else
\if#5w%
\@namedef{opening@#9}{\opening{Dear Mrs.\ #1,}}%
\else
\@namedef{opening@#9}{\opening{Sirs,}}%
\@companytrue
\fi
\fi
\def\@tempa{#2}%
\ifx\@tempa\@empty
\if@company
\@namedef{#9}{#1\\#6\\#3}%
\else
\@namedef{#9}{#1\\#3}%
\fi
\else
\if@company
\@namedef{#9}{#2\\#6\\#3}%
\else
\@namedef{#9}{#2 #1\\#3}%
\fi
\fi
\fi
}
\makeatother
% global koma vars
\setkomavar{fromname}{Max Myname}
\setkomavar{fromphone}{+49\,123\,45678910}
\setkomavar{fromemail}{[email protected]}
\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}
\setkomavar{yourmail}{1.1.2013}
\setkomavar{yourref}{76/54/321}
\setkomavar{myref}{123/456/789}
\input{meier.adr}
\begin{letter}{\MEIER}
\makeopening
\lipsum[7]
\closing{Sincerely yours}
\end{letter}
\begin{letter}{\CCI}
\makeopening
\lipsum[7]
\closing{Sincerely yours}
\end{letter}
\end{document}
以下是两封信(仅相关部分)