# DTLforeach* - "unrecognized key in key option"

I'm quite new to the LaTeX universe, so ..., in fact, I'm not sure, if the DTLtool is best suited for my purposes. I'd like to use data from a .csv in a table environment used for all tables in the document. I want the whole thing to be as generic as possible, since the document (manual for data users) needs to be recompiled every now and then (whenever new survey data releases need to be documented, there might be different counts, new files, etc.). In general, the changes won't be dramatic, but (e.g.) changing (string & numeric) values coming from that .csv. I want to minimize errors (such as of copy/paste manually). Therefore, I'm assuming, I wouldn't know none of data contained in the current .csv (I could just open it, though.). I only know, it has two columns and need to put the values (sourcecount) for each file (sourceSUFfile) into the mentioned table, however.

Translated into what I've guessed the code might be - and as early as at the \DTLforeach* fails ("unrecognized key in key option"):

% set document class
\documentclass{article}

\usepackage{caption} %% for captions outside of floats
\usepackage{datatool}
\usepackage{filecontents}
\usepackage{pgffor} % for use of \foreach

% create database
\begin{filecontents*}{sourceSUFfiles.csv}
sourceSUFfile,sourcecount
SC1CohortProfile,2
SC1pParent,818
SC1xDirectMeasures,262
SC1xTargetCompetencies,5
SC2CohortProfile,2
SC2pParent,784
SC2pParentCORONA,6
SC2pTarget,326
SC2xTargetCompetencies,110
\end{filecontents*}

% load database and create macros
\newcommand{\rows}{\DTLrowcount}
\DTLforeach*{dbsource}{ %
\newcommand{\currentrow}{\dtlrownum}
\newcommand{\file\currentrow}{sourceSUFfile}
\newcommand{\sourcecount\currentrow}{sourcecount}
}

% start document (content)
\begin{document}

% put data into table with predefined format (specialTable)
\begingroup
%\renewcommand{\arraystretch}{1.2}
\begin{table}
%   \captionabove{data source files variables are selected from \label{tab:Sourcefiles}}
\begin{tabular}{lr} %
%\nth{Filename} & \nth{Number of variables selected}    \\
\foreach \n in {2-\rows} { % iterate through values 2 upto the number of rows (*not* counting the first [header row])
\file\n & \sourcecount\n % fill in filename of sourcefile and count of variables of that file
}
\end{tabular}
\end{table}
\endgroup

\end{document}


To sum it up: I want to print this .csv into my document without (as in a black box) knowing the number of rows or even the filenames and counts in advance. What might be the best way to accomplish this?

What's wrong with the keys (not even mentioned in the code)? Thanks a lot for any hints on that!

• Welcome to TeX SX! Your code is not entirely compilable, because it misses documentclass. Even after filling that, there seem to be some more syntax errors, which complicates testing and add unneccessary time. With datatool, you can load any arbitrary csv file and typeset it in table as you need. Only limitation is that the individual cells cant contain a paragraph break. Do you need help with that? Nov 10, 2021 at 9:16
• Thank you @TomášKruliš! I modified the code as reproducible as possible. I'm not sure, if the range of the row numbers ({2-\rows}) is specified correctly (and the problem with the keys remains). Yes, I could create a table using the datatool. What I'm searching for, is a way to create a table with a predefined layout for all tables and just insert the mere (string and numeric) values, which should be exctracted of the csv and stored in macros, respectively a priori. (The csv is automatically created by another programm.) Nov 10, 2021 at 12:30
• Please accept the answer if you found it useful. Nov 22, 2021 at 12:16

This simple template will work with two columns tables. You might store the headers in commands and insert them later when needed.

Note that the headers of the data base are not part of the rows. I you want to display only the rows delete the line  \bfseries Source SUF file & \bfseries Source count.

Use \begin{filecontents*}[overwrite] if the data will change. \usepackage{filecontents} is not longer needed.

% set document class
\documentclass{article}

\usepackage{caption} %% for captions outside of floats
\usepackage{datatool}

% create database
\begin{filecontents*}[overwrite]{sourceSUFfiles.csv}
sourceSUFfile,sourcecount
SC1CohortProfile,2
SC1pParent,818
SC1xDirectMeasures,262
SC1xTargetCompetencies,5
SC2CohortProfile,2
SC2pParent,784
SC2pParentCORONA,6
SC2pTarget,326
SC2xTargetCompetencies,110
\end{filecontents*}

\begin{document}

%   \DTLdisplaydb{dbsource} %^ to check the database loaded OK

\begin{table}[htbp]
\caption{data source files variables are selected from \label{tab:Sourcefiles}}
\centering
\begin{tabular}{lc}
\bfseries Source SUF file & \bfseries Source count
\DTLforeach{dbsource}{%

• Thanks Simon! Cool, I just hadn't understood, the \DTLforeach needs to be inserted into the {tabular} environment. Nov 22, 2021 at 11:49