![通过 \input 从文件检索的多行文本,在 newcommand 声明中,将新声明的命令作为标准字母类的参数传递](https://linux22.com/image/268803/%E9%80%9A%E8%BF%87%20%5Cinput%20%E4%BB%8E%E6%96%87%E4%BB%B6%E6%A3%80%E7%B4%A2%E7%9A%84%E5%A4%9A%E8%A1%8C%E6%96%87%E6%9C%AC%EF%BC%8C%E5%9C%A8%20newcommand%20%E5%A3%B0%E6%98%8E%E4%B8%AD%EF%BC%8C%E5%B0%86%E6%96%B0%E5%A3%B0%E6%98%8E%E7%9A%84%E5%91%BD%E4%BB%A4%E4%BD%9C%E4%B8%BA%E6%A0%87%E5%87%86%E5%AD%97%E6%AF%8D%E7%B1%BB%E7%9A%84%E5%8F%82%E6%95%B0%E4%BC%A0%E9%80%92.png)
我已经整理了一个由多个文件组成的最小工作示例
mwe.tex % instance of personal letter
address.tex % full address of individual being addressed in mwe.tex
C:\LocalTeX\tex\latex\my\mygeneralmatter.sty % author's address and signature defined here
C:\LocalTeX\tex\latex\my\myletter.cls % author's address and signature defined here
以下是文件内容:
麦格
\NeedsTeXFormat{LaTeX2e}
\documentclass{myletter}
\newcommand{\thetoaddress}{\input{address}} % doesn't compile
%\newcommand{\thetoaddress}{%
%Addressee name\\
%Addressee title\\
%Address line 1\\
%Address line 2\\
%Address line 3%
%} % does compile
\newcommand{\theopening}{Dear addressee,}
\newcommand{\theclosing}{Sincerely,}
\newcommand{\thepostscript}{}
\begin{document}
I would like to thank you for your recommendation during the hiring process for the position of position with company.
I have thoroughly enjoyed being a part of company. It has indeed been a privelege to have worked with such a competant team.
I very much appreciate the support.
\end{document}
地址.tex
Person\\
Title\\
Address line 1\\
Address line 2\\
Address line 3
我的一般事务
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mygeneralmatter}[2013/05/21 a package containing general matter]
\newcommand{\email}{[email protected]} % appropriate email address
\newcommand{\phone}{(999) 999 9999} % phone number
% company name and address
\newcommand{\authoraddress}{%
Address line 1 \\%
Address line 2 \\%
Address line 3 %
}%
% company name, address and phone numbers
\newcommand{\authoraddresslong}{%
\authoraddress \\%
\phone \\%
\email
}%
% signature for formal letters
\newcommand{\authorsignature}{First Last}
我的信件
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{myletter}[2013/04/22 a class for a personal letter which essentially preloads the authors current personal information]
\LoadClass[]{letter}
\RequirePackage{mygeneralmatter} % provides the commands: \authorsignature, \authoraddresslong
\signature{\authorsignature} % signature of the letter defined in jclgenmatter.sty
\address{\authoraddresslong} % author's long address defined in jclgenmatter.sty
\AtBeginDocument{%
\begin{letter}{\thetoaddress}
\opening{\theopening}
}%
\AtEndDocument{%
\closing{\theclosing}
\ps{\thepostscript}
\end{letter}
}
请注意,该文件address.tex
位于旁边mwe.tex
(相同路径)。另请注意,我已更新本地 TeX 系统以了解这些文件mygeneralmatter.sty
,myletter.cls
方法是执行 MikTeX --> 维护 --> 设置 --> 刷新 FNDB
这是我在编译时收到的错误mwe.tex
! Argument of \@no@pgbk has an extra }.
<inserted text>
\par
l.22 \end{document}
具体来说,问题可以概括为,新命令\thetoaddress
被定义为mwe.tex
多行文本,该文本address.tex
通过\input{}
命令从文件中检索。然后,新声明的命令\thetwoaddress
作为参数传递给文件中的标准字母环境myletter.cls
更一般地,该问题可以概括为通过从文件中检索多行文本\input
,在新命令声明中,然后将新声明的命令作为标准字母类的参数传递
有人能建议一种方法,让我能够从个性化信件的实例中引用存储在文件中的“收件人地址”吗?
答案1
我建议您将以下几行添加到您的类文件中:
\RequirePackage{catchfile}
\newcommand{\getaddressfrom}[1]{\CatchFileDef{\thetoaddress}{#1}{}}
\newcommand{\defineaddress}[1]{\def\thetoaddress{#1}}
并改变你开始字母的部分
\AtBeginDocument{%
\begingroup\def\tempa{\endgroup\begin{letter}}
\expandafter\tempa\expandafter{\thetoaddress}%
\opening{\theopening}
}
这样,\begin{letter}
命令将以 的展开形式呈现\thetoaddress
。这很重要,因为该类letter
特别考虑了此参数中第一个 之前的部分\\
。
为了从文件中读取地址,比如说
\documentclass{myletter}
\getaddressfrom{address}
\newcommand{\theopening}{Dear addressee,}
\newcommand{\theclosing}{Sincerely,}
\newcommand{\thepostscript}{}
\begin{document}
I would like to thank you for your recommendation during the hiring
process for the position of position with company.
I have thoroughly enjoyed being a part of company. It has indeed
been a privelege to have worked with such a competant team.
I very much appreciate the support.
\end{document}
或者,\getaddressfrom
您也可以直接使用以下命令指定地址
\defineaddress{John Doe\\
42, Some Street\\
12345 Somecity}
答案2
\newcommand{\thetoaddress}{\protect\input{address}} % does compile
答案3
对于同样的问题,我决定增加变量,并将 8 增加到 9,将 6.5 增加到 7。
\textheight=9in % text height can be bigger for a longer letter
\textwidth=7in % text width of `6.5in` leaves 1 inch for right margin
我希望它有用。