Debian Codegraph @

texmf-dist/doc/latex/csvsimple/csvsimple-legacy.tex

raw
1 % \LaTeX-Main\
2 % !TeX encoding=UTF-8
3 %% The LaTeX package csvsimple - version 2.7.1 (2026/04/27)
4 %% csvsimple.tex: Manual
5 %%
6 %% -------------------------------------------------------------------------------------------
7 %% Copyright (c) 2008-2026 by Prof. Dr. Dr. Thomas F. Sturm <thomas dot sturm at unibw dot de>
8 %% -------------------------------------------------------------------------------------------
9 %%
10 %% This work may be distributed and/or modified under the
11 %% conditions of the LaTeX Project Public License, either version 1.3
12 %% of this license or (at your option) any later version.
13 %% The latest version of this license is in
14 %% http://www.latex-project.org/lppl.txt
15 %% and version 1.3 or later is part of all distributions of LaTeX
16 %% version 2005/12/01 or later.
17 %%
18 %% This work has the LPPL maintenance status `author-maintained'.
19 %%
20 %% This work consists of all files listed in README.md
21 %%
22 \documentclass[a4paper,11pt]{ltxdoc}
23 \usepackage{csvsimple-doc}
24
25 \usepackage{\csvpkgprefix csvsimple-legacy}
26
27 \tcbmakedocSubKey{docCsvKey}{csv}
28
29 \hypersetup{
30 pdftitle={Manual for the csvsimple-legacy package},
31 pdfauthor={Thomas F. Sturm},
32 pdfsubject={csv file processing with LaTeX2e},
33 pdfkeywords={csv file, comma separated values, key value syntax}
34 }
35
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 \begin{document}
38 \begin{center}
39 \begin{tcolorbox}[enhanced,hbox,tikznode,left=8mm,right=8mm,boxrule=0.4pt,
40 colback=white,colframe=black!50!yellow,
41 drop lifted shadow=black!50!yellow,arc is angular,
42 before=\par\vspace*{5mm},after=\par\bigskip]
43 {\bfseries\LARGE The \texttt{csvsimple-legacy} package}\\[3mm]
44 {\large Manual for version \version\ (\datum)}
45 \end{tcolorbox}
46 {\large Thomas F.~Sturm%
47 \footnote{Prof.~Dr.~Dr.~Thomas F.~Sturm, Institut f\"{u}r Mathematik und Informatik,
48 University of the Bundeswehr Munich, D-85577 Neubiberg, Germany;
49 email: \href{mailto:[email protected]}{[email protected]}}\par\medskip
50 \normalsize\url{https://www.ctan.org/pkg/csvsimple}\par
51 \url{https://github.com/T-F-S/csvsimple}
52 }
53 \end{center}
54 \bigskip
55 \begin{absquote}
56 \begin{center}\bfseries Abstract\end{center}
57 |csvsimple(-legacy)| provides a simple \LaTeX\ interface for the processing of files with
58 comma separated values (CSV). |csvsimple-legacy| relies heavily on the key value
59 syntax from |pgfkeys| which results in an easy way of usage.
60 Filtering and table generation is especially supported. Since the package
61 is considered as a lightweight tool, there is no support for data sorting
62 or data base storage.
63 \end{absquote}
64
65
66 \begin{tcolorbox}[enhanced,left=8mm,right=8mm,boxrule=2pt,boxsep=3mm,
67 colback=red!85!gray!5!white,colframe=red!85!gray,
68 arc is angular,arc=5mm,
69 before skip=1cm]
70 Actually, |csvsimple-legacy| is identical to the old version 1.22 (2021/06/07)
71 of |csvsimple|. It is superseded by |csvsimple-l3|, a \LaTeX3 implementation
72 of |csvsimple| which is a \emph{nearly} drop-in for the erstwhile implementation.
73 \begin{itemize}
74 \item If you are a new user or an experienced user of |csvsimple| creating a
75 new document, you are encouraged to turn to |csvsimple-l3|, see\\
76 \href{csvsimple-l3.pdf}{\flqq The |csvsimple-l3| package\frqq}
77 \item If you used |csvsimple| before version 2.00 in one or many documents,
78 there is \emph{no need} to change anything. Loading |csvsimple|
79 without options loads |csvsimple-legacy|.
80 |csvsimple-legacy| will be maintained to stay functional as it is for the
81 sake of compatibility to old documents.
82 \item Differences between |csvsimple-legacy| and |csvsimple-l3| are
83 discussed in \href{csvsimple.pdf}{\flqq The |csvsimple| package\frqq}.
84 \end{itemize}
85 \end{tcolorbox}
86
87
88 \clearpage
89 \tableofcontents
90
91 \clearpage
92 \section{Introduction}%
93 The |csvsimple-legacy| package is applied to the processing of
94 CSV\footnote{CSV file: file with comma separated values.} files.
95 This processing is controlled by key value assignments according to the
96 syntax of |pgfkeys|. Sample applications of the package
97 are tabular lists, serial letters, and charts.
98
99 An alternative to |csvsimple-legacy| is the |datatool| package
100 which provides considerably more functions and allows sorting of data by \LaTeX.
101 |csvsimple-legacy| has a different approach for the user interface and
102 is deliberately restricted to some basic functions with fast
103 processing speed.
104
105 Mind the following restrictions:
106 \begin{itemize}
107 \item Sorting is not supported directly but can be done
108 with external tools, see \Fullref{sec:Sorting}.
109 \item Values are expected to be comma separated, but the package
110 provides support for other separators, see \Fullref{sec:separators}.
111 \item Values are expected to be either not quoted or quoted with
112 curly braces |{}| of \TeX\ groups. Other quotes like doublequotes
113 are not supported directly, but can be achieved
114 with external tools, see \Fullref{sec:importeddata}.
115 \item Every data line is expected to contain the same amount of values.
116 Unfeasible data lines are silently ignored by default, but this can
117 be configured, see \Fullref{sec:consistency}.
118 \end{itemize}
119
120
121 \subsection{Loading the Package}
122 The package |csvsimple-legacy| loads the packages
123 |pgfkeys|,
124 |etoolbox|,
125 and |ifthen|.
126 |csvsimple-legacy| itself is loaded with \emph{one} of the following
127 alternatives inside the preamble:
128 \begin{dispListing}
129 \usepackage{csvsimple}
130 % or alternatively (not simultaneously!)
131 \usepackage[legacy]{csvsimple}
132 % or alternatively (not simultaneously!)
133 \usepackage{csvsimple-legacy}
134 \end{dispListing}
135
136
137 Not automatically loaded, but used for many examples are the packages
138 |longtable|
139 and
140 |booktabs|.
141
142
143 \clearpage
144 \subsection{First Steps}
145 Every line of a processable CSV file has to contain an identical amount of
146 comma\footnote{See \refKey{/csv/separator} for other separators than comma.} separated values. The curly braces |{}| of \TeX\ groups can be used
147 to mask a block which may contain commas not to be processed as separators.
148
149 The first line of such a CSV file is usually but not necessarily a header line
150 which contains the identifiers for each column.
151
152 %-- file embedded for simplicity --
153 \begin{tcbverbatimwrite}{grade.csv}
154 name,givenname,matriculation,gender,grade
155 Maier,Hans,12345,m,1.0
156 Huber,Anna,23456,f,2.3
157 Weißbäck,Werner,34567,m,5.0
158 Bauer,Maria,19202,f,3.3
159 \end{tcbverbatimwrite}
160 %-- end embedded file --
161
162 \csvlisting{grade}
163
164 \smallskip
165 The most simple way to display a CSV file in tabular form is the processing
166 with the \refCom{csvautotabular} command.
167
168 \begin{dispExample}
169 \csvautotabular{grade.csv}
170 \end{dispExample}
171
172
173 Typically, one would use \refCom{csvreader} instead of |\csvautotabular| to
174 gain full control over the interpretation of the included data.
175
176 In the following example, the entries of the header line are automatically
177 assigned to \TeX\ macros which may be used deliberately.
178
179
180 \begin{dispExample}
181 \begin{tabular}{|l|c|}\hline%
182 \bfseries Person & \bfseries Matr.~No.
183 \csvreader[head to column names]{grade.csv}{}%
184 {\\\givenname\ \name & \matriculation}%
185 \\\hline
186 \end{tabular}
187 \end{dispExample}
188
189
190 \clearpage
191 |\csvreader| is controlled by a plenty of options. For example, for table
192 applications line breaks are easily inserted by
193 \refKey{/csv/late after line}. This defines a macro execution just before
194 the following line.
195 Additionally, the assignment of columns to \TeX\ macros is shown in a non automated
196 way.
197
198 \begin{dispExample}
199 \begin{tabular}{|r|l|c|}\hline%
200 & Person & Matr.~No.\\\hline\hline
201 \csvreader[late after line=\\\hline]%
202 {grade.csv}{name=\name,givenname=\firstname,matriculation=\matnumber}%
203 {\thecsvrow & \firstname~\name & \matnumber}%
204 \end{tabular}
205 \end{dispExample}
206
207 \smallskip
208 An even more comfortable and preferrable way to create a table is setting
209 appropriate option keys. Note, that this gives you the possibility to create a
210 |pgfkeys| style which contains the whole table creation.
211
212 \begin{dispExample}
213 \csvreader[tabular=|r|l|c|,
214 table head=\hline & Person & Matr.~No.\\\hline\hline,
215 late after line=\\\hline]%
216 {grade.csv}{name=\name,givenname=\firstname,matriculation=\matnumber}%
217 {\thecsvrow & \firstname~\name & \matnumber}%
218 \end{dispExample}
219
220 \smallskip
221 The next example shows such a style definition with the convenience macro
222 \refCom{csvstyle}. Here, we see again the automated assignment of header
223 entries to column names by \refKey{/csv/head to column names}.
224 For this, the header entries have to be without spaces and special characters.
225 But you can always assign entries to canonical macro names by hand like in the examples
226 above. Here, we also add a \refKey{/csv/head to column names prefix} to avoid
227 macro name clashes.
228
229 \begin{dispExample}
230 \csvstyle{myTableStyle}{tabular=|r|l|c|,
231 table head=\hline & Person & Matr.~No.\\\hline\hline,
232 late after line=\\\hline,
233 head to column names,
234 head to column names prefix=MY,
235 }
236
237 \csvreader[myTableStyle]{grade.csv}{}%
238 {\thecsvrow & \MYgivenname~\MYname & \MYmatriculation}%
239 \end{dispExample}
240
241
242 \clearpage
243 Another way to address columns is to use their roman numbers.
244 The direct addressing is done by |\csvcoli|, |\csvcolii|, |\csvcoliii|, \ldots:
245
246 \begin{dispExample}
247 \csvreader[tabular=|r|l|c|,
248 table head=\hline & Person & Matr.~No.\\\hline\hline,
249 late after line=\\\hline]%
250 {grade.csv}{}%
251 {\thecsvrow & \csvcolii~\csvcoli & \csvcoliii}%
252 \end{dispExample}
253
254 \smallskip
255 And yet another method to assign macros to columns is to use arabic numbers
256 for the assignment:
257
258 \begin{dispExample}
259 \csvreader[tabular=|r|l|c|,
260 table head=\hline & Person & Matr.~No.\\\hline\hline,
261 late after line=\\\hline]%
262 {grade.csv}{1=\name,2=\firstname,3=\matnumber}%
263 {\thecsvrow & \firstname~\name & \matnumber}%
264 \end{dispExample}
265
266 \smallskip
267 For recurring applications, the |pgfkeys| syntax allows to create own styles
268 for a consistent and centralized design. The following example is easily
269 modified to obtain more or less option settings.
270
271 \begin{dispExample}
272 \csvset{myStudentList/.style={%
273 tabular=|r|l|c|,
274 table head=\hline & Person & #1\\\hline\hline,
275 late after line=\\\hline,
276 column names={name=\name,givenname=\firstname}
277 }}
278
279 \csvreader[myStudentList={Matr.~No.}]{grade.csv}{matriculation=\matnumber}%
280 {\thecsvrow & \firstname~\name & \matnumber}%
281 \hfill%
282 \csvreader[myStudentList={Grade}]{grade.csv}{grade=\grade}%
283 {\thecsvrow & \firstname~\name & \grade}%
284 \end{dispExample}
285
286
287 \clearpage
288 Alternatively, column names can be set by \refCom{csvnames}
289 and style definitions by \refCom{csvstyle}.
290 With this, the last example is rewritten as follows:
291
292 \begin{dispExample}
293 \csvnames{myNames}{1=\name,2=\firstname,3=\matnumber,5=\grade}
294 \csvstyle{myStudentList}{tabular=|r|l|c|,
295 table head=\hline & Person & #1\\\hline\hline,
296 late after line=\\\hline, myNames}
297
298 \csvreader[myStudentList={Matr.~No.}]{grade.csv}{}%
299 {\thecsvrow & \firstname~\name & \matnumber}%
300 \hfill%
301 \csvreader[myStudentList={Grade}]{grade.csv}{}%
302 {\thecsvrow & \firstname~\name & \grade}%
303 \end{dispExample}
304
305 \smallskip
306 The data lines of a CSV file can also be filtered. In the following example,
307 a certificate is printed only for students with grade unequal to 5.0.
308
309 \begin{dispExample}
310 \csvreader[filter not strcmp={\grade}{5.0}]%
311 {grade.csv}{1=\name,2=\firstname,3=\matnumber,4=\gender,5=\grade}%
312 {\begin{center}\Large\bfseries Certificate in Mathematics\end{center}
313 \large\ifcsvstrcmp{\gender}{f}{Ms.}{Mr.}
314 \firstname~\name, matriculation number \matnumber, has passed the test
315 in mathematics with grade \grade.\par\ldots\par
316 }%
317 \end{dispExample}
318
319
320 \clearpage
321 \section{Macros for the Processing of CSV Files}\label{sec:makros}%
322
323 \begin{docCommand}{csvreader}{\oarg{options}\marg{file name}\marg{assignments}\marg{command list}}
324 |\csvreader| reads the file denoted by \meta{file name} line by line.
325 Every line of the file has to contain an identical amount of
326 comma separated values. The curly braces |{}| of \TeX\ groups can be used
327 to mask a block which may contain commas not to be processed as separators.\smallskip
328
329 The first line of such a CSV file is by default but not necessarily
330 processed as a header line which contains the identifiers for each column.
331 The entries of this line can be used to give \meta{assignments} to \TeX\ macros
332 to address the columns. The number of entries of this first line
333 determines the accepted number of entries for all following lines.
334 Every line which contains a higher or lower number of entries is ignored
335 during standard processing.\smallskip
336
337 The \meta{assignments} are given by key value pairs
338 \mbox{\meta{name}|=|\meta{macro}}. Here, \meta{name} is an entry from the
339 header line \emph{or} the arabic number of the addressed column.
340 \meta{macro} is some \TeX\ macro which gets the content of the addressed column.\smallskip
341
342 The \meta{command list} is executed for every accepted data line. Inside the
343 \meta{command list} is applicable:
344 \begin{itemize}
345 \item \docAuxCommand{thecsvrow} or the counter |csvrow| which contains the number of the
346 current data line (starting with 1).
347 \item \docAuxCommand{csvcoli}, \docAuxCommand{csvcolii}, \docAuxCommand{csvcoliii}, \ldots,
348 which contain the contents of the column entries of the current data line.
349 Alternatively can be used:
350 \item \meta{macro} from the \meta{assignments} to have a logical
351 addressing of a column entry.
352 \end{itemize}
353 Note, that the \meta{command list} is allowed to contain |\par| and
354 that all macro definitions are made global to be used for table applications.\smallskip
355
356 The processing of the given CSV file can be controlled by various
357 \meta{options} given as key value list. The feasible option keys
358 are described in section \ref{sec:schluessel} from page \pageref{sec:schluessel}.
359
360 \begin{dispExample}
361 \csvreader[tabular=|r|l|l|, table head=\hline, table foot=\hline]{grade.csv}%
362 {name=\name,givenname=\firstname,grade=\grade}%
363 {\grade & \firstname~\name & \csvcoliii}
364 \end{dispExample}
365
366 Mainly, the |\csvreader| command consists of a \refCom{csvloop} macro with
367 following parameters:\par
368 |\csvloop{|\meta{options}|, file=|\meta{file name}|, column names=|\meta{assignments}|,|\\
369 \hspace*{2cm} |command=|\meta{command list}|}|\par
370 Therefore, the application of the keys \refKey{/csv/file} and \refKey{/csv/command}
371 is useless for |\csvreader|.
372 \end{docCommand}
373
374 \begin{docCommand}{csvloop}{\marg{options}}
375 Usually, \refCom{csvreader} may be preferred instead of |\csvloop|.
376 \refCom{csvreader} is based on |\csvloop| which takes a mandatory list of
377 \meta{options} in key value syntax.
378 This list of \meta{options} controls the total processing. Especially,
379 it has to contain the CSV file name.
380 \begin{dispExample}
381 \csvloop{file={grade.csv}, head to column names, command=\name,
382 before reading={List of students:\ },
383 late after line={{,}\ }, late after last line=.}
384 \end{dispExample}
385 \end{docCommand}
386
387 \clearpage
388 The following |\csvauto...| commands are intended for quick data overview
389 with limited formatting potential.
390 See Subsection~\ref{subsec:tabsupport} on page \pageref{subsec:tabsupport}
391 for the general table options in combination with \refCom{csvreader} and
392 \refCom{csvloop}.
393
394 \begin{docCommand}{csvautotabular}{\oarg{options}\marg{file name}}
395 |\csvautotabular| is an abbreviation for the application of the option key
396 \refKey{/csv/autotabular} together with other \meta{options} to \refCom{csvloop}.
397 This macro reads the whole CSV file denoted by \meta{file name}
398 with an automated formatting.
399 \begin{dispExample}
400 \csvautotabular{grade.csv}
401 \end{dispExample}
402 \begin{dispExample}
403 \csvautotabular[filter equal={\csvcoliv}{f}]{grade.csv}
404 \end{dispExample}
405 \end{docCommand}
406
407
408 \begin{docCommand}{csvautolongtable}{\oarg{options}\marg{file name}}
409 |csvautolongtable| is an abbreviation for the application of the option key
410 \refKey{/csv/autolongtable} together with other \meta{options} to \refCom{csvloop}.
411 This macro reads the whole CSV file denoted by \meta{file name}
412 with an automated formatting.
413 For application, the package |longtable| is required which has to be
414 loaded in the preamble.
415 \begin{dispListing}
416 \csvautolongtable{grade.csv}
417 \end{dispListing}
418 \csvautolongtable{grade.csv}
419 \end{docCommand}
420
421 \clearpage
422
423 \begin{docCommand}{csvautobooktabular}{\oarg{options}\marg{file name}}
424 |\csvautobooktabular| is an abbreviation for the application of the option key
425 \refKey{/csv/autobooktabular} together with other \meta{options} to \refCom{csvloop}.
426 This macro reads the whole CSV file denoted by \meta{file name}
427 with an automated formatting.
428 For application, the package |booktabs| is required which has to be
429 loaded in the preamble.
430 \begin{dispExample}
431 \csvautobooktabular{grade.csv}
432 \end{dispExample}
433 \end{docCommand}
434
435
436 \begin{docCommand}{csvautobooklongtable}{\oarg{options}\marg{file name}}
437 |csvautobooklongtable| is an abbreviation for the application of the option key
438 \refKey{/csv/autobooklongtable} together with other \meta{options} to \refCom{csvloop}.
439 This macro reads the whole CSV file denoted by \meta{file name}
440 with an automated formatting.
441 For application, the packages |booktabs| and |longtable| are required which have to be
442 loaded in the preamble.
443 \begin{dispListing}
444 \csvautobooklongtable{grade.csv}
445 \end{dispListing}
446 \csvautobooklongtable{grade.csv}
447 \end{docCommand}
448
449
450
451 \clearpage
452
453 \begin{docCommand}{csvset}{\marg{options}}
454 Sets \meta{options} for every following
455 \refCom{csvreader} and \refCom{csvloop}. For example, this command may
456 be used for style definitions.
457 \begin{dispExample}
458 \csvset{grade list/.style=
459 {column names={name=\name,givenname=\firstname,grade=\grade}},
460 passed/.style={filter not strcmp={\grade}{5.0}} }
461
462 The following students passed the test in mathematics:
463 \csvreader[grade list,passed]{grade.csv}{}{\firstname\ \name\ (\grade); }%
464 \end{dispExample}
465 \end{docCommand}
466
467
468 \begin{docCommand}{csvstyle}{\marg{key}\marg{options}}
469 Abbreviation for |\csvset{|\meta{key}|/.style=|\marg{options}|}|
470 to define a new style.
471 \end{docCommand}
472
473 \begin{docCommand}{csvnames}{\marg{key}\marg{assignments}}
474 Abbreviation for |\csvset{|\meta{key}|/.style={column names=|\marg{assignments}|}}|
475 to define additional \meta{assignments} of macros to columns.
476 \begin{dispExample}
477 \csvnames{grade list}{name=\name,givenname=\firstname,grade=\grade}
478 \csvstyle{passed}{filter not strcmp={\grade}{5.0}}
479
480 The following students passed the test in mathematics:
481 \csvreader[grade list,passed]{grade.csv}{}{\firstname\ \name\ (\grade); }%
482 \end{dispExample}
483 \end{docCommand}
484
485
486 \begin{docCommand}{csvheadset}{\marg{assignments}}
487 For some special cases, this command can be used to change the
488 \meta{assignments} of macros to columns during execution of
489 \refCom{csvreader} and \refCom{csvloop}.
490 \begin{dispExample}
491 \csvreader{grade.csv}{}%
492 { \csvheadset{name=\n} \fbox{\n}
493 \csvheadset{givenname=\n} \ldots\ \fbox{\n} }%
494 \end{dispExample}
495 \end{docCommand}
496
497 \clearpage
498
499 \begin{docCommand}{csviffirstrow}{\marg{then macros}\marg{else macros}}
500 Inside the command list of \refCom{csvreader}, the \meta{then macros}
501 are executed for the first data line, and the \meta{else macros}
502 are executed for all following lines.
503 \begin{dispExample}
504 \csvreader[tabbing, head to column names, table head=\hspace*{3cm}\=\kill]%
505 {grade.csv}{}%
506 {\givenname~\name \> (\csviffirstrow{first entry!!}{following entry})}
507 \end{dispExample}
508 \end{docCommand}
509
510
511 \begin{docCommand}{csvifoddrow}{\marg{then macros}\marg{else macros}}
512 Inside the command list of \refCom{csvreader}, the \meta{then macros}
513 are executed for odd-numbered data lines, and the \meta{else macros}
514 are executed for even-numbered lines.
515 \begin{dispExample}
516 \csvreader[head to column names,tabular=|l|l|l|l|,
517 table head=\hline\bfseries \# & \bfseries Name & \bfseries Grade\\\hline,
518 table foot=\hline]{grade.csv}{}{%
519 \csvifoddrow{\slshape\thecsvrow & \slshape\name, \givenname & \slshape\grade}%
520 {\bfseries\thecsvrow & \bfseries\name, \givenname & \bfseries\grade}}
521 \end{dispExample}
522
523 The |\csvifoddrow| macro may be used for striped tables:
524
525 \begin{dispExample}
526 % This example needs the xcolor package
527 \csvreader[head to column names,tabular=rlcc,
528 table head=\hline\rowcolor{red!50!black}\color{white}\# & \color{white}Person
529 & \color{white}Matr.~No. & \color{white}Grade,
530 late after head=\\\hline\rowcolor{yellow!50},
531 late after line=\csvifoddrow{\\\rowcolor{yellow!50}}{\\\rowcolor{red!25}}]%
532 {grade.csv}{}%
533 {\thecsvrow & \givenname~\name & \matriculation & \grade}%
534 \end{dispExample}
535
536 \enlargethispage*{1cm}
537 Alternatively, |\rowcolors| from the |xcolor| package can be used for this
538 purpose:
539
540 \begin{dispExample}
541 % This example needs the xcolor package
542 \csvreader[tabular=rlcc, before table=\rowcolors{2}{red!25}{yellow!50},
543 table head=\hline\rowcolor{red!50!black}\color{white}\# & \color{white}Person
544 & \color{white}Matr.~No. & \color{white}Grade\\\hline,
545 head to column names]{grade.csv}{}%
546 {\thecsvrow & \givenname~\name & \matriculation & \grade}%
547 \end{dispExample}
548 \end{docCommand}
549
550 \clearpage
551
552 \begin{docCommand}{csvfilteraccept}{}
553 All following consistent data lines will be accepted and processed.
554 This command overwrites all previous filter settings and may be used
555 inside \refKey{/csv/full filter} to implement
556 an own filtering rule together with |\csvfilterreject|.
557 \begin{dispExample}
558 \csvreader[autotabular,
559 full filter=\ifcsvstrcmp{\csvcoliv}{m}{\csvfilteraccept}{\csvfilterreject}
560 ]{grade.csv}{}{\csvlinetotablerow}%
561 \end{dispExample}
562 \end{docCommand}
563
564
565 \begin{docCommand}{csvfilterreject}{}
566 All following data lines will be ignored.
567 This command overwrites all previous filter settings.
568 \end{docCommand}
569
570
571 \begin{docCommand}{csvline}{}
572 This macro contains the current and unprocessed data line.
573 \begin{dispExample}
574 \csvreader[no head, tabbing, table head=\textit{line XX:}\=\kill]%
575 {grade.csv}{}{\textit{line \thecsvrow:} \> \csvline}%
576 \end{dispExample}
577 \end{docCommand}
578
579
580 \begin{docCommand}{thecsvrow}{}
581 Typesets the current data line number. This is the
582 current number of accepted data lines without the header line.
583 The \LaTeX\ counter |csvrow| can be addressed directly in the usual way,
584 e.\,g. by |\roman{csvrow}|.
585 \end{docCommand}
586
587
588 \begin{docCommand}{thecsvinputline}{}
589 Typesets the current file line number. This is the
590 current number of all data lines including the header line.
591 The \LaTeX\ counter |csvinputline| can be addressed directly in the usual way,
592 e.\,g. by |\roman{csvinputline}|.
593 \begin{dispExample}
594 \csvreader[no head, filter test=\ifnumequal{\thecsvinputline}{3}]%
595 {grade.csv}{}%
596 {The line with number \thecsvinputline\ contains: \csvline}%
597 \end{dispExample}
598 \end{docCommand}
599
600
601 \begin{docCommand}[doc updated=2016-07-01]{csvlinetotablerow}{}
602 Typesets the current processed data line with |&| between the entries.
603 %Most users will never apply this command.
604 \end{docCommand}
605
606
607
608 \clearpage
609 \section{Option Keys}\label{sec:schluessel}%
610 For the \meta{options} in \refCom{csvreader} respectively \refCom{csvloop}
611 the following |pgf| keys can be applied. The key tree path |/csv/| is not
612 to be used inside these macros.
613
614
615 \subsection{Command Definition}%--------%[[
616
617 \begin{docCsvKey}{before reading}{=\meta{code}}{no default, initially empty}
618 Sets the \meta{code} to be executed before the CSV file is processed.
619 \end{docCsvKey}
620
621 \begin{docCsvKey}{after head}{=\meta{code}}{no default, initially empty}
622 Sets the \meta{code} to be executed after the header line is read.
623 \end{docCsvKey}
624
625 \begin{docCsvKey}{before filter}{=\meta{code}}{no default, initially empty}
626 Sets the \meta{code} to be executed after reading and consistency checking
627 of a data line. They are executed before any filter condition is checked,
628 see \refKey{/csv/filter}.
629 Also see \refKey{/csv/full filter}.
630 \end{docCsvKey}
631
632 \begin{docCsvKey}{late after head}{=\meta{code}}{no default, initially empty}
633 Sets the \meta{code} to be executed after reading and disassembling
634 of the first accepted data line. They are executed before further processing
635 of this line.
636 \end{docCsvKey}
637
638 \begin{docCsvKey}{late after line}{=\meta{code}}{no default, initially empty}
639 Sets the \meta{code} to be executed after reading and disassembling
640 of the next accepted data line (after \refKey{/csv/before filter}).
641 They are executed before further processing of this next line.
642 |late after line| overwrites |late after first line| and |late after last line|.
643 Note that table options like \refKey{/csv/tabular} set this key to |\\|
644 automatically.
645 \end{docCsvKey}
646
647
648 \begin{docCsvKey}{late after first line}{=\meta{code}}{no default, initially empty}
649 Sets the \meta{code} to be executed after reading and disassembling
650 of the second accepted data line instead of \refKey{/csv/late after line}.
651 This key has to be set after |late after line|.
652 \end{docCsvKey}
653
654
655 \begin{docCsvKey}{late after last line}{=\meta{code}}{no default, initially empty}
656 Sets the \meta{code} to be executed after processing of the last
657 accepted data line instead of \refKey{/csv/late after line}.
658 This key has to be set after |late after line|.
659 \end{docCsvKey}
660
661
662 \begin{docCsvKey}{before line}{=\meta{code}}{no default, initially empty}
663 Sets the \meta{code} to be executed after \refKey{/csv/late after line}
664 and before \refKey{/csv/command}.
665 |before line| overwrites |before first line|.
666 \end{docCsvKey}
667
668
669 \begin{docCsvKey}{before first line}{=\meta{code}}{no default, initially empty}
670 Sets the \meta{code} to be executed instead of \refKey{/csv/before line}
671 for the first accepted data line.
672 This key has to be set after |before line|.
673 \end{docCsvKey}
674
675
676 \begin{docCsvKey}{command}{=\meta{code}}{no default, initially \cs{csvline}}
677 Sets the \meta{code} to be executed for every accepted data line.
678 They are executed between \refKey{/csv/before line} and \refKey{/csv/after line}.
679 \end{docCsvKey}
680
681
682 \begin{docCsvKey}{after line}{=\meta{code}}{no default, initially empty}
683 Sets the \meta{code} to be executed for every accepted data line
684 after \refKey{/csv/command}.
685 |after line| overwrites |after first line|.
686 \end{docCsvKey}
687
688
689 \begin{docCsvKey}{after first line}{=\meta{code}}{no default, initially empty}
690 Sets the \meta{code} to be executed instead of \refKey{/csv/after line}
691 for the first accepted data line.
692 This key has to be set after |after line|.
693 \end{docCsvKey}
694
695 \begin{docCsvKey}{after reading}{=\meta{code}}{no default, initially empty}
696 Sets the \meta{code} to be executed after the CSV file is processed.
697 \end{docCsvKey}
698
699
700 \begin{dispExample}
701 \csvreader[
702 before reading = \meta{before reading}\\,
703 after head = \meta{after head},
704 before filter = \\\meta{before filter},
705 late after head = \meta{late after head},
706 late after line = \meta{late after line},
707 late after first line = \meta{late after first line},
708 late after last line = \\\meta{late after last line},
709 before line = \meta{before line},
710 before first line = \meta{before first line},
711 after line = \meta{after line},
712 after first line = \meta{after first line},
713 after reading = \\\meta{after reading}
714 ]{grade.csv}{name=\name}{\textbf{\name}}%
715 \end{dispExample}
716
717 Additional command definition keys are provided for the supported tables,
718 see Section~\ref{subsec:tabsupport} from page~\pageref{subsec:tabsupport}.
719
720 \clearpage
721 \subsection{Header Processing and Column Name Assignment}%
722
723 \begin{docCsvKey}{head}{\colOpt{=true\textbar false}}{default |true|, initially |true|}
724 If this key is set, the first line of the CSV file is treated as a header
725 line which can be used for column name assignments.
726 \end{docCsvKey}
727
728
729 \begin{docCsvKey}{no head}{}{no value}
730 Abbreviation for |head=false|, i.\,e. the first line of the CSV file is
731 treated as data line.
732 Note that this option cannot be used in combination with
733 \refCom{csvautotabular}, \refKey{/csv/autotabular}, and similar automated commands/options.
734 See Section~\ref{noheader} on page~\pageref{noheader} for assistance.
735 \end{docCsvKey}
736
737 \begin{docCsvKey}{column names}{=\meta{assignments}}{no default, initially empty}
738 Adds some new \meta{assignments} of macros to columns in key value syntax.
739 Existing assignments are kept.
740 \end{docCsvKey}
741
742
743 \begin{docCsvKey}{column names reset}{}{no value}
744 Clears all assignments of macros to columns.
745 \end{docCsvKey}
746
747
748 \begin{docCsvKey}{head to column names}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
749 If this key is set, the entries of the header line are used automatically
750 as macro names for the columns. This option can be used only, if
751 the header entries do not contain spaces and special characters to be
752 used as feasible \LaTeX\ macro names.
753 Note that the macro definition is \emph{global} and may therefore override
754 existing macros for the rest of the document. Adding
755 \refKey{/csv/head to column names prefix} may help to avoid unwanted
756 overrides.
757 \end{docCsvKey}
758
759
760 \begin{docCsvKey}[][doc new=2019-07-16]{head to column names prefix}{=\meta{text}}{no default, initially empty}
761 The given \meta{text} is prefixed to the name of all macros generated by
762 \refKey{/csv/head to column names}. For example, if you use the settings
763 \begin{dispListing}
764 head to column names,
765 head to column names prefix=MY,
766 \end{dispListing}
767 a header entry |section| will generate the corresponding macro
768 |\MYsection| instead of destroying the standard \LaTeX\ |\section| macro.
769 \end{docCsvKey}
770
771
772 \clearpage
773 \subsection{Consistency Check}\label{sec:consistency}%
774
775 \begin{docCsvKey}{check column count}{\colOpt{=true\textbar false}}{default |true|, initially |true|}
776 This key defines, wether the number of entries in a data line is checked against
777 an expected value or not.\\
778 If |true|, every non consistent line is ignored without announcement.\\
779 If |false|, every line is accepted and may produce an error during
780 further processing.
781 \end{docCsvKey}
782
783
784 \begin{docCsvKey}{no check column count}{}{no value}
785 Abbreviation for |check column count=false|.
786 \end{docCsvKey}
787
788
789 \begin{docCsvKey}{column count}{=\meta{number}}{no default}
790 Sets the \meta{number} of feasible entries per data line.
791 This setting is only useful in connection with \refKey{/csv/no head},
792 since \meta{number} would be replaced by the number of entries in the
793 header line otherwise.
794 \end{docCsvKey}
795
796
797 \begin{docCsvKey}{on column count error}{=\meta{code}}{no default, initially empty}
798 \meta{code} to be executed for unfeasible data lines.
799 \end{docCsvKey}
800
801
802 \begin{docCsvKey}{warn on column count error}{}{style, no value}
803 Display of a warning for unfeasible data lines.
804 \end{docCsvKey}
805
806
807 \clearpage
808 \subsection{Filtering}%
809
810 \begin{docCsvKey}[][doc new=2016-07-01]{filter test}{=\meta{condition}}{no default}
811 Only data lines which fulfill a logical \meta{condition} are accepted.
812 For the \meta{condition}, every single test normally employed like
813 \begin{dispListing}
814 \iftest{some testing}{true}{false}
815 \end{dispListing}
816 can be used as
817 \begin{dispListing}
818 filter test=\iftest{some testing},
819 \end{dispListing}
820 For |\iftest|, tests from the |etoolbox| package like
821 |\ifnumcomp|, |\ifdimgreater|, etc. and from \Fullref{sec:stringtests} can be used.
822
823 \begin{dispExample}
824 \csvreader[head to column names,tabular=llll,
825 table head=\toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
826 table foot=\bottomrule,
827 %>> list only matriculation numbers greater than 20000 <<
828 filter test=\ifnumgreater{\matriculation}{20000},
829 ]{grade.csv}{}{%
830 \thecsvrow & \slshape\name, \givenname & \matriculation & \grade}
831 \end{dispExample}
832 \end{docCsvKey}
833
834
835 \begin{docCsvKey}{filter strcmp}{=\marg{stringA}\marg{stringB}}{style, no default}
836 Only lines where \meta{stringA} and \meta{stringB} are equal after expansion
837 are accepted.
838 The implementation is done with \refCom{ifcsvstrcmp}.
839 \end{docCsvKey}
840
841
842 \begin{docCsvKey}{filter not strcmp}{=\marg{stringA}\marg{stringB}}{style, no default}
843 Only lines where \meta{stringA} and \meta{stringB} are not equal after expansion
844 are accepted.
845 The implementation is done with \refCom{ifcsvnotstrcmp}.
846 \end{docCsvKey}
847
848
849 \begin{docCsvKey}[][doc new=2016-07-01]{filter expr}{=\meta{condition}}{no default}
850 Only data lines which fulfill a logical \meta{condition} are accepted.
851 For the \meta{condition}, every boolean expression
852 from the |etoolbox| package is feasible.
853 To preprocess the data line before testing the \meta{condition},
854 the option key \refKey{/csv/before filter} can be used.
855 \begin{dispExample}
856 \csvreader[head to column names,tabular=llll,
857 table head=\toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
858 table foot=\bottomrule,
859 %>> list only matriculation numbers greater than 20000
860 % and grade less than 4.0 <<
861 filter expr={ test{\ifnumgreater{\matriculation}{20000}}
862 and test{\ifdimless{\grade pt}{4.0pt}} },
863 ]{grade.csv}{}{%
864 \thecsvrow & \slshape\name, \givenname & \matriculation & \grade}
865 \end{dispExample}
866 \end{docCsvKey}
867
868 \clearpage
869 \begin{docCsvKey}[][doc new=2016-07-01]{filter ifthen}{=\meta{condition}}{no default}
870 Only data lines which fulfill a logical \meta{condition} are accepted.
871 For the \meta{condition}, every term from the |ifthen| package
872 is feasible.
873 To preprocess the data line before testing the \meta{condition},
874 the option key \refKey{/csv/before filter} can be used.
875
876 \begin{dispExample}
877 \csvreader[head to column names,tabular=llll,
878 table head=\toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
879 table foot=\bottomrule,
880 %>> list only female persons <<
881 filter ifthen=\equal{\gender}{f},
882 ]{grade.csv}{}{%
883 \thecsvrow & \slshape\name, \givenname & \matriculation & \grade}
884 \end{dispExample}
885
886 \end{docCsvKey}
887
888
889 \begin{docCsvKey}{filter}{=\meta{condition}}{no default}
890 Alias for \refKey{/csv/filter ifthen}.
891 \end{docCsvKey}
892
893 \begin{docCsvKey}{filter equal}{=\marg{stringA}\marg{stringB}}{style, no default}
894 Only lines where \meta{stringA} and \meta{stringB} are equal after expansion
895 are accepted.
896 The implementation is done with the |ifthen| package.
897 \end{docCsvKey}
898
899
900 \begin{docCsvKey}{filter not equal}{=\marg{stringA}\marg{stringB}}{style, no default}
901 Only lines where \meta{stringA} and \meta{stringB} are not equal after expansion
902 are accepted.
903 The implementation is done with the |ifthen| package.
904 \end{docCsvKey}
905
906
907
908 \begin{docCsvKey}{no filter}{}{no value, initially set}
909 Clears a set filter.
910 \end{docCsvKey}
911
912
913 \begin{docCsvKey}{filter accept all}{}{no value, initially set}
914 Alias for |no filter|. All consistent data lines are accepted.
915 \end{docCsvKey}
916
917
918 \begin{docCsvKey}{filter reject all}{}{no value}
919 All data line are ignored.
920 \end{docCsvKey}
921
922
923
924 \enlargethispage*{2cm}
925 \begin{docCsvKey}[][doc new=2016-07-01]{full filter}{=\meta{code}}{no default}
926 Technically, this key is an alias for \refKey{/csv/before filter}.
927 Philosophically, \refKey{/csv/before filter} computes something before
928 a filter condition is set, but \refKey{/csv/full filter} should implement
929 the full filtering. Especially, \refCom{csvfilteraccept} or
930 \refCom{csvfilterreject} \emph{should} be set inside the \meta{code}.
931 \begin{dispExample}
932 \csvreader[head to column names,tabular=llll,
933 table head=\toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
934 table foot=\bottomrule,
935 %>> list only matriculation numbers greater than 20000
936 % and grade less than 4.0 <<
937 full filter=\ifnumgreater{\matriculation}{20000}
938 {\ifdimless{\grade pt}{4.0pt}{\csvfilteraccept}{\csvfilterreject}}
939 {\csvfilterreject},
940 ]{grade.csv}{}{%
941 \thecsvrow & \slshape\name, \givenname & \matriculation & \grade}
942 \end{dispExample}
943 \end{docCsvKey}
944
945
946
947 %]]
948
949
950 \clearpage
951 \subsection{Table Support}\label{subsec:tabsupport}%--------%[[
952
953 \begin{docCsvKey}{tabular}{=\meta{table format}}{style, no default}
954 Surrounds the CSV processing with |\begin{tabular}|\marg{table format}
955 at begin and with |\end{tabular}| at end.
956 Additionally, the commands defined by the key values of
957 \refKey{/csv/before table}, \refKey{/csv/table head}, \refKey{/csv/table foot},
958 and \refKey{/csv/after table} are executed at the appropriate places.
959 \end{docCsvKey}
960
961
962 \begin{docCsvKey}{centered tabular}{=\meta{table format}}{style, no default}
963 Like \refKey{/csv/tabular} but inside an additional |center| environment.
964 \end{docCsvKey}
965
966
967 \begin{docCsvKey}{longtable}{=\meta{table format}}{style, no default}
968 Like \refKey{/csv/tabular} but for the |longtable| environment.
969 This requires the package |longtable| (not loaded automatically).
970 \end{docCsvKey}
971
972
973 \begin{docCsvKey}{tabbing}{}{style, no value}
974 Like \refKey{/csv/tabular} but for the |tabbing| environment.
975 \end{docCsvKey}
976
977
978 \begin{docCsvKey}{centered tabbing}{}{style, no value}
979 Like \refKey{/csv/tabbing} but inside an additional |center| environment.
980 \end{docCsvKey}
981
982
983 \begin{docCsvKey}{no table}{}{style, no value}
984 Deactivates |tabular|, |longtable|, and |tabbing|.
985 \end{docCsvKey}
986
987
988 \begin{docCsvKey}{before table}{=\meta{code}}{no default, initially empty}
989 Sets the \meta{code} to be executed before |\begin{tabular}| or before |\begin{longtable}|
990 or before |\begin{tabbing}|, respectively.
991 \end{docCsvKey}
992
993
994 \begin{docCsvKey}{table head}{=\meta{code}}{no default, initially empty}
995 Sets the \meta{code} to be executed after |\begin{tabular}| or after |\begin{longtable}|
996 or after |\begin{tabbing}|, respectively.
997 \end{docCsvKey}
998
999
1000 \begin{docCsvKey}{table foot}{=\meta{code}}{no default, initially empty}
1001 Sets the \meta{code} to be executed before |\end{tabular}| or before |\end{longtable}|
1002 or before |\end{tabbing}|, respectively.
1003 \end{docCsvKey}
1004
1005
1006 \begin{docCsvKey}{after table}{=\meta{code}}{no default, initially empty}
1007 Sets the \meta{code} to be executed after |\end{tabular}| or after |\end{longtable}|
1008 or after |\end{tabbing}|, respectively.
1009 \end{docCsvKey}
1010
1011 \bigskip
1012
1013 The following |auto| options are the counterparts for the respective quick
1014 overview commands like \refCom{csvautotabular}. They are listed for
1015 completeness, but are unlikely to be used directly.
1016
1017 \begin{docCsvKey}{autotabular}{=\meta{file name}}{no default}
1018 Reads the whole CSV file denoted \meta{file name} with an automated formatting.
1019 \end{docCsvKey}
1020
1021
1022 \begin{docCsvKey}{autolongtable}{=\meta{file name}}{no default}
1023 Reads the whole CSV file denoted \meta{file name} with an automated formatting
1024 using the required |longtable| package.
1025 \end{docCsvKey}
1026
1027
1028 \begin{docCsvKey}{autobooktabular}{=\meta{file name}}{no default}
1029 Reads the whole CSV file denoted \meta{file name} with an automated formatting
1030 using the required |booktabs| package.
1031 \end{docCsvKey}
1032
1033
1034 \begin{docCsvKey}{autobooklongtable}{=\meta{file name}}{no default}
1035 Reads the whole CSV file denoted \meta{file name} with an automated formatting
1036 using the required |booktabs| and |longtable| packages.
1037 \end{docCsvKey}
1038
1039
1040 \clearpage
1041 \subsection{Special Characters}\label{subsec:specchar}
1042 Be default, the CSV content is treated like normal \LaTeX\ text, see
1043 Subsection~\ref{macrocodexample} on page~\pageref{macrocodexample}.
1044 But, \TeX\ special characters of the CSV content may also be interpreted
1045 as normal characters, if one or more of the following options are used.
1046
1047 \begin{docCsvKey}{respect tab}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1048 If this key is set, every
1049 tabulator sign
1050 inside the CSV content is a normal character.
1051 \end{docCsvKey}
1052
1053 \begin{docCsvKey}{respect percent}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1054 If this key is set, every
1055 percent sign \verbbox{\%}
1056 inside the CSV content is a normal character.
1057 \end{docCsvKey}
1058
1059 \begin{docCsvKey}{respect sharp}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1060 If this key is set, every
1061 sharp sign \verbbox{\#}
1062 inside the CSV content is a normal character.
1063 \end{docCsvKey}
1064
1065 \begin{docCsvKey}{respect dollar}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1066 If this key is set, every
1067 dollar sign \verbbox{\$}
1068 inside the CSV content is a normal character.
1069 \end{docCsvKey}
1070
1071 \begin{docCsvKey}{respect and}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1072 If this key is set, every
1073 and sign \verbbox{\&}
1074 inside the CSV content is a normal character.
1075 \end{docCsvKey}
1076
1077 \begin{docCsvKey}{respect backslash}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1078 If this key is set, every
1079 backslash sign \verbbox{\textbackslash}
1080 inside the CSV content is a normal character.
1081 \end{docCsvKey}
1082
1083 \begin{docCsvKey}{respect underscore}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1084 If this key is set, every
1085 underscore sign \verbbox{\_}
1086 inside the CSV content is a normal character.
1087 \end{docCsvKey}
1088
1089 \begin{docCsvKey}{respect tilde}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1090 If this key is set, every
1091 tilde sign \verbbox{\textasciitilde}
1092 inside the CSV content is a normal character.
1093 \end{docCsvKey}
1094
1095 \begin{docCsvKey}{respect circumflex}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1096 If this key is set, every
1097 circumflex sign \verbbox{\textasciicircum}
1098 inside the CSV content is a normal character.
1099 \end{docCsvKey}
1100
1101 \begin{docCsvKey}{respect leftbrace}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1102 If this key is set, every
1103 left brace sign \verbbox{\textbraceleft}
1104 inside the CSV content is a normal character.
1105 \end{docCsvKey}
1106
1107 \begin{docCsvKey}{respect rightbrace}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1108 If this key is set, every
1109 right brace sign \verbbox{\textbraceright}
1110 inside the CSV content is a normal character.
1111 \end{docCsvKey}
1112
1113 \begin{docCsvKey}{respect all}{}{style, no value, initially unset}
1114 Set all special characters from above to normal characters. This means
1115 a quite verbatim interpretation of the CSV content.
1116 \end{docCsvKey}
1117
1118 \begin{docCsvKey}{respect none}{}{style, no value, initially set}
1119 Do not change any special character from above to normal character.
1120 \end{docCsvKey}
1121
1122 \clearpage
1123 \subsection{Separators}\label{sec:separators}%
1124 \begin{docCsvKey}{separator}{=\meta{sign}}{no default, initially |comma|}
1125 \catcode `|=12
1126 Sets the \meta{sign} which is treates as separator between the data values
1127 of a data line. Feasible values are:
1128 \begin{itemize}
1129 \item\docValue{comma}: This is the initial value with '\texttt{,}' as separator.
1130 \medskip
1131
1132 \item\docValue{semicolon}: Sets the separator to '\texttt{;}'.
1133 \begin{dispExample}
1134 % \usepackage{tcolorbox} for tcbverbatimwrite
1135 \begin{tcbverbatimwrite}{testsemi.csv}
1136 name;givenname;matriculation;gender;grade
1137 Maier;Hans;12345;m;1.0
1138 Huber;Anna;23456;f;2.3
1139 Weißbäck;Werner;34567;m;5.0
1140 \end{tcbverbatimwrite}
1141
1142 \csvautobooktabular[separator=semicolon]{testsemi.csv}
1143 \end{dispExample}
1144 \medskip
1145
1146 \item\docValue{pipe}: Sets the separator to '\texttt{|}'.
1147 \begin{dispExample}
1148 % \usepackage{tcolorbox} for tcbverbatimwrite
1149 \begin{tcbverbatimwrite}{pipe.csv}
1150 name|givenname|matriculation|gender|grade
1151 Maier|Hans|12345|m|1.0
1152 Huber|Anna|23456|f|2.3
1153 Weißbäck|Werner|34567|m|5.0
1154 \end{tcbverbatimwrite}
1155
1156 \csvautobooktabular[separator=pipe]{pipe.csv}
1157 \end{dispExample}
1158 \medskip
1159
1160 \item\docValue{tab}: Sets the separator to the tabulator sign.
1161 Automatically, \refKey{/csv/respect tab} is set also.
1162 \end{itemize}
1163 \end{docCsvKey}
1164
1165 \clearpage
1166 \subsection{Miscellaneous}%
1167
1168 \begin{docCsvKey}{every csv}{}{style, initially empty}
1169 A style definition which is used for every following CSV file.
1170 This definition can be overwritten with user code.
1171 \begin{dispListing}
1172 % Sets a warning message for unfeasible data lines.
1173 \csvset{every csv/.style={warn on column count error}}
1174 % Alternatively:
1175 \csvstyle{every csv}{warn on column count error}
1176 \end{dispListing}
1177 \end{docCsvKey}
1178
1179 \begin{docCsvKey}{default}{}{style}
1180 A style definition which is used for every following CSV file which
1181 resets all settings to default values\footnote{\texttt{default} is used
1182 because of the global nature of most settings.}.
1183 This key should not be used or changed by the user if there is not a
1184 really good reason (and you know what you do).
1185 \end{docCsvKey}
1186
1187
1188 \begin{docCsvKey}{file}{=\meta{file name}}{no default, initially |unknown.csv|}
1189 Sets the \meta{file name} of the CSV file to be processed.
1190 \end{docCsvKey}
1191
1192
1193 \begin{docCsvKey}{preprocessed file}{=\meta{file name}}{no default, initially \texttt{\textbackslash\detokenize{jobname_sorted.csv}}}
1194 Sets the \meta{file name} of the CSV file which is the output of a
1195 preprocessor.
1196 \end{docCsvKey}
1197
1198
1199 \begin{docCsvKey}{preprocessor}{=\meta{macro}}{no default}
1200 Defines a preprocessor for the given CSV file.
1201 The \meta{macro} has to have two mandatory arguments. The first argument
1202 is the original CSV file which is set by \refKey{/csv/file}.
1203 The second argument is the preprocessed CSV file
1204 which is set by \refKey{/csv/preprocessed file}.\par\smallskip
1205 Typically, the \meta{macro} may call an external program which preprocesses
1206 the original CSV file (e.\,g. sorting the file) and creates the
1207 preprocessed CSV file. The later file is used by \refCom{csvreader}
1208 or \refCom{csvloop}.
1209 \begin{dispListing}
1210 \newcommand{\mySortTool}[2]{%
1211 % call to an external program to sort file #1 with resulting file #2
1212 }
1213
1214 \csvreader[%
1215 preprocessed file=\jobname_sorted.csv,
1216 preprocessor=\mySortTool,
1217 ]{some.csv}{}{%
1218 % do something
1219 }
1220 \end{dispListing}
1221 See Subsection~\ref{sec:Sorting} on page~\pageref{sec:Sorting} for a
1222 concrete sorting preprocessing implemented with an external tool.
1223 \end{docCsvKey}
1224
1225
1226 \begin{docCsvKey}{no preprocessing}{}{style, no value, initially set}
1227 Clears any preprocessing, i.\,e. preprocessing is switched of.
1228 \end{docCsvKey}
1229
1230
1231
1232 \clearpage
1233 \subsection{Sorting}\label{sec:Sorting}%
1234 \TeX/\LaTeX\ was not born under a sorting planet. |csvsimple-legacy| provides no
1235 sorting of data lines by \LaTeX-methods since sorting can be done much faster
1236 and much better by external tools.
1237
1238 First, one should consider the appropriate \emph{place} for sorting:
1239 \begin{itemize}
1240 \item CSV files may be sorted by a tool \emph{before} the \LaTeX\ document is processed
1241 at all. If the CSV data is not likely to change, this is the most efficient method.
1242 \item CSV files may be sorted by a tool every time before the \LaTeX\ document is compiled.
1243 This could be automated by a shell script or some processing tool like |arara|.
1244 \item CSV files may be sorted on-the-fly by a tool during compilation of
1245 a \LaTeX\ document. This is the most elegant but not the most efficient way.
1246 \end{itemize}
1247
1248 The first two methods are decoupled from anything concerning |csvsimple-legacy|.
1249 For the third method, the \refKey{/csv/preprocessor} option is made for.
1250 This allows to access an external tool for sorting.
1251 \emph{Which tool} is your choice.
1252
1253 \csvsorter\ was written as a companion tool for |csvsimple|.
1254 It is an open source Java command-line tool for sorting CSV files, available at\\
1255 \url{http://T-F-S.github.io/csvsorter/}\quad or\quad
1256 \url{https://github.com/T-F-S/csvsorter}
1257
1258 It can be
1259 used for all three sorting approaches described above.
1260 There is special support for on-the-fly sorting with \csvsorter\ using the
1261 following options.
1262
1263 \begin{enumerate}\bfseries
1264 \item To use the sorting options, you have to install \csvsorter\ before!\\
1265 |csvsimple| v1.12 or newer needs \csvsorter\ v0.94 of newer!
1266 \item You have to give permission to call external tools during
1267 compilation, i.\,e.\ the command-line options for |latex| have to include
1268 |-shell-escape|.
1269 \end{enumerate}
1270
1271 \bigskip
1272
1273 \begin{docCsvKey}{csvsorter command}{=\meta{system command}}{no default, initially |csvsorter|}
1274 The \meta{system command} specifies the system call for \csvsorter\ (without the options).
1275 If \csvsorter\ was completely installed following its documentation, there is
1276 nothing to change here. If the |csvsorter.jar| file is inside the same
1277 directory as the \LaTeX\ source file, you may configure:% preferrably inside the preamble:
1278 \begin{dispListing}
1279 \csvset{csvsorter command=java -jar csvsorter.jar}
1280 \end{dispListing}
1281 \end{docCsvKey}
1282
1283 \begin{docCsvKey}{csvsorter configpath}{=\meta{path}}{no default, initially |.|}
1284 Sorting with \csvsorter\ is done using XML configuration files. If these files
1285 are not stored inside the same directory as the \LaTeX\ source file, a
1286 \meta{path} to access them can be configured:
1287 \begin{dispListing}
1288 \csvset{csvsorter configpath=xmlfiles}
1289 \end{dispListing}
1290 Here, the configuration files would be stored in a subdirectory named |xmlfiles|.
1291 \end{docCsvKey}
1292
1293 \begin{docCsvKey}{csvsorter log}{=\meta{file name}}{no default, initially |csvsorter.log|}
1294 Sets the log file of \csvsorter\ to the given \meta{file name}.
1295 \begin{dispListing}
1296 \csvset{csvsorter log=outdir/csvsorter.log}
1297 \end{dispListing}
1298 Here, the log file is written to a subdirectory named |outdir|.
1299 \end{docCsvKey}
1300
1301 \clearpage
1302 \begin{docCsvKey}{csvsorter token}{=\meta{file name}}{no default, initially |\textbackslash jobname.csvtoken|}
1303 Sets \meta{file name} as token file. This is an auxiliary file which
1304 communicates the success of \csvsorter\ to |csvsimple|.
1305 \begin{dispListing}
1306 \csvset{csvsorter log=outdir/\jobname.csvtoken}
1307 \end{dispListing}
1308 Here, the token file is written to a subdirectory named |outdir|.
1309 \end{docCsvKey}
1310
1311
1312 \begin{docCsvKey}{sort by}{=\meta{file name}}{style, initially unset}
1313 The \meta{file name} denotes an XML configuration file for \csvsorter.
1314 Setting this option inside \refCom{csvreader} or
1315 \refCom{csvloop} will issue a system call to \csvsorter.
1316 \begin{itemize}
1317 \item \csvsorter\ uses the given CSV file as input file.
1318 \item \csvsorter\ uses \meta{file name} as configuration file.
1319 \item The output CSV file is denoted by \refKey{/csv/preprocessed file}
1320 which is by default \texttt{\textbackslash\detokenize{jobname_sorted.csv}}.
1321 This output file is this actual file processed by \refCom{csvreader} or \refCom{csvloop}.
1322 \item \csvsorter\ also generates a log file denoted by \refKey{/csv/csvsorter log} which is by default |csvsorter.log|.
1323 \end{itemize}
1324
1325 \par\medskip\textbf{First example:}
1326 To sort our example |grade.csv| file according to |name| and |givenname|, we
1327 use the following XML configuration file. Since \csvsorter\ uses double quotes
1328 as default brackets for column values, we remove bracket recognition to avoid
1329 a clash with the escaped umlauts of the example CSV file.\par\smallskip
1330
1331 \xmllisting{namesort}
1332 \begin{dispExample}
1333 % \usepackage{booktabs}
1334 \csvreader[sort by=namesort.xml,
1335 head to column names,
1336 tabular=>{\color{red}}lllll,
1337 table head=\toprule Name & Given Name & Matriculation & Gender & Grade\\\midrule,
1338 table foot=\bottomrule]
1339 {grade.csv}{}{\csvlinetotablerow}
1340 \end{dispExample}
1341
1342 \clearpage\textbf{Second example:}
1343 To sort our example |grade.csv| file according to |grade|, we
1344 use the following XML configuration file. Further, persons with the same |grade|
1345 are sorted by |name| and |givenname|. Since \csvsorter\ uses double quotes
1346 as default brackets for column values, we remove bracket recognition to avoid
1347 a clash with the escaped umlauts of the example CSV file.\par\smallskip
1348
1349 \xmllisting{gradesort}
1350 \begin{dispExample}
1351 % \usepackage{booktabs}
1352 \csvreader[sort by=gradesort.xml,
1353 head to column names,
1354 tabular=llll>{\color{red}}l,
1355 table head=\toprule Name & Given Name & Matriculation & Gender & Grade\\\midrule,
1356 table foot=\bottomrule]
1357 {grade.csv}{}{\csvlinetotablerow}
1358 \end{dispExample}
1359
1360 \clearpage\textbf{Third example:}
1361 To generate a matriculation/grade list, we sort our example |grade.csv| file
1362 using the following XML configuration file.
1363 Again, since \csvsorter\ uses double quotes
1364 as default brackets for column values, we remove bracket recognition to avoid
1365 a clash with the escaped umlauts of the example CSV file.\par\smallskip
1366
1367 \xmllisting{matriculationsort}
1368 \begin{dispExample}
1369 % \usepackage{booktabs}
1370 \csvreader[sort by=matriculationsort.xml,
1371 head to column names,
1372 tabular=>{\color{red}}ll,
1373 table head=\toprule Matriculation & Grade\\\midrule,
1374 table foot=\bottomrule]
1375 {grade.csv}{}{\matriculation & \grade}
1376 \end{dispExample}
1377 \end{docCsvKey}
1378
1379
1380 \clearpage
1381 \begin{docCsvKey}{new sorting rule}{=\marg{name}\marg{file name}}{style, initially unset}
1382 This is a convenience option to generate a new shortcut for often used
1383 \refKey{/csv/sort by} applications. It also adds a more semantic touch.
1384 The new shortcut option is
1385 \tcbox[on line,size=small,colback=white,colframe=red]{|sort by| \meta{name}} which expands to
1386 \tcbox[on line,size=small,colback=white,colframe=red]{|sort by=|\marg{file name}}.\par\medskip
1387
1388 Consider the following example:
1389 \begin{dispExample}
1390 \csvautotabular[sort by=namesort.xml]{grade.csv}
1391 \end{dispExample}
1392 A good place for setting up a new sorting rule would be inside the preamble:
1393
1394 \csvset{new sorting rule={name}{namesort.xml}}
1395 \begin{dispListing}
1396 \csvset{new sorting rule={name}{namesort.xml}}
1397 \end{dispListing}
1398
1399 Now, we can use the new rule:
1400 \begin{dispExample}
1401 \csvautotabular[sort by name]{grade.csv}
1402 \end{dispExample}
1403
1404 \end{docCsvKey}
1405
1406
1407 \clearpage
1408 \section{String Tests}\label{sec:stringtests}%
1409
1410 The following string tests are complementing the string tests
1411 from the |etoolbox| package. They all do the same, i.e.,
1412 comparing expanded strings for equality.
1413 \begin{itemize}
1414 \item\refCom{ifcsvstrcmp} is the most efficient method, because it uses
1415 native compiler string comparison (if available).
1416 \item\refCom{ifcsvstrequal} does not rely on a compiler. It also is the
1417 fallback implementation for \refCom{ifcsvstrcmp}, if there is no
1418 native comparison method.
1419 \item\refCom{ifcsvprostrequal} is possibly more failsafe than the other two
1420 string tests. It may be used, if strings contain dirty things like |\textbf{A}|.
1421 \end{itemize}
1422 \medskip
1423
1424 \begin{docCommand}[doc new=2016-07-01]{ifcsvstrcmp}{\marg{stringA}\marg{stringB}\marg{true}\marg{false}}
1425 Compares two strings and executes \meta{true} if they are equal, and \meta{false} otherwise.
1426 The comparison is done using |\pdfstrcmp|, if compilation is done with pdf\LaTeX.
1427 The comparison is done using |\pdf@strcmp|, if the package |pdftexcmds| is
1428 loaded and compilation is done with lua\LaTeX\ or Xe\LaTeX.
1429 Otherwise, \refCom{ifcsvstrcmp} is identical to \refCom{ifcsvstrequal}.
1430 This command cannot be used inside the preamble.
1431 \end{docCommand}
1432
1433
1434 \begin{docCommand}[doc new=2016-07-01]{ifcsvnotstrcmp}{\marg{stringA}\marg{stringB}\marg{true}\marg{false}}
1435 Compares two strings and executes \meta{true} if they are \emph{not} equal, and \meta{false} otherwise.
1436 The implementation uses \refCom{ifcsvstrcmp}.
1437 \end{docCommand}
1438
1439
1440 \begin{docCommand}[doc new=2016-07-01]{ifcsvstrequal}{\marg{stringA}\marg{stringB}\marg{true}\marg{false}}
1441 Compares two strings and executes \meta{true} if they are equal, and \meta{false} otherwise.
1442 The strings are expanded with |\edef| in the test.
1443 \end{docCommand}
1444
1445 \begin{docCommand}[doc new=2016-07-01]{ifcsvprostrequal}{\marg{stringA}\marg{stringB}\marg{true}\marg{false}}
1446 Compares two strings and executes \meta{true} if they are equal, and \meta{false} otherwise.
1447 The strings are expanded with |\protected@edef| in the test, i.e. parts of the
1448 strings which are protected stay unexpanded.
1449 \end{docCommand}
1450
1451
1452
1453 \clearpage
1454 \section{Examples}%
1455
1456 \subsection{A Serial Letter}%
1457 In this example, a serial letter is to be written to all persons with
1458 addresses from the following CSV file. Deliberately, the file content is
1459 not given in very pretty format.
1460
1461 %-- file embedded for simplicity --
1462 \begin{tcbverbatimwrite}{address.csv}
1463 name,givenname,gender,degree,street,zip,location,bonus
1464 Maier,Hans,m,,Am Bachweg 17,10010,Hopfingen,20
1465 % next line with a comma in curly braces
1466 Huber,Erna,f,Dr.,{Moosstraße 32, Hinterschlag},10020,Örtingstetten,30
1467 Weißbäck,Werner,m,Prof. Dr.,Brauallee 10,10030,Klingenbach,40
1468 % this line is ignored %
1469 Siebener , Franz,m, , Blaumeisenweg 12 , 10040 , Pardauz , 50
1470 % preceding and trailing spaces in entries are removed %
1471 Schmitt,Anton,m,,{\AE{}lfred-Esplanade, T\ae{}g 37}, 10050,\OE{}resung,60
1472 \end{tcbverbatimwrite}
1473 %-- end embedded file --
1474
1475 \csvlisting{address}
1476
1477 Firstly, we survey the file content quickly using
1478 |\csvautotabular|.
1479 As can be seen, unfeasible lines are ignored automatically.
1480
1481 \begin{dispExample}
1482 \tiny\csvautotabular{address.csv}
1483 \end{dispExample}
1484
1485 Now, we create the serial letter where every feasible data line produces
1486 an own page. Here, we simulate the page by a |tcolorbox| (from the package
1487 |tcolorbox|).
1488 For the gender specific salutations, an auxiliary macro |\ifmale| is
1489 introduced.
1490
1491 \begin{dispExample}
1492 % this example requires the tcolorbox package
1493 \newcommand{\ifmale}[2]{\ifcsvstrcmp{\gender}{m}{#1}{#2}}
1494
1495 \csvreader[head to column names]{address.csv}{}{%
1496 \begin{tcolorbox}[colframe=DarkGray,colback=White,arc=0mm,width=(\linewidth-2pt)/2,
1497 equal height group=letter,before=,after=\hfill,fonttitle=\bfseries,
1498 adjusted title={Letter to \name}]
1499 \ifcsvstrcmp{\degree}{}{\ifmale{Mr.}{Ms.}}{\degree}~\givenname~\name\\
1500 \street\\\zip~\location
1501 \tcblower
1502 {\itshape Dear \ifmale{Sir}{Madam},}\\
1503 we are pleased to announce you a bonus value of \bonus\%{}
1504 which will be delivered to \location\ soon.\\\ldots
1505 \end{tcolorbox}}
1506 \end{dispExample}
1507
1508
1509
1510 \clearpage
1511 \subsection{A Graphical Presentation}%
1512 For this example, we use some artificial statistical data given by a CSV file.
1513
1514 %-- file embedded for simplicity --
1515 \begin{tcbverbatimwrite}{data.csv}
1516 land,group,amount
1517 Bayern,A,1700
1518 Baden-Württemberg,A,2300
1519 Sachsen,B,1520
1520 Thüringen,A,1900
1521 Hessen,B,2100
1522 \end{tcbverbatimwrite}
1523 %-- end embedded file --
1524
1525 \csvlisting{data}
1526
1527 Firstly, we survey the file content using
1528 |\csvautobooktabular|.
1529
1530 \begin{dispExample}
1531 % needs the booktabs package
1532 \csvautobooktabular{data.csv}
1533 \end{dispExample}
1534
1535 The amount values are presented in the following diagram by bars where
1536 the group classification is given using different colors.
1537
1538 \begin{dispExample}
1539 % This example requires the package tikz
1540 \begin{tikzpicture}[Group/A/.style={left color=red!10,right color=red!20},
1541 Group/B/.style={left color=blue!10,right color=blue!20}]
1542 \csvreader[head to column names]{data.csv}{}{%
1543 \begin{scope}[yshift=-\thecsvrow cm]
1544 \path [draw,Group/\group] (0,-0.45)
1545 rectangle node[font=\bfseries] {\amount} (\amount/1000,0.45);
1546 \node[left] at (0,0) {\land};
1547 \end{scope} }
1548 \end{tikzpicture}
1549 \end{dispExample}
1550
1551
1552 \clearpage
1553 It would be nice to sort the bars by length, i.\,e.\ to sort the CSV file
1554 by the |amount| column. If the \csvsorter\ program is properly installed,
1555 see Subsection~\ref{sec:Sorting} on page~\pageref{sec:Sorting},
1556 this can be done with the following configuration file for \csvsorter:
1557
1558 \xmllisting{amountsort}
1559
1560 Now, we just have to add an option |sort by=amountsort.xml|:
1561 \begin{dispExample}
1562 % This example requires the package tikz
1563 % Also, the CSV-Sorter tool has to be installed
1564 \begin{tikzpicture}[Group/A/.style={left color=red!10,right color=red!20},
1565 Group/B/.style={left color=blue!10,right color=blue!20}]
1566 \csvreader[head to column names,sort by=amountsort.xml]{data.csv}{}{%
1567 \begin{scope}[yshift=-\thecsvrow cm]
1568 \path [draw,Group/\group] (0,-0.45)
1569 rectangle node[font=\bfseries] {\amount} (\amount/1000,0.45);
1570 \node[left] at (0,0) {\land};
1571 \end{scope} }
1572 \end{tikzpicture}
1573 \end{dispExample}
1574
1575
1576
1577
1578 \clearpage
1579 Next, we create a pie chart by calling |\csvreader| twice.
1580 In the first step, the total sum of amounts is computed, and in the second
1581 step the slices are drawn.
1582
1583 \begin{dispExample}
1584 % Modified example from www.texample.net for pie charts
1585 % This example needs the packages tikz, xcolor, calc
1586 \definecolorseries{myseries}{rgb}{step}[rgb]{.95,.85,.55}{.17,.47,.37}
1587 \resetcolorseries{myseries}%
1588
1589 % a pie slice
1590 \newcommand{\slice}[4]{
1591 \pgfmathsetmacro{\midangle}{0.5*#1+0.5*#2}
1592 \begin{scope}
1593 \clip (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;
1594 \colorlet{SliceColor}{myseries!!+}%
1595 \fill[inner color=SliceColor!30,outer color=SliceColor!60] (0,0) circle (1cm);
1596 \end{scope}
1597 \draw[thick] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;
1598 \node[label=\midangle:#4] at (\midangle:1) {};
1599 \pgfmathsetmacro{\temp}{min((#2-#1-10)/110*(-0.3),0)}
1600 \pgfmathsetmacro{\innerpos}{max(\temp,-0.5) + 0.8}
1601 \node at (\midangle:\innerpos) {#3};
1602 }
1603
1604 % sum of amounts
1605 \csvreader[before reading=\def\mysum{0}]{data.csv}{amount=\amount}{%
1606 \pgfmathsetmacro{\mysum}{\mysum+\amount}%
1607 }
1608
1609 % drawing of the pie chart
1610 \begin{tikzpicture}[scale=3]%
1611 \def\mya{0}\def\myb{0}
1612 \csvreader[head to column names]{data.csv}{}{%
1613 \let\mya\myb
1614 \pgfmathsetmacro{\myb}{\myb+\amount}
1615 \slice{\mya/\mysum*360}{\myb/\mysum*360}{\amount}{\land}
1616 }
1617 \end{tikzpicture}%
1618 \end{dispExample}
1619
1620
1621 \clearpage
1622 Finally, the filter option is demonstrated by separating the groups A and B.
1623 Every item is piled upon the appropriate stack.
1624
1625 \begin{dispExample}
1626 \newcommand{\drawGroup}[2]{%
1627 \def\mya{0}\def\myb{0}
1628 \node[below=3mm] at (2.5,0) {\bfseries Group #1};
1629 \csvreader[head to column names,filter equal={\group}{#1}]{data.csv}{}{%
1630 \let\mya\myb
1631 \pgfmathsetmacro{\myb}{\myb+\amount}
1632 \path[draw,top color=#2!25,bottom color=#2!50]
1633 (0,\mya/1000) rectangle node{\land\ (\amount)} (5,\myb/1000);
1634 }}
1635
1636 \begin{tikzpicture}
1637 \fill[gray!75] (-1,0) rectangle (13,-0.1);
1638 \drawGroup{A}{red}
1639 \begin{scope}[xshift=7cm]
1640 \drawGroup{B}{blue}
1641 \end{scope}
1642 \end{tikzpicture}
1643
1644 \end{dispExample}
1645
1646
1647 \clearpage
1648 \subsection{Macro code inside the data}\label{macrocodexample}%
1649
1650 If needed, the data file may contain macro code. Note that the first character
1651 of a data line is not allowed to be the backslash '|\|'.
1652
1653 %-- file embedded for simplicity --
1654 \begin{tcbverbatimwrite}{macrodata.csv}
1655 type,description,content
1656 M,A nice \textbf{formula}, $\displaystyle \int\frac{1}{x} = \ln|x|+c$
1657 G,A \textcolor{red}{colored} ball, {\tikz \shadedraw [shading=ball] (0,0) circle (.5cm);}
1658 M,\textbf{Another} formula, $\displaystyle \lim\limits_{n\to\infty} \frac{1}{n}=0$
1659 \end{tcbverbatimwrite}
1660 %-- end embedded file --
1661
1662 \csvlisting{macrodata}
1663
1664 Firstly, we survey the file content using
1665 |\csvautobooktabular|.
1666
1667 \begin{dispExample}
1668 \csvautobooktabular{macrodata.csv}
1669 \end{dispExample}
1670
1671
1672 \begin{dispExample}
1673 \csvstyle{my enumerate}{head to column names,
1674 before reading=\begin{enumerate},after reading=\end{enumerate}}
1675
1676 \csvreader[my enumerate]{macrodata.csv}{}{%
1677 \item \description:\par\content}
1678
1679 \bigskip
1680 Now, formulas only:
1681 \csvreader[my enumerate,filter equal={\type}{M}]{macrodata.csv}{}{%
1682 \item \description:\qquad\content}
1683 \end{dispExample}
1684
1685 \clearpage
1686 \subsection{Tables with Number Formatting}\label{numberformatting}%
1687
1688 We consider a file with numerical data which should be pretty-printed.
1689
1690 %-- file embedded for simplicity --
1691 \begin{tcbverbatimwrite}{data_numbers.csv}
1692 month, dogs, cats
1693 January, 12.50,12.3e5
1694 February, 3.32, 8.7e3
1695 March, 43, 3.1e6
1696 April, 0.33, 21.2e4
1697 May, 5.12, 3.45e6
1698 June, 6.44, 6.66e6
1699 July, 123.2,7.3e7
1700 August, 12.3, 5.3e4
1701 September,2.3, 4.4e4
1702 October, 6.5, 6.5e6
1703 November, 0.55, 5.5e5
1704 December, 2.2, 3.3e3
1705 \end{tcbverbatimwrite}
1706
1707 \csvlisting{data_numbers}
1708
1709 The |siunitx| package provides a new column type |S|
1710 which can align material using a number of different strategies.
1711 The following example demonstrates the application with CSV reading.
1712 The package documentation of |siunitx| contains a huge amount
1713 of formatting options.
1714
1715 \begin{dispExample}
1716 % \usepackage{siunitx,array,booktabs}
1717 \csvloop{
1718 file=data_numbers.csv,
1719 head to column names,
1720 before reading=\centering\sisetup{table-number-alignment=center},
1721 tabular={lSS[table-format=2.2e1]@{}c},
1722 table head=\toprule\textbf{Month} & \textbf{Dogs} & \textbf{Cats} &\\\midrule,
1723 command=\month & \dogs & \cats &,
1724 table foot=\bottomrule}
1725 \end{dispExample}
1726
1727 \clearpage
1728 Special care is needed, if the \emph{first} or the \emph{last} column is to be formatted with
1729 the column type |S|. The number detection of |siunitx| is disturbed by
1730 the line reading code of |csvsimple-legacy| which actually is present at the
1731 first and last column. To avoid this problem, the content of the first and last column
1732 could be formatted not by the table format definition, but by using a
1733 suitable |\tablenum| formatting directly, see |siunitx|.
1734
1735 Another and very nifty workaround suggested by Enrico Gregorio is to
1736 add an invisible dummy column with |c@{}| as first column
1737 and |@{}c| as last column:
1738
1739
1740 \begin{dispExample}
1741 % \usepackage{siunitx,array,booktabs}
1742 \csvloop{
1743 file=data_numbers.csv,
1744 head to column names,
1745 before reading=\centering\sisetup{table-number-alignment=center},
1746 tabular={c@{}S[table-format=2.2e1]S@{}c},
1747 table head= & \textbf{Cats} & \textbf{Dogs} & \\\midrule,
1748 command= & \cats & \dogs &,
1749 table foot=\bottomrule}
1750 \end{dispExample}
1751
1752
1753 \clearpage
1754 Now, the preceding table shall be sorted by the \emph{cats} values.
1755 If the \csvsorter\ program is properly installed,
1756 see Subsection~\ref{sec:Sorting} on page~\pageref{sec:Sorting},
1757 this can be done with the following configuration file for \csvsorter:
1758
1759 \xmllisting{catsort}
1760
1761 Now, we just have to add an option |sort by=catsort.xml|:
1762 \begin{dispExample}
1763 % \usepackage{siunitx,array,booktabs}
1764 % Also, the CSV-Sorter tool has to be installed
1765 \csvloop{
1766 file=data_numbers.csv,
1767 sort by=catsort.xml,
1768 head to column names,
1769 before reading=\centering\sisetup{table-number-alignment=center},
1770 tabular={lSS[table-format=2.2e1]@{}c},
1771 table head=\toprule\textbf{Month} & \textbf{Dogs} & \textbf{Cats} & \\\midrule,
1772 command=\month & \dogs & \cats &,
1773 table foot=\bottomrule}
1774 \end{dispExample}
1775
1776
1777 \clearpage
1778 \subsection{CSV data without header line}\label{noheader}%
1779 CSV files with a header line are more semantic than files without header,
1780 but it's no problem to work with headless files.
1781
1782 For this example, we use again some artificial statistical data given by a CSV file
1783 but this time without header.
1784
1785 %-- file embedded for simplicity --
1786 \begin{tcbverbatimwrite}{data_headless.csv}
1787 Bayern,A,1700
1788 Baden-Württemberg,A,2300
1789 Sachsen,B,1520
1790 Thüringen,A,1900
1791 Hessen,B,2100
1792 \end{tcbverbatimwrite}
1793 %-- end embedded file --
1794
1795 \csvlisting{data_headless}
1796
1797 Note that you cannot use the \refKey{/csv/no head} option for the auto tabular
1798 commands. If no options are given, the first line is interpreted as header line
1799 which gives an unpleasant result:
1800
1801 \begin{dispExample}
1802 \csvautobooktabular{data_headless.csv}
1803 \end{dispExample}
1804
1805 To get the expected result, one can redefine \refKey{/csv/table head}
1806 using \refCom{csvlinetotablerow} which holds the first line data for the
1807 |\csvauto...| commands:
1808
1809 \begin{dispExample}
1810 \csvautobooktabular[table head=\toprule\csvlinetotablerow\\]{data_headless.csv}
1811 \end{dispExample}
1812
1813 This example can be extended to insert a table head for this headless data:
1814
1815 \begin{dispExample}
1816 \csvautobooktabular[table head=\toprule\bfseries Land & \bfseries Group
1817 & \bfseries Amount\\\midrule\csvlinetotablerow\\]{data_headless.csv}
1818 \end{dispExample}
1819
1820 \clearpage
1821
1822 For the normal \refCom{csvreader} command, the \refKey{/csv/no head} option
1823 should be applied. Of course, we cannot use \refKey{/csv/head to column names}
1824 because there is no head, but the columns can be addressed by their numbers:
1825
1826 \begin{dispExample}
1827 \csvreader[no head,
1828 tabular=lr,
1829 table head=\toprule\bfseries Land & \bfseries Amount\\\midrule,
1830 table foot=\bottomrule]
1831 {data_headless.csv}
1832 {1=\land,3=\amount}
1833 {\land & \amount}
1834 \end{dispExample}
1835
1836
1837 \clearpage
1838 \subsection{Imported CSV data}\label{sec:importeddata}%
1839 If data is imported from other applications, there is not always a choice
1840 to format in comma separated values with curly brackets.
1841
1842 Consider the following example data file:
1843
1844 %-- file embedded for simplicity --
1845 \begin{tcbverbatimwrite}{imported.csv}
1846 "name";"address";"email"
1847 "Frank Smith";"Yellow Road 123, Brimblsby";"[email protected]"
1848 "Mary May";"Blue Alley 2a, London";"[email protected]"
1849 "Hans Meier";"Hauptstraße 32, Berlin";"[email protected]"
1850 \end{tcbverbatimwrite}
1851 %-- end embedded file --
1852
1853 \csvlisting{imported}
1854
1855 If the \csvsorter\ program is properly installed,
1856 see Subsection~\ref{sec:Sorting} on page~\pageref{sec:Sorting},
1857 this can be transformed on-the-fly
1858 with the following configuration file for \csvsorter:
1859
1860 \xmllisting{transform}
1861
1862 Now, we just have to add an option |sort by=transform.xml| to transform
1863 the input data. Here, we actually do not sort.
1864
1865 \begin{dispExample}
1866 % \usepackage{booktabs,array}
1867 % Also, the CSV-Sorter tool has to be installed
1868 \newcommand{\Header}[1]{\normalfont\bfseries #1}
1869
1870 \csvreader[
1871 sort by=transform.xml,
1872 tabular=>{\itshape}ll>{\ttfamily}l,
1873 table head=\toprule\Header{Name} & \Header{Address} & \Header{email}\\\midrule,
1874 table foot=\bottomrule]
1875 {imported.csv}{}{\csvlinetotablerow}
1876 \end{dispExample}
1877
1878 The file which is generated on-the-fly and which is actually read by
1879 |csvsimple-legacy| is the following:
1880
1881 \tcbinputlisting{docexample,listing style=tcbdocumentation,fonttitle=\bfseries,
1882 listing only,listing file=\jobname_sorted._csv}
1883
1884
1885 \clearpage
1886 \subsection{Encoding}\label{encoding}%
1887 If the CSV file has a different encoding than the \LaTeX\ source file,
1888 then special care is needed.
1889
1890 \begin{itemize}
1891 \item The most obvious treatment is to change the encoding of the CSV file
1892 or the \LaTeX\ source file to match the other one (every good editor
1893 supports such a conversion). This is the easiest choice, if there a no
1894 good reasons against such a step. E.g., unfortunately, several tools
1895 under Windows need the CSV file to be |cp1252| encoded while
1896 the \LaTeX\ source file may need to be |utf8| encoded.
1897
1898 \item The |inputenc| package allows to switch the encoding inside the
1899 document, say from |utf8| to |cp1252|. Just be aware that you should only
1900 use pure ASCII for additional texts inside the switched region.
1901 \begin{dispListing}
1902 % !TeX encoding=UTF-8
1903 % ....
1904 \usepackage[utf8]{inputenc}
1905 % ....
1906 \begin{document}
1907 % ....
1908 \inputencoding{latin1}% only use ASCII from here, e.g. "Uberschrift
1909 \csvreader[%...
1910 ]{data_cp1252.csv}{%...
1911 }{% ....
1912 }
1913 \inputencoding{utf8}
1914 % ....
1915 \end{document}
1916 \end{dispListing}
1917
1918 \item As a variant to the last method, the encoding switch can be done
1919 using options from |csvsimple-legacy|:
1920 \begin{dispListing}
1921 % !TeX encoding=UTF-8
1922 % ....
1923 \usepackage[utf8]{inputenc}
1924 % ....
1925 \begin{document}
1926 % ....
1927 % only use ASCII from here, e.g. "Uberschrift
1928 \csvreader[%...
1929 before reading=\inputencoding{latin1},
1930 after reading=\inputencoding{utf8},
1931 ]{data_cp1252.csv}{%...
1932 }{% ....
1933 }
1934 % ....
1935 \end{document}
1936 \end{dispListing}
1937
1938 \pagebreak\item
1939 If the \csvsorter\ program is properly installed,
1940 see Subsection~\ref{sec:Sorting} on page~\pageref{sec:Sorting},
1941 the CSV file can be re-encoded on-the-fly
1942 with the following configuration file for \csvsorter:
1943
1944 \xmllisting{encoding}
1945
1946 \begin{dispListing}
1947 % !TeX encoding=UTF-8
1948 % ....
1949 \usepackage[utf8]{inputenc}
1950 % ....
1951 \begin{document}
1952 % ....
1953 \csvreader[%...
1954 sort by=encoding.xml,
1955 ]{data_cp1252.csv}{%...
1956 }{% ....
1957 }
1958 % ....
1959 \end{document}
1960 \end{dispListing}
1961
1962
1963 \end{itemize}
1964
1965
1966 \clearpage
1967
1968 \printindex
1969
1970 \end{document}