下面的代码可以很好地工作:
信封.tex
\documentclass[12pt]{letter}
\usepackage[businessenvelope,customenvelopes]{envlab}
\SetEnvelope[2.0in]{240mm}{145mm}
\setlength{\EnvelopeTopMargin}{0.75in} % 0.5
\setlength{\EnvelopeLeftMargin}{1.25in} % 2.0
\begin{document}
\startlabels
\mlabel{%
1234 Center Lane \\
Long Mile Village \\
92319%
}%
{%
239 Mill Lake Rd \\
Centreville \\
78392%
}%
\end{document}
不过,我想使用 catchfile 包来从外部文件中检索地址。实现可能看起来像这样。
地址.tex
1234 Center Lane \\%
Long Mile Village \\%
92319%
地址b.tex
239 Mill Lake Rd \\%
Centreville \\%
78392%
letterb.tex
\documentclass[12pt]{letter}
\usepackage[businessenvelope,customenvelopes]{envlab}
\usepackage{catchfile}
\newcommand{\getfromaddress}[1]{\CatchFileDef{\thefromaddress}{#1}{}}
\newcommand{\gettoaddress}[1]{\CatchFileDef{\thetoaddress}{#1}{}}
\providecommand{\expandonce}{\unexpanded\expandafter}
\getfromaddress{addressa.tex}
\gettoaddress{addressb.tex}
\SetEnvelope[2.0in]{240mm}{145mm}
\setlength{\EnvelopeTopMargin}{0.75in} % 0.5
\setlength{\EnvelopeLeftMargin}{1.25in} % 2.0
\begin{document}
\startlabels
\mlabel{%
\expandonce{\thefromaddress}%
}%
{%
\expandonce{\thetoaddress}%
}%
\end{document}
当我编译此代码时,我收到一条错误消息,抱怨花括号不平衡。乍一看,事情似乎是平衡的。我不太熟悉和的用法。\expandafter
我\expandonce
想有人可能能够很容易地做到这一点。任何评论/建议都非常感谢。
我已经合并了代码这个帖子与 egreg 建议的代码一起生成一个字母的 .cls 文件,可以通过指定以下信息来调用该文件:
- 发件人地址所在的文件
- 要寻址的文件
- 开信/致辞
- 信件结束
- 信后记
- 信件签名
- 信封尺寸
我已包含以下代码:
字母表
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{theletterb}[]
\LoadClass[]{letter}
\RequirePackage{lipsum}
\RequirePackage{catchfile}
\RequirePackage[businessenvelope,customenvelopes]{envlab}
\RequirePackage{xparse}
\RequirePackage[textwidth=345.0pt,%
top=2in,
headheight=1.0in,
headsep=0.20in]{geometry}
\newcommand{\getfromaddress}[1]{\CatchFileDef{\thefromaddress}{#1}{}}
\newcommand{\gettoaddress}[1]{\CatchFileDef{\thetoaddress}{#1}{}}
\providecommand{\expandonce}{\unexpanded\expandafter}
\ExplSyntaxOn
\NewDocumentCommand{\MLABEL}{ }
{
\johnchris_mlabel:oo { \thefromaddress } { \thetoaddress }
}
\cs_set_eq:NN \johnchris_mlabel:nn \mlabel
\cs_generate_variant:Nn \johnchris_mlabel:nn { oo }
\ExplSyntaxOff
\AtBeginDocument{%
\pagestyle{empty}
\let\ps@firstpage\ps@plain
\begingroup\edef\x{\endgroup
\noexpand\begin{letter}{\expandonce{\thetoaddress}}
{\expandonce{\thefromaddress}}}\x
\opening{\theopening}
}%
\AtEndDocument{%
\closing{\theclosing}
\ps{\thepostscript}
\newpage
\startlabels
\MLABEL
\end{letter}
}
信件.tex
\documentclass{theletter}
\getfromaddress{addressa.tex}
\gettoaddress{addressb.tex}
\newcommand{\theopening}{Dear Recipient}
\newcommand{\theclosing}{Sincerely,}
\newcommand{\thepostscript}{}
\signature{Mae L. Mann}
\SetEnvelope[2.0in]{240mm}{145mm}
\setlength{\EnvelopeTopMargin}{0.75in}
\setlength{\EnvelopeLeftMargin}{1.25in}
\begin{document}
\input{body.tex}
\end{document}
再次强调,支持文件如下:
地址.tex
1234 Center Lane \\%
Long Mile Village \\%
92319%
地址b.tex
239 Mill Lake Rd \\%
Centreville \\%
78392%
正文.tex
lipsum[1]
答案1
这个问题与另一个问题类似:\mlabel
需要查看明确的分隔符\\
才能正常工作。因此,您需要展开\thefromaddress
并在其开始工作\thetoaddress
之前进行。\mlabel
有一些技术可以解决这个问题;但是,由于您只使用一对发件人和收件人地址,所以您只需要一个无参数命令。
您可以选择
\newcommand{\MLABEL}{%
\expandafter\expandafter\expandafter\mlabel
\expandafter\expandafter\expandafter
{\expandafter\thefromaddress\expandafter}\expandafter{\thetoaddress}
}
或者
\newcommand{\MLABEL}{%
\begingroup\edef\x{\endgroup
\noexpand\mlabel{\expandonce{\thefromaddress}}
{\expandonce{\thetoaddress}}%
}\x
}
甚至
\newcommand{\MLABEL}{%
\expandafter\mlabel\expandafter
{\csname thefromaddress\expandafter\endcsname\expandafter}%
\expandafter{\thetoaddress}%
}
三个定义最终都会产生相同的效果。选择最有吸引力的一个。;-)
\documentclass[12pt]{letter}
\usepackage[businessenvelope,customenvelopes]{envlab}
\usepackage{catchfile}
\newcommand{\getfromaddress}[1]{\CatchFileDef{\thefromaddress}{#1}{}}
\newcommand{\gettoaddress}[1]{\CatchFileDef{\thetoaddress}{#1}{}}
\providecommand{\expandonce}{\unexpanded\expandafter}
% Definition 1
%\newcommand{\MLABEL}{%
% \expandafter\expandafter\expandafter\mlabel
% \expandafter\expandafter\expandafter
% {\expandafter\thefromaddress\expandafter}\expandafter{\thetoaddress}
%}
% Definition 2
%\newcommand{\MLABEL}{%
% \begingroup\edef\x{\endgroup
% \noexpand\mlabel{\expandonce{\thefromaddress}}
% {\expandonce{\thetoaddress}}%
% }\x
%}
% Definition 3
\newcommand{\MLABEL}{%
\expandafter\mlabel\expandafter
{\csname thefromaddress\expandafter\endcsname\expandafter}%
\expandafter{\thetoaddress}%
}
\getfromaddress{addressa.tex}
\gettoaddress{addressb.tex}
\SetEnvelope[2.0in]{240mm}{145mm}
\setlength{\EnvelopeTopMargin}{0.75in} % 0.5
\setlength{\EnvelopeLeftMargin}{1.25in} % 2.0
\begin{document}
\startlabels
\MLABEL
\end{document}
更简洁的版本使用expl3
LaTeX3 的编程层。您可能会喜欢这种简洁性:我将 的含义复制\mlabel
到具有两个参数的内部函数中,然后生成一个变体,该变体在采取正常操作之前扩展参数的内容。
\documentclass[12pt]{letter}
\usepackage[businessenvelope,customenvelopes]{envlab}
\usepackage{catchfile}
\usepackage{xparse}
\newcommand{\getfromaddress}[1]{\CatchFileDef{\thefromaddress}{#1}{}}
\newcommand{\gettoaddress}[1]{\CatchFileDef{\thetoaddress}{#1}{}}
\ExplSyntaxOn
\NewDocumentCommand{\MLABEL}{ }
{
\johnchris_mlabel:oo { \thefromaddress } { \thetoaddress }
}
\cs_set_eq:NN \johnchris_mlabel:nn \mlabel
\cs_generate_variant:Nn \johnchris_mlabel:nn { oo }
\ExplSyntaxOff
\getfromaddress{addressa.tex}
\gettoaddress{addressb.tex}
\SetEnvelope[2.0in]{240mm}{145mm}
\setlength{\EnvelopeTopMargin}{0.75in} % 0.5
\setlength{\EnvelopeLeftMargin}{1.25in} % 2.0
\begin{document}
\startlabels
\MLABEL
\end{document}