0

I have to write many letters where each time I have to modify just the names, dates and address, but it is always the same content.

A short version would be something like "Dear [name], from [address] you have to present yourself at [time] [date]."

Is there anyway to automate this? I would just like to insert the right fields for Latex to create the letter from a template.

Mauricio
  • 265
  • Could you set the letter as a new command with four variables? – Jay Apr 14 '22 at 13:15
  • @Jay could you provide an example? – Mauricio Apr 14 '22 at 13:18
  • I... can't find any tutorial to create new LaTeX macro on this site, but you should be able to find plenty on the Internet (hopefully). – user202729 Apr 14 '22 at 13:55
  • 1
    Check the datatool package. You simply create a CSV file with the fields and then read it to print what you want. – Sigur Apr 14 '22 at 14:10
  • 1
    https://tex.stackexchange.com/a/71322/11604 – Fran Apr 14 '22 at 14:27
  • @Mauricio I don't write letters. What I was thinking of was a new command which has several variables for the items in the letter which vary. The command would be a completely formatted letter with the variables would be [name], [address], etc. When you call the command the information would be the parameters of the command. Fran'a comment seems to be a better idea. – Jay Apr 14 '22 at 15:39

3 Answers3

2

I like to do it like this, using the datatool package (like also suggested in the comments) :

\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}

Then I have all the addresses in a separate addresses.csv, like this:

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

The resulting letter is probably not styled like you prefer, of course. But you have the whole world of KOMA options at your disposal, just check the KOMA manual!

codecepts
  • 396
  • 1
  • 4
1

A somewhat simpler approach is to remember that you can have multiple letter environments in a single document. Thus you could write something like this:

\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}

Don Hosek
  • 14,078
0

Here is a way with the listofitems package.

\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}

enter image description here

If you need to use the data items out of sequence, or repeatedly, then this variation would suffice:

\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}

enter image description here