2

I am writing a form letter in latex. The addresses come from an external data source. The problem is that empty fields occur (quite frequently, actually) in the data source (see below). Therefore, because of \def\chopline#1,#2,#3,#4,#5,#6,#7,#8 \\ the line \expandafter\chopline\zeile\\ somehow expects at least one character behind a comma and consumes it. If there is no data in one field, it will just "eat" the comma and include the next data field, leading to a complete mess. Any advice on how to migitate the problem would be helpful.

Besides, I copied this example some time ago and don't understand all commands and their implications. I have the impression the structure I use is rather old and might not be the safest way to implement the behaviour I desire. What more advanced tools that LuaLaTeX offers could I use to improve the code that implements the form letter?

This is the code:

\documentclass{scrlttr2}

\usepackage{etoolbox}

\usepackage{fontspec}
\setmainfont{Linux Libertine O}
\setsansfont{Linux Biolinum O}
\setmonofont{Linux Libertine Mono O}

\def\chopline#1,#2,#3,#4,#5,#6,#7,#8 \\{
    \def\lastname{#1}
    \def\firstname{#2}
    \def\fanilyname{#3}
    \def\addrsuppa{#4}
    \def\street{#5}
    \def\addrsuppb{#6}
    \def\postcode{#7}
    \def\town{#8}
}

\newif\ifmore \moretrue

\begin{document}

\setkomavar{subject}{Subject}

\newread\quelle
\openin\quelle=../teilnehmer.csv

\read\quelle to \zeile

\loop
    \read\quelle to \zeile
    \ifeof\quelle
        \global\morefalse
    \else
        \expandafter\chopline\zeile\\
        \begin{letter}{\firstname\ \lastname \\
        \postcode\ \town}

        \opening{Sehr geehrte Familie \lastname,}

        [content]

        \closing{Best regards}

        \end{letter}  
    \fi
\ifmore\repeat

\closein\quelle

\end{document}

Here comes the data source (containing some dummies):

last_name,first_name,family_name,address_supplement_1,street,address_supplement_2,postcode,town,telephone_1,telephone_2,mail
Barnes,Helen,,Apartment 3,Potsdamer Platz 10,Neukölln,99823,Görlitz,3714 2487824,8797 9717692,[email protected]
Thomas,Brandon,,,Dorfstraße 34d,,20903,Aachen,9214 3252403,,[email protected]
Kelley,Juan,,,Hauptstraße 11,,00495,Hamburg,6265 9683011,6263 6235169,[email protected]
Fisher,Anne,,,Berliner Straße 99,,39856,Oldenburg,0666 6226586,7917 5816244,
Sanchez,Martha,Miller,,Mittelweg 23,,11932,Leverkusen,3728 6123496,,[email protected]
Morrison,Daniel,,,Schulstraße 8,,11943,Neustadt,9589 2520624,
Brown,David,Clark,Room 42,Bahnhofstraße 12,Amorbach,12821,Neckarsulm,9304 3674329,,[email protected]
Graham,Frank,,,Lindenallee 19,,32956,Neustadt,,,[email protected]
Price,Douglas,,,Mühlweg 5e,,04923,Neustadt,7934 4700638,7689 9255281,[email protected]
1
  • 1
    With \def\foo#1,#2,#3 \\ the input \foo X,,Z \\ will assign #1<-X, #2<- (empty) and #3<-Z. None of the first seven commas in your code will be used as argument. Your problem seems to be that you have more than eight fields. Probably datatool is better for such a project, because it can handle any number of fields.
    – egreg
    Sep 30, 2014 at 21:33

1 Answer 1

4

Here's a solution using datatool (assuming the data is in test.csv, change as appropriate):

\documentclass{scrlttr2}

\usepackage{etoolbox}

\usepackage{fontspec}
\setmainfont{Liberation Sans}% (I don't have the fonts from the original MWE)

\usepackage{datatool}

\DTLloaddb{mydata}{test.csv}

\begin{document}
\setkomavar{subject}{Subject}

\DTLforeach*{mydata}%
 {\LastName=last_name,\FirstName=first_name,%
  \AddrI=address_supplement_1,\Street=street,%
  \AddrII=address_supplement_2,\PostCode=postcode,\Town=town}%
 {%
    \begin{letter}{\FirstName\ \LastName\\%
    \DTLifnullorempty{\AddrI}{}{\AddrI\\}%
    \DTLifnullorempty{\Street}{}{\Street\\}%
    \DTLifnullorempty{\AddrII}{}{\AddrII\\}%
    \PostCode\\\Town}

    \opening{Sehr geehrt Familie \LastName,}

    [content]

    \closing{Best regards}
    \end{letter}
 }

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .