# Serial letter from csv with table with multiple lines in one cell

I'm struggeling with creating a "serial letter" (not really a letter, but a document wich I will need different versions of for different people). I have all the data I need to insert in a csv file. In the beginning it was only supposed to make the document just personalized with names. It has 4 pages and it was only supposed to put the name of the person on the first page, so out would come a pdf with the 4-sided document for each person.

Now I want to create something similar, but now it has to do a little more than just inserting names. The last page is supposed to be a table containing their tasks. At the moment it's supposed to be one row per day and in the second column there should be the different tasks for the day (ideal with one line per task, still in one cell). I use the package datatool for that. Now the problem is to get the csv formatted right and to play it together nicely with babel (think that's one of the problems).

I know you all like minimal examples, so here you go.

\documentclass[parskip,twoside,9pt]{scrartcl}
\usepackage{fontspec}
\usepackage[german]{babel}
\usepackage{marvosym}
\usepackage{lmodern}
\usepackage{array}
\usepackage{wrapfig}
\usepackage{fancyhdr}
\tolerance=2000
\setlength{\emergencystretch}{20pt} %verhindert das herrausragen von Wörtern übers Zeilenende
\renewcommand{\familydefault}{\sfdefault}
\usepackage[table]{xcolor}
\usepackage{datatool}

\DTLsetseparator{;}

\begin{document}

\DTLforeach{Orga}{
\Mittwoch=Mittwoch,\Donnerstag=Donnerstag,\Freitag=Freitag,\Samstag=Samstag,\Sonntag=Sonntag}{
\renewcommand{\arraystretch}{4}
\begin{table}[h!]
\begin{tabular}{|p{2cm}|p{6cm}|}
\hline
Mittwoch & \Mittwoch \\\hline
Donnerstag & \Donnerstag \\\hline
Freitag & \Freitag \\\hline
Samstag & \Samstag \\\hline
Sonntag & \Sonntag \\\hline
\end{tabular}
\end{table}
\renewcommand{\arraystretch}{1}
}
\end{document}


This only creates the last page, the table.

This is the csv (obviously without names) I use:

Mittwoch;Donnerstag;Freitag;Samstag;Sonntag
;;18:30 – 20:00: Verantwortlicher Fachvortrag;;9:30 – 12:30: Verantwortlicher Workshops
Aufgabe2
;16:00 – 18:00: Verantwortlicher Anfangsplenum;07:00 – 14:00: Verantwortlicher E;9:00 – 12:00: Verantwortlicher Wanderung
13:00 – 14:30: Verantwortlicher Zwischenplenum;12:00 – 14:30: Verantwortlicher Endplenum
Aufgabe2
;13:00 – 16:00: Verantwortlicher Hostel;14:00 – 18:30: Verantwortlicher Workshops;;8:00 – 10:00: Verantwortlicher Hostel
Aufgabe2
;;;ab 19:30: Verantwortliche Kochen;Spätestens 21:00: Schnaps
;19:00 – 20:00: Verantwortlicher Abendessen;14:00 – 19:00: Verantwortlicher B;9:00 – 12:00: Verantwortlicher Stadtführung;14:30 – 16:00: Verantwortlicher Lunchpakete
Aufgabe 2
;13:00 – 16:00: Verantwortlicher Fachschaft;ab 19:00: Verantwortlicher Grillen;9:00 – 12:00: Verantwortlicher Wanderung
14:00 – 15:30: Verantwortlicher Mitgliederversammlung;Aufgabe
;;ab 20:30: Verantwortlicher Spieleabend;9:00 – 12:00: Verantwortlicher Stadtführung
Ab 20:30: Verantwortlicher H;Aufgabe
;ab 20:00: Verantwortlicher Kneipentour;;;16:00 – 19:00: Verantwortlicher Putzen
Aufgabe


And this is how it looked like in LibreOffice where I created that

• We don't use to add “[Solved]” to the title; the green check mark is sufficient. – egreg Mar 21 '20 at 17:04

Welcome to TeX.SE! I'm afraid your CSV file is a mess, and this is what causes most of the problems here. You have to check that every physical line of the CSV file contains the appropriate number of fields: five here, therefore each physical line of the file must contain four semi-colon (;) separators. This is definitely not the case in the file you provided, which causes datatool to output columns that show up as NULL.

This, plus the fact that your data file is not minimal cost me a good 30 minutes, only to try to guess where each row and cell starts and ends. I had to insert \\ in some cells, because you want a line break inside these cells, but your CSV file had a physical line break in these places, which causes the start of a new data row, and thus completely messes up the columns (after a physical newline, the datatool CSV parser is back at column 1: this is not the continuation of a cell!).

Now, these \\ commands I added in the CSV data should be convenient for you when writing the input data, but they required me to teach tabular that \\ mustn't be interpreted as marking the end of a tabular row. That is the purpose of the >{\let\\=\newline} I added for each column in the tabular header. With this, LaTeX understands that \\ is the same as \newline: a command for starting a new line in the current paragraph. As a consequence, we use \tabularnewline in order to finish a row of the tabular, since \\ means something else after \let\\=\newline.

Please include a much simpler data file next time, using something like this:

\begin{filecontents*}{test_file.csv}
a1;b1;c1;d1;e1
a2;b2;c2;d2;e2
a3;b3;c3;d3;e3
\end{filecontents*}


This makes things easier for everyone and demonstrates the issues much more clearly.

It would be better to move the \renewcommand{\arraystretch}{4} inside the table environment, just before the tabular starts. This way, the previous definition of \arraystretch would be automatically restored as soon as the table environment ends. In fact, you don't even need table in such a document. Simply use \newpage or \clearpage after each tabular, that is enough for what you do since there is no text outside the tabular environments (table is for making floats, but nothing needs to float here).

I used \DTLforeach* instead of \DTLforeach. The former is faster than the latter; use \DTLforeach only when you use commands from datatool that modify the table data in memory. Here, once the data is read, it is simply inserted in the document. The underlying data is not modified, thus \DTLforeach* is to be preferred.

Here is the result (I removed packages that are not used in your example: it is not minimal either...):

\begin{filecontents*}{Orga_test.csv}
Mittwoch;Donnerstag;Freitag;Samstag;Sonntag
;;18:30 – 20:00: Verantwortlicher Fachvortrag;;9:30 – 12:30: Verantwortlicher Workshops\\ Aufgabe2
;16:00 – 18:00: Verantwortlicher Anfangsplenum;07:00 – 14:00: Verantwortlicher E;9:00 – 12:00: Verantwortlicher Wanderung\\ 13:00 – 14:30: Verantwortlicher Zwischenplenum;12:00 – 14:30: Verantwortlicher Endplenum\\ Aufgabe2
;13:00 – 16:00: Verantwortlicher Hostel;14:00 – 18:30: Verantwortlicher Workshops;;8:00 – 10:00: Verantwortlicher Hostel\\ Aufgabe2
;;;ab 19:30: Verantwortliche Kochen;Spätestens 21:00: Schnaps
;19:00 – 20:00: Verantwortlicher Abendessen;14:00 – 19:00: Verantwortlicher B;9:00 – 12:00: Verantwortlicher Stadtführung;14:30 – 16:00: Verantwortlicher Lunchpakete\\ Aufgabe 2
13:00 – 16:00: Verantwortlicher Fachschaft;ab 19:00: Verantwortlicher Grillen;9:00 – 12:00: Verantwortlicher Wanderung;14:00 – 15:30: Verantwortlicher Mitgliederversammlung;Aufgabe
;;ab 20:30: Verantwortlicher Spieleabend;9:00 – 12:00: Verantwortlicher Stadtführung\\ Ab 20:30: Verantwortlicher H;Aufgabe
;ab 20:00: Verantwortlicher Kneipentour;;;16:00 – 19:00: Verantwortlicher Putzen\\ Aufgabe
\end{filecontents*}

\documentclass[parskip,twoside,fontsize=9pt]{scrartcl}
\usepackage{fontspec}
\usepackage[german]{babel}
\usepackage{lmodern}
\usepackage[a6paper, left=8.5mm, right=8.5mm, top=2.26cm, bottom=8.5mm,
\usepackage{array}

\tolerance=2000
\setlength{\emergencystretch}{20pt}
\renewcommand{\familydefault}{\sfdefault}

\usepackage{datatool}
\DTLsetseparator{;}

\begin{document}

\renewcommand{\arraystretch}{4}

\begin{center}
\DTLforeach*{Orga}{%
\Mittwoch=Mittwoch,\Donnerstag=Donnerstag,\Freitag=Freitag,%
\Samstag=Samstag,\Sonntag=Sonntag}{%
\noindent
\begin{tabular}{|>{\let\\=\newline}p{1.6cm}|>{\let\\=\newline}p{6cm}|}
\hline
Mittwoch   & \Mittwoch   \tabularnewline \hline
Donnerstag & \Donnerstag \tabularnewline \hline
Freitag    & \Freitag    \tabularnewline \hline
Samstag    & \Samstag    \tabularnewline \hline
Sonntag    & \Sonntag    \tabularnewline \hline
\end{tabular}
\newpage
}
\end{center}

\end{document}


• thanks for the tip. It seems the missing semicolons where the problem. Don't know why they disappeared, but exporting the table again fixed it now. PS: it seems tabular understands \newline just fine. I thought I'd have to use tabularx for that but that just gave more problems – Richard Rosner Jan 31 '20 at 9:19