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}
% load packages
\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
\DTLloaddb{dbsource}{sourceSUFfiles.csv}
\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} \\
%\addlinespace
\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!
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?{2-\rows}
) is specified correctly (and the problem with thekeys
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.)