我正在尝试编写一个为 scrlttr2 生成信封的程序包。想法是收集收件人地址列表,并在最后写下所有信封。以下是我目前所掌握的内容,归纳为相关部分:
\documentclass[paper=letter,pagesize,UScommercial9]{scrlttr2}
%%%%% Goes in a separate package %%%%%
\makeatletter
\def\@envelopes{}
\AtEndLetter{
\g@addto@macro\@envelopes{\@elt{%
~ % So that the page isn't totally blank in this non-working example
\usekomavar{toname}\\
\usekomavar{toaddress}%
}}
}
\AtEndDocument{
\KOMAoptions{paper=4.125in:9.5in,paper=landscape}
\recalctypearea
\pagestyle{empty}
\def\@elt#1{ % Some stuff
#1
\clearpage
}
\@envelopes
}
\makeatother
%%%%% End of package %%%%%
\begin{document}
\setkomavar{fromname}{Dominick Pastore}
\setkomavar{fromaddress}{%
123 Main St\\
Somewhere, ST 12345%
}
\begin{letter}{%
John Doe\\
456 High St\\
Someplace, ST 67890%
}
\opening{Dear Mr. Doe:}
I look forward to hearing from you.
\closing{Sincerely,}
\end{letter}
\end{document}
问题是和\usekomavar{toname}
不会\usekomavar{toaddress}
立即展开,并且toname
和toaddress
在展开时(在钩子中AtEndDocument
)是空的。这个问题有一个非常类似的问题:扩展由 \g@addto@macro 添加的代码参数
我尝试使用那里建议的方法:
%...
\AtEndLetter{
\edef\next{%
\noexpand\g@addto@macro\noexpand\@envelopes{\noexpand\@elt{%
\usekomavar{toname}\\
\usekomavar{toaddress}%
}}%
}
\next
}
%...
但后来我收到一条非常意外的错误信息pdflatex
:
! Argument of \reserved@a has an extra }.
<inserted text>
\par
l.48 \end{letter}
(实际上,这与我的原始文件不同,原始文件给出的是,! Incomplete \iffalse; all text was ignored after line 59.
)。
我做错了什么吗?有办法解决吗?
答案1
我在手册中没有看到任何以可扩展的方式使用变量的方法,而且您需要的是值,而不是容器。
\documentclass[paper=letter,pagesize,UScommercial9]{scrlttr2}
%%%%% Goes in a separate package %%%%%
\makeatletter
\def\@envelopes{}
\AtEndLetter{%
\begingroup\edef\x{\endgroup
\noexpand\g@addto@macro\noexpand\@envelopes{%
\noexpand\@elt{%
\noindent % So that the page isn't totally blank
\expandafter\unexpanded\expandafter{\scr@toname@var}\noexpand\\
\expandafter\unexpanded\expandafter{\scr@toaddress@var}%
}%
}%
}\x
}
\AtEndDocument{%
\KOMAoptions{paper=4.125in:9.5in,paper=landscape}
\recalctypearea
\pagestyle{empty}
\def\@elt#1{% Some stuff
#1\clearpage
}%
\@envelopes
}
\makeatother
%%%%% End of package %%%%%
\begin{document}
\setkomavar{fromname}{Dominick Pastore}
\setkomavar{fromaddress}{%
123 Main St\\
Somewhere, ST 12345%
}
\begin{letter}{%
John Doe\\
456 High St\\
Someplace, ST 67890%
}
\opening{Dear Mr. Doe:}
I look forward to hearing from you.
\closing{Sincerely,}
\end{letter}
\end{document}
该值存储在 中\scr@<varname>@var
。
一个expl3
版本:
\documentclass[paper=letter,pagesize,UScommercial9]{scrlttr2}
\usepackage{xparse}
\ExplSyntaxOn
\seq_new:N \g__pastore_envelopes_seq
\AtEndLetter
{
\seq_gput_right:Nx \g__pastore_envelopes_seq
{
\noindent
\exp_not:v { scr@toname@var }
\exp_not:N \\
\exp_not:v { scr@toaddress@var }
}
}
\NewDocumentCommand{\printenvelopes}{}
{
\seq_use:Nn \g__pastore_envelopes_seq { \clearpage }
}
\ExplSyntaxOff
\AtEndDocument{%
\KOMAoptions{paper=4.125in:9.5in,paper=landscape}
\recalctypearea
\pagestyle{empty}
\printenvelopes
}
%%%%% End of package %%%%%
\begin{document}
\setkomavar{fromname}{Dominick Pastore}
\setkomavar{fromaddress}{%
123 Main St\\
Somewhere, ST 12345%
}
\begin{letter}{%
John Doé\\
456 High St\\
Someplace, ST 67890%
}
\opening{Dear Mr. Doe:}
I look forward to hearing from you.
\closing{Sincerely,}
\end{letter}
\end{document}
我用Doé
它来检查重音符号是否没有不合时宜地扩大。