I'm interested in writing some form letters. I'm a big fan of scrletter2's features, notably placing the "to" address in just the right place for business envelopes.

For the mail merge, I've used datatools package in the past. It's pretty straightforward to use in conjunction with the article class. A MWE might look like

\documentclass{letter}
\usepackage{datatool}
\begin{document}
% load database
\DTLloaddb{scores}{StudentScores.csv}
\DTLforeach{scores}{\firstname=FirstName,\surname=Surname,\score=Score}{%

\begin{letter}{}
\opening{Dear \firstname\ \surname}
Congratulations on your score of \score !
\closing{Yours Sincerely}
\end{letter}

}
\end{document}

However, with scrletter2 things get hairy. Specifically, I'm having trouble figuring out how to place my commands.

I don't know how to post a MWE that's not horrendously long, but here's the gist:

\documentclass[11pt,english]{scrlttr2}
\usepackage{datatool}
\begin{document}

% a bunch of \setkomavar{fromname}{From name} and similar

\DTLloaddb{info}{Book1.csv}
% read database
\DTLforeach{info}{\firstname=FirstName,
\surname=LastName,
\position=Position,}{%

\begin{letter}{\firstname}
Text here
\end{letter}
}
\end{document}

I get the following error:

Runaway argument?
\@nil \@@ {scores}\fi \ifthenelse {\boolean {true}}{\refstepcounter {\ETC.
Paragraph ended before \@dtl@next was complete.
<to be read again> 
\par 
l.130 }

I feel like something's wrong with the placement of my \DTL commands within the document, but can't figure anything out.

  • Can you add a sample StudentScores.csv file? – egreg Jul 2 '15 at 16:00
  • egreg not uploading because esdd answered the question, I should have included in MWE – mbarete Jul 3 '15 at 18:56
up vote 4 down vote accepted

You have to enclose the letter environment by the \DTLforeach command or the DTLenvforeach environment. But you have to be accurate with the assign list: there should be no additional space before or after a key and no comma at the end of the assign list.

enter image description here

Code with the environment DTLenvforeach:

\begin{filecontents*}{test.csv}
FirstName,LastName,Score
John,"Smith, Jr",68
Jane,Brown,75
Andy,Brown,42
Z\"oe,Adams,52
Roger,Brady,58
Clare,Verdon,45
\end{filecontents*}

\documentclass[
  english,
  fromalign=right
]{scrlttr2}
\usepackage{babel}
\usepackage{datatool}

\begin{document}
\setkomavar{fromname}{Sydney Summer}
\setkomavar{fromaddress}{Entenstr. 5\\99999 Katzenhausen}

\DTLloaddb{info}{test.csv}
\begin{DTLenvforeach}{info}{\firstname=FirstName,\surname=LastName,\score=Score}
  \begin{letter}{\firstname{} \surname}
    \opening{Dear \firstname,}
    Congratulations on your score of \score !
    \closing{Yours sincerely}
  \end{letter}
\end{DTLenvforeach}
\end{document}

Or with the command \DTLforeach:

\documentclass[
  english,
  fromalign=right
]{scrlttr2}
\usepackage{babel}
\usepackage{datatool}

\begin{document}
\setkomavar{fromname}{Sydney Summer}
\setkomavar{fromaddress}{Entenstr. 5\\99999 Katzenhausen}

\DTLloaddb{info}{test.csv}
\DTLforeach{info}{\firstname=FirstName,\surname=LastName,\score=Score}{
  \begin{letter}{\firstname{} \surname}
    \opening{Dear \firstname,}
    Congratulations on your score of \score !
    \closing{Yours sincerely}
  \end{letter}
}
\end{document}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.