通过 \input 从文件检索的多行文本,在 newcommand 声明中,将新声明的命令作为标准字母类的参数传递

通过 \input 从文件检索的多行文本,在 newcommand 声明中,将新声明的命令作为标准字母类的参数传递

我已经整理了一个由多个文件组成的最小工作示例

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.stymyletter.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

我希望它有用。

相关内容