创建一个生成字母的函数?

创建一个生成字母的函数?

我必须写很多封信,每次只需修改姓名、日期和地址,但内容总是相同的。

简短版本可以是这样的“亲爱的 [姓名],您必须于 [时间] [日期] 从 [地址] 到场。”

有没有办法自动完成这个?我只想插入正确的字段,让 Latex 从模板创建信件。

答案1

我喜欢这样做,使用datatool(就像评论中所建议的那样)

\documentclass[10pt,a4paper]{scrlttr2}  
\usepackage{datatool}

\begin{document}        
\setkomavar{date}{\today}
\setkomavar{subject}{Very Important Letter}
\setkomavar{fromname}{Tom}
\setkomavar{fromaddress} {%
    My Company\\
    Company Way 42\\
    1337~Corporatetown%
}   

\DTLloaddb{list}{addresses.csv}
\DTLforeach{list}{\first=Firstname,\last=Lastname,\address=Street,
    \town=Town,\postal=Postal}{% start of loop
\begin{letter}{\first~\last \\ \address \\ \postal~\town}%
\opening{Dear \first~\last,}%

    some text   
    
    some more text

        \closing{Best regards}

\end{letter}
} % end of for loop
\end{document}

然后我把所有的地址放在一个单独的文件中addresses.csv,如下所示:

Firstname,Lastname,Street,Postal,Town
John,Doe,Example Street 1, 12345, North Pole
Jane,Doe,Example Street 2, 12345, North Pole

当然,生成的字母可能不是你喜欢的样式。但你可以使用整个 KOMA 选项,只需检查KOMA手册

答案2

一个稍微简单的方法是记住你可以letter在一个文档中有多个环境。因此你可以写如下内容:

\documentclass{letter}
\address{YOUR ADDRESS GOES HERE}
\signature{YOUR SIGNATURE INFO HERE}

\NewDocumentCommand{\myletter}{mmmm}
  {
    \begin{letter}{#1}
    \opening{Dear #2:}
    Please present yourself at the #3 at #4 on #5.
    \closing{Threateningly,}
    \end{letter}
  }

\begin{document}
\myletter{Julius Caesar\\Subura\\Rome}{Julius}{Curia of Pompey}{3:00PM}{15 March 44 BC}
\myletter{Gaius Germanicus\\Temple of Castor and Pollux\\Rome}{Caligula}{Cryptoporticus}{3:00PM}{24 January 41}
\end{document}

答案3

这是使用该软件包的方法listofitems

\documentclass{article}
\usepackage{listofitems}
\newcommand\letterdata{
Sam, Main Street, 3 p.m., 14 April 2022\\
Lisa, Elm St., 1500, 25 December 2022}

\newcommand\stencil{
\noindent Dear <data>,

Please report to our <data> location at <data> on <data>.

\noindent kind regards,\\
The Management}

\setsepchar{\\/,}
\readlist\letteritems{\letterdata}
\setsepchar{<data>}
\readlist\stencilitems{\stencil}

\begin{document}

\foreachitem\z\in\letteritems[]{%
  \foreachitem\zz\in\stencilitems[]{%
    \zz 
    \ifnum\zzcnt<\listlen\stencilitems[]\letteritems[\zcnt,\zzcnt]\fi%
  }%
  \par\bigskip\noindent\hrulefill\par\bigskip
}

\end{document}

在此处输入图片描述

如果您需要不按顺序或重复使用数据项,那么这种变化就足够了:

\documentclass{article}
\usepackage{listofitems}
\newcommand\letterdata{
Sam, Main Street, 3 p.m., 14 April 2022\\
Lisa, Elm St., 1500, 25 December 2022}

\newcommand\stencil{%
\noindent Dear <data>{1},

Please report to our <data>{2} location at <data>{3} on <data>{4}.

Thank you again, <data>{1}, for your willingness to assist us in
  this matter.

\noindent kind regards,\\
The Management}

\setsepchar{\\/,}
\readlist*\letteritems{\letterdata}
\setsepchar{<data>}
\readlist\stencilitems{\stencil}

\newcommand\fillinthedata[2]{\letteritems[#1,#2]}
\newtoks\lettertoks
\newcommand\addtolettertoks[1]{%
      \lettertoks\expandafter{\the\lettertoks#1}}
\newcommand\xaddtolettertoks[1]{%
      \lettertoks\expandafter\expandafter\expandafter{\expandafter
      \the\expandafter\lettertoks#1}}
\begin{document}

\foreachitem\z\in\letteritems[]{%
  \lettertoks{}%
  \foreachitem\zz\in\stencilitems[]{%
    \xaddtolettertoks{\zz}%
    \ifnum\zzcnt<\listlen\stencilitems[]\relax
      \addtolettertoks{\fillinthedata}%
      \xaddtolettertoks{\expandafter{\zcnt}}%
    \fi%
  }%
  \the\lettertoks
  \par\bigskip\noindent\hrulefill\par\bigskip
}

\end{document}

在此处输入图片描述

相关内容