# Creating form letter in LyX

This really refers to the following posting:

How do I insert names into birthday invitations automatically?

I've tried to used the suggested LaTeX coding in LyX and don't quite seem to get it to work.

Does anyone know if this is actually achievable in LyX or would I need to go back to native LaTeX code?

Here's an example - I would need to exchange the full name several times:

-

My approach (in LyX) is based on the LaTeX package "datatool".

For the name list, create a simple text file "studentlist.csv" with one name per line. The first line contains a heading!

Student Name
Peter M.
Tom J.

For the LyX document, add following to the "LaTeX Preamble":

\usepackage{datatool}
% load database 'studentlist' from file '/home/tux/studentlist.csv';
% in LyX the absolute path to the file is *necessary*!

Then write the main document starting with the following TeX code/ERT:

\DTLforeach{studentlist}
{\name=Student Name}
{

Write the main part of the document (Would a sans serif font look better?) and replace the names with TeX code/ERT \name. (The package "datatool" can do much more complex stuff, e.g., adding more fields or creating conditional texts - see this example or datatool user documentation.) Probably you would also like to set "Page Layout > Headings style: empty".

Finally, insert a "Clear Page" and put as TeX code/ERT }. This will looks like:

-
Brilliant. Thank you very much! I was nearby there, but my CSV file didn't have a header and I also missed out the "clear page"! – Joanne Demmler Apr 18 '13 at 18:53

What I did when faced with something similar (mail the requests for a program committee to review papers) was to write a skeleton file using markers for the data to be replaced (reviever name, email, papers to review, ...) and use a Perl script to take the values from a simple file (fields separated by ';', essentially) to write a specific message for each member.

With LaTeX I'd probably create a skeleton that \includes a file defining macros, and switch those around.

-
The Perl solution sounds interesting. Does this mean that you end up with multiple files that you then need to compile separately? – Joanne Demmler Apr 18 '13 at 18:59
@JoanneDemmler, that was what I did, yes. Or more precisely, I wrote a script that created a message to email from the template and the line with values on the fly and sent it out. Here I'd use the template to create a .tex, pdflatex it to PDF, rename/move to the final destination and go for the next entry in the control file. Here it is simpler, perhaps create files for each recipient, which \includes the template after defining commands for name and such? – vonbrand Apr 18 '13 at 19:05
I've played around with the macro and \include version for a while, but LyX bombarded me with unhelpful error messages (something about too may #'s). Another time... – Joanne Demmler Apr 18 '13 at 19:15
@JoanneDemmler, dump LyX. Use a honest text editor and command line, as Knuth intendeth. – vonbrand Apr 18 '13 at 19:26
I probably just got something wrong in the preamble. I don't mind plain LaTeX for shortish documents, but I get badly lost in my code in very long documents. – Joanne Demmler Apr 18 '13 at 19:30