Debian Codegraph @

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

raw
1 % \LaTeX-Main\
2 % !TeX encoding=UTF-8
3 % !TeX spellcheck=en_US
4 %% The LaTeX package csvsimple - version 2.7.1 (2026/04/27)
5 %% csvsimple.tex: Manual
6 %%
7 %% -------------------------------------------------------------------------------------------
8 %% Copyright (c) 2008-2026 by Prof. Dr. Dr. Thomas F. Sturm <thomas dot sturm at unibw dot de>
9 %% -------------------------------------------------------------------------------------------
10 %%
11 %% This work may be distributed and/or modified under the
12 %% conditions of the LaTeX Project Public License, either version 1.3
13 %% of this license or (at your option) any later version.
14 %% The latest version of this license is in
15 %% http://www.latex-project.org/lppl.txt
16 %% and version 1.3 or later is part of all distributions of LaTeX
17 %% version 2005/12/01 or later.
18 %%
19 %% This work has the LPPL maintenance status `author-maintained'.
20 %%
21 %% This work consists of all files listed in README.md
22 %%
23 % \RequirePackage[check-declarations,enable-debug]{expl3}
24 \documentclass[a4paper,11pt]{ltxdoc}
25 \usepackage{csvsimple-doc}
26
27 \usepackage{\csvpkgprefix csvsimple-l3}
28
29 \tcbmakedocSubKey[doc key prefix=]{docCsvKey}{csvsim}
30 \tcbmakedocSubKeys[doc key prefix=]{docCsvKeys}{csvsim}
31
32 \hypersetup{
33 pdftitle={Manual for the csvsimple-l3 package},
34 pdfauthor={Thomas F. Sturm},
35 pdfsubject={csv file processing with LaTeX3},
36 pdfkeywords={csv file, comma-separated values, key–value syntax}
37 }
38
39 \usepackage{incgraph}
40
41 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 \begin{document}
43
44
45 \begin{center}
46 \begin{tcolorbox}[enhanced,hbox,tikznode,left=8mm,right=8mm,boxrule=0.4pt,
47 colback=white,colframe=black!50!yellow,
48 drop lifted shadow=black!50!yellow,arc is angular,
49 before=\par\vspace*{5mm},after=\par\bigskip]
50 {\bfseries\LARGE The \texttt{csvsimple-l3} package}\\[3mm]
51 {\large Manual for version \version\ (\datum)}
52 \end{tcolorbox}
53 {\large Thomas F.~Sturm%
54 \footnote{Prof.~Dr.~Dr.~Thomas F.~Sturm, Institut f\"{u}r Mathematik und Informatik,
55 University of the Bundeswehr Munich, D-85577 Neubiberg, Germany;
56 email: \href{mailto:[email protected]}{[email protected]}}\par\medskip
57 \normalsize\url{https://www.ctan.org/pkg/csvsimple}\par
58 \url{https://github.com/T-F-S/csvsimple}
59 }
60 \end{center}
61 \bigskip
62 \begin{absquote}
63 \begin{center}\bfseries Abstract\end{center}
64 |csvsimple(-l3)| provides a simple \LaTeX\ interface for processing files with
65 comma-separated values (CSV).
66 |csvsimple(-l3)| makes extensive use of key–value syntax, resulting in a straightforward interface.
67
68 Filtering and table generation are particularly supported. As the package
69 is intended to be lightweight, it does not provide support for data sorting or
70 database storage.
71 \end{absquote}
72
73 \vspace{1cm}
74
75 \includegraphics[width=\linewidth]{csvsimple-title.png}
76 % Source code for the title picture - omitted for PDF viewer compatibility
77 \begin{tcolorbox}[void]
78 \begin{NoHyper}
79 \begin{inctext}[]
80 \begin{tikzpicture}
81 \fill[top color=blue!50!gray!50,bottom color=red!50!gray!50] (-8,-5) rectangle (8,5);
82 \node at (0,2.5) {\tcbinputlisting{listing file=csvsimple-example.csv,listing only,width=11cm,blankest,colupper=blue!50!black}};
83 \node[red!50!black] at (0,-2.5) {\csvautotabular{csvsimple-example.csv}};
84 \begin{scope}[transparency group=knockout]
85 \fill [top color=blue!50!gray!10,bottom color=red!50!gray!10] (-7.7,-4.7) rectangle (7.7,4.7);
86 \node at (0,2.5) {\tcbinputlisting{listing file=csvsimple-example.csv,listing only,width=11cm,blankest,colupper=blue!20}};
87 \node[red!20] at (0,-2.5) {\csvautotabular{csvsimple-example.csv}};
88 \node at (0,2.5) [opacity=0,font=\fontencoding{T1}\fontfamily{lmr}\fontsize{7cm}{7cm}\bfseries] {csv};
89 \node at (0,-2.5) [opacity=0,font=\fontencoding{T1}\fontfamily{lmr}\fontsize{4.8cm}{4.8cm}\bfseries] {simple};
90 \end{scope}
91 \end{tikzpicture}
92 \end{inctext}
93 \end{NoHyper}
94 \end{tcolorbox}
95
96
97 \clearpage
98 \tableofcontents
99
100 \clearpage
101 \section{Introduction}%
102 The |csvsimple-l3| package is applied to the processing of
103 CSV\footnote{CSV file: file with comma-separated values.} files.
104 This processing is controlled by key–value assignments according to the
105 syntax of |l3keys|. Sample applications of the package
106 are tabular lists, serial letters, and charts.
107
108 An alternative to |csvsimple-l3| is the \refPkg{datatool} package
109 which provides considerably more functions and allows sorting of data by \LaTeX.
110 |csvsimple-l3| has a different approach for the user interface and
111 is deliberately restricted to some basic functions with fast
112 processing speed.
113
114 Mind the following restrictions:
115 \begin{itemize}
116 \item Sorting is not supported directly but can be done
117 with external tools, see \Fullref{sec:Sorting}.
118 \item Values are expected to be comma-separated, but the package
119 provides support for other separators, see \Fullref{sec:separators}.
120 \item Values are expected to be either not quoted or quoted with
121 curly braces |{}| of \TeX\ groups. Other quotes like double-quotes
122 are not supported directly, but can be achieved
123 with external tools, see \Fullref{sec:importeddata}.
124 For approximate patching see \Fullref{sec:hooks}.
125 \item Every data line is expected to contain the same amount of values.
126 Unfeasible data lines are silently ignored by default, but this can
127 be configured, see \Fullref{sec:consistency}.
128 \end{itemize}
129
130
131 \subsection{Loading the Package}
132 |csvsimple-l3| is loaded with \emph{one} of the following
133 alternatives inside the preamble:
134 \begin{dispListing}
135 \usepackage[l3]{csvsimple}
136 % or alternatively (not simultaneously!)
137 \usepackage{csvsimple-l3}
138 \end{dispListing}
139
140 Not automatically loaded, but used for many examples are the packages
141 \refPkg{longtable}, \refPkg{tabularray}, \refPkg{booktabs}, \refPkg{ifthen}, and \refPkg{etoolbox}.
142
143
144 \clearpage
145 \subsection{First Steps}
146 Every line of a processable CSV file has to contain an identical amount of
147 comma\footnote{See \refKey{csvsim/separator} for other separators than comma.} separated values. The curly braces |{}| of \TeX\ groups can be used
148 to mask a block which may contain commas not to be processed as separators.
149
150 The first line of such a CSV file is usually but not necessarily a header line
151 which contains the identifiers for each column.
152
153 %-- file embedded for simplicity --
154 \begin{tcbverbatimwrite}{grade.csv}
155 name,givenname,matriculation,gender,grade
156 Maier,Hans,12345,m,1.0
157 Huber,Anna,23456,f,2.3
158 Weißbäck,Werner,34567,m,5.0
159 Bauer,Maria,19202,f,3.3
160 \end{tcbverbatimwrite}
161 %-- end embedded file --
162
163 \csvlisting{grade}
164
165 \smallskip
166 The most simple way to display a CSV file in tabular form is the processing
167 with the \refCom{csvautotabular} command.
168
169 \begin{dispExample}
170 \csvautotabular{grade.csv}
171 \end{dispExample}
172
173
174 Typically, one would use \refCom{csvreader} instead of |\csvautotabular| to
175 gain full control over the interpretation of the included data.
176
177 In the following example, the entries of the header line are automatically
178 assigned to \TeX\ macros which may be used deliberately.
179
180
181 \begin{dispExample}
182 \begin{tabular}{|l|c|}\hline%
183 \bfseries Person & \bfseries Matr.~No.
184 \csvreader[
185 head to column names
186 ]{grade.csv}{}{%
187 \\\givenname\ \name & \matriculation
188 }%
189 \\\hline
190 \end{tabular}
191 \end{dispExample}
192
193
194 \clearpage
195 |\csvreader| is controlled by a plenty of options. For example, for table
196 applications line breaks are easily inserted by
197 \refKey{csvsim/late after line}. This defines a macro execution just before
198 the following line.
199 Additionally, the assignment of columns to \TeX\ macros is shown in a non automated
200 way.
201
202 \begin{dispExample}
203 \begin{tabular}{|r|l|c|}\hline%
204 & Person & Matr.~No.\\\hline\hline
205 \csvreader[
206 late after line = \\\hline
207 ]{grade.csv}%
208 {name=\name, givenname=\firstname, matriculation=\matnumber}{%
209 \thecsvrow & \firstname~\name & \matnumber
210 }%
211 \end{tabular}
212 \end{dispExample}
213
214 \smallskip
215 An even more comfortable and preferable way to create a table is setting
216 appropriate option keys. Note, that this gives you the possibility to create a
217 meta key (called style here) which contains the whole table creation
218 using \refCom{csvstyle} or |\keys_define:nn| from |l3keys|.
219
220 \begin{dispExample}
221 \csvreader[
222 tabular = |r|l|c|,
223 table head = \hline & Person & Matr.~No.\\\hline\hline,
224 late after line = \\\hline
225 ]{grade.csv}
226 {name=\name, givenname=\firstname, matriculation=\matnumber}{%
227 \thecsvrow & \firstname~\name & \matnumber
228 }%
229 \end{dispExample}
230
231
232 \clearpage
233 The next example shows such a style definition with the convenience macro
234 \refCom{csvstyle}. Here, we see again the automated assignment of header
235 entries to column names by \refKey{csvsim/head to column names}.
236 For this, the header entries have to be without spaces and special characters.
237 But you can always assign entries to canonical macro names manually like in the examples
238 above. Here, we also add a \refKey{csvsim/head to column names prefix} to avoid
239 macro name clashes.
240
241 \begin{dispExample}
242 \csvstyle{myTableStyle}{
243 tabular = |r|l|c|,
244 table head = \hline & Person & Matr.~No.\\\hline\hline,
245 late after line = \\\hline,
246 head to column names,
247 head to column names prefix = MY,
248 }
249
250 \csvreader[myTableStyle]
251 {grade.csv}{}{%
252 \thecsvrow & \MYgivenname~\MYname & \MYmatriculation
253 }
254 \end{dispExample}
255
256
257 \smallskip
258 Another way to address columns is to use their roman numbers.
259 The direct addressing is done by |\csvcoli|, |\csvcolii|, |\csvcoliii|, \ldots:
260
261 \begin{dispExample}
262 \csvreader[
263 tabular = |r|l|c|,
264 table head = \hline & Person & Matr.~No.\\\hline\hline,
265 late after line = \\\hline
266 ]{grade.csv}{}{%
267 \thecsvrow & \csvcolii~\csvcoli & \csvcoliii
268 }
269 \end{dispExample}
270
271 \smallskip
272 And yet another method to assign macros to columns is to use arabic numbers
273 for the assignment:
274
275 \begin{dispExample}
276 \csvreader[
277 tabular = |r|l|c|,
278 table head = \hline & Person & Matr.~No.\\\hline\hline,
279 late after line = \\\hline]%
280 {grade.csv}
281 {1=\name, 2=\firstname, 3=\matnumber}{%
282 \thecsvrow & \firstname~\name & \matnumber
283 }
284 \end{dispExample}
285
286 \smallskip
287 For recurring applications, the |l3keys| syntax allows to create own meta options
288 (styles) for a consistent and centralized design. The following example is easily
289 modified to obtain more or less option settings.
290
291 \begin{dispExample}
292 \csvstyle{myStudentList}{%
293 tabular = |r|l|c|,
294 table head = \hline & Person & #1\\\hline\hline,
295 late after line = \\\hline,
296 column names = {name=\name, givenname=\firstname}
297 }
298
299 \csvreader[ myStudentList={Matr.~No.} ]
300 {grade.csv}
301 {matriculation=\matnumber}{%
302 \thecsvrow & \firstname~\name & \matnumber
303 }%
304 \hfill%
305 \csvreader[ myStudentList={Grade} ]
306 {grade.csv}
307 {grade=\grade}{%
308 \thecsvrow & \firstname~\name & \grade
309 }
310 \end{dispExample}
311
312
313 \clearpage
314 Alternatively, column names can be set by \refCom{csvnames}
315 and style definitions by \refCom{csvstyle}.
316 With this, the last example is rewritten as follows:
317
318 \begin{dispExample}
319 \csvnames{myNames}{1=\name,2=\firstname,3=\matnumber,5=\grade}
320 \csvstyle{myStudentList}{
321 tabular = |r|l|c|,
322 table head = \hline & Person & #1\\\hline\hline,
323 late after line = \\\hline,
324 myNames
325 }
326
327 \csvreader[ myStudentList={Matr.~No.} ]
328 {grade.csv}{}{%
329 \thecsvrow & \firstname~\name & \matnumber
330 }%
331 \hfill%
332 \csvreader[ myStudentList={Grade} ]
333 {grade.csv}{}{%
334 \thecsvrow & \firstname~\name & \grade
335 }
336 \end{dispExample}
337
338 \smallskip
339 The data lines of a CSV file can also be filtered. In the following example,
340 a certificate is printed only for students with grade unequal to 5.0.
341
342 \begin{dispExample}
343 \csvreader[
344 filter not strcmp={\grade}{5.0}
345 ]{grade.csv}
346 {1=\name,2=\firstname,3=\matnumber,4=\gender,5=\grade}{%
347 \begin{center}\Large\bfseries Certificate in Mathematics\end{center}
348 \large\IfCsvsimStrEqualTF{\gender}{f}{Ms.}{Mr.}
349 \firstname~\name, matriculation number \matnumber, has passed the test
350 in mathematics with grade \grade.\par\ldots\par
351 }%
352 \end{dispExample}
353
354
355 \clearpage
356 \section{Macros for the Processing of CSV Files}\label{sec:makros}%
357
358 \begin{docCommand}{csvreader}{\oarg{options}\marg{file name}\marg{assignments}\marg{command list}}
359 \refCom{csvreader} reads the file denoted by \meta{file name} line by line.
360 Every line of the file has to contain an identical amount of
361 comma-separated values. The curly braces |{}| of \TeX\ groups can be used
362 to mask a block which may contain commas not to be processed as separators.\smallskip
363
364 The first line of such a CSV file is by default but not necessarily
365 processed as a header line which contains the identifiers for each column.
366 The entries of this line can be used to give \meta{assignments} to \TeX\ macros
367 to address the columns. The number of entries of this first line
368 determines the accepted number of entries for all following lines.
369 Every line which contains a higher or lower number of entries is ignored
370 during standard processing.\smallskip
371
372 The \meta{assignments} are given as comma-separated list of key–value pairs
373 \mbox{\meta{name}|=|\meta{macro}}. Here, \meta{name} is an entry from the
374 header line \emph{or} the arabic number of the addressed column.
375 \meta{macro} is some \TeX\ macro which gets the content of the addressed column.\smallskip
376
377 The \meta{command list} is executed for every accepted data line. Inside the
378 \meta{command list} is applicable:
379 \begin{itemize}
380 \item \docAuxCommand{thecsvrow} or the counter |csvrow| which contains the number of the
381 current data line (starting with 1).
382 \item \docAuxCommand{csvcoli}, \docAuxCommand{csvcolii}, \docAuxCommand{csvcoliii}, \ldots,
383 which contain the contents of the column entries of the current data line.
384 Alternatively can be used:
385 \item \meta{macro} from the \meta{assignments} to have a logical
386 addressing of a column entry.
387 \end{itemize}
388 Note, that the \meta{command list} is allowed to contain |\par| and
389 that \textbf{all macro definitions are made global} to be used for table applications.\smallskip
390
391 The processing of the given CSV file can be controlled by various
392 \meta{options} given as key–value list. The feasible option keys
393 are described in section \ref{sec:schluessel} from page \pageref{sec:schluessel}.
394
395 \begin{dispExample}
396 \csvreader[
397 tabular = |r|l|l|,
398 table head = \hline,
399 table foot = \hline
400 ]{grade.csv}%
401 {name=\name, givenname=\firstname, grade=\grade}{%
402 \grade & \firstname~\name & \csvcoliii
403 }
404 \end{dispExample}
405
406 Mainly, the |\csvreader| command consists of a \refCom{csvloop} macro with
407 following parameters:\par
408 |\csvloop{|\meta{options}|, file=|\meta{file name}|, column names=|\meta{assignments}|,|\\
409 \hspace*{2cm} |command=|\meta{command list}|}|\par
410 Therefore, the application of the keys \refKey{csvsim/file} and \refKey{csvsim/command}
411 is useless for |\csvreader|.
412 \end{docCommand}
413
414
415 \clearpage
416 \begin{docCommand}{csvloop}{\marg{options}}
417 Usually, \refCom{csvreader} may be preferred instead of |\csvloop|.
418 \refCom{csvreader} is based on |\csvloop| which takes a mandatory list of
419 \meta{options} in key–value syntax.
420 This list of \meta{options} controls the total processing. Especially,
421 it has to contain the CSV file name.
422 \begin{dispExample}
423 \csvloop{
424 file = {grade.csv},
425 head to column names,
426 command = \name,
427 before reading = {List of students:\ },
428 late after line = {{,}\ },
429 late after last line = .
430 }
431 \end{dispExample}
432 \end{docCommand}
433
434
435 \begin{docCommand}[doc updated = 2021-06-25]{csvset}{\marg{options}}
436 Sets \meta{options} for every following
437 \refCom{csvreader} and \refCom{csvloop}.
438 Note that most options are set to default values at the begin of these
439 commands and therefore cannot be defined reasonable by \refCom{csvset}.
440 But it may be used for options like \refKey{csvsim/csvsorter command}
441 to give global settings. Also see \refKey{csvsim/every csv}.
442 \end{docCommand}
443
444
445 \begin{docCommand}{csvstyle}{\marg{key}\marg{options}}
446 Defines a new |l3keys| meta key to call other keys. It is used to
447 make abbreviations for convenient key set applications.
448 The new \meta{key} can take one parameter. The name \refCom{csvstyle}
449 originates from an old version of |csvsimple| which used |pgfkeys|
450 instead of |l3keys|.
451
452 \begin{dispExample}
453 \csvstyle{grade list}{
454 column names = {name=\name, givenname=\firstname, grade=\grade}
455 }
456 \csvstyle{passed}{
457 filter not strcmp = {\grade}{5.0}
458 }
459 The following students passed the test in mathematics:\\
460 \csvreader[grade list,passed]{grade.csv}{}{
461 \firstname\ \name\ (\grade);
462 }
463 \end{dispExample}
464 \end{docCommand}
465
466 \enlargethispage*{1cm}
467
468 \begin{docCommand}{csvnames}{\marg{key}\marg{assignments}}
469 Abbreviation for |\csvstyle{|\meta{key}|}{column names=|\marg{assignments}|}|
470 to define additional \meta{assignments} of macros to columns.
471 \begin{dispExample}
472 \csvnames{grade list}{
473 name=\name, givenname=\firstname, grade=\grade
474 }
475 \csvstyle{passed}{
476 filter not strcmp = {\grade}{5.0}
477 }
478 The following students passed the test in mathematics:\\
479 \csvreader[grade list,passed]{grade.csv}{}{
480 \firstname\ \name\ (\grade);
481 }
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
500 \begin{docCommand}[doc updated=2021-06-28]{ifcsvoddrow}{\marg{then macros}\marg{else macros}}
501 Inside the command list of \refCom{csvreader}, the \meta{then macros}
502 are executed for odd-numbered data lines, and the \meta{else macros}
503 are executed for even-numbered lines.
504 \refCom{ifcsvoddrow} is expandable.
505 \begin{dispExample}
506 \csvreader[
507 head to column names,
508 tabular = |l|l|l|l|,
509 table head = \hline\bfseries \# & \bfseries Name & \bfseries Grade\\\hline,
510 table foot = \hline
511 ]{grade.csv}{}{%
512 \ifcsvoddrow{\slshape\thecsvrow & \slshape\name, \givenname & \slshape\grade}%
513 {\bfseries\thecsvrow & \bfseries\name, \givenname & \bfseries\grade}
514 }
515 \end{dispExample}
516
517 The |\ifcsvoddrow| macro may be used for striped tables:
518
519 \begin{dispExample}
520 % This example needs the xcolor package
521 \csvreader[
522 head to column names,
523 tabular = rlcc,
524 table head = \hline\rowcolor{red!50!black}\color{white}\# & \color{white}Person
525 & \color{white}Matr.~No. & \color{white}Grade,
526 late after head = \\\hline\rowcolor{yellow!50},
527 late after line = \ifcsvoddrow{\\\rowcolor{yellow!50}}{\\\rowcolor{red!25}}
528 ]{grade.csv}{}{%
529 \thecsvrow & \givenname~\name & \matriculation & \grade
530 }
531 \end{dispExample}
532
533 Alternatively, |\rowcolors| from the |xcolor| package can be used for this
534 purpose:
535
536 \begin{dispExample}
537 % This example needs the xcolor package
538 \csvreader[
539 head to column names,
540 tabular = rlcc,
541 before table = \rowcolors{2}{red!25}{yellow!50},
542 table head = \hline\rowcolor{red!50!black}\color{white}\# & \color{white}Person
543 & \color{white}Matr.~No. & \color{white}Grade\\\hline
544 ]{grade.csv}{}{%
545 \thecsvrow & \givenname~\name & \matriculation & \grade
546 }
547 \end{dispExample}
548
549 The deprecated, but still available alias for this command is
550 \docAuxCommand{csvifoddrow}.
551 \end{docCommand}
552
553 \clearpage
554
555 \begin{docCommand}[doc updated=2021-06-28]{ifcsvfirstrow}{\marg{then macros}\marg{else macros}}
556 Inside the command list of \refCom{csvreader}, the \meta{then macros}
557 are executed for the first data line, and the \meta{else macros}
558 are executed for all following lines.
559 \refCom{ifcsvfirstrow} is expandable.
560 \begin{dispExample}
561 \csvreader[
562 tabbing,
563 head to column names,
564 table head = {\hspace*{3cm}\=\kill}
565 ]{grade.csv}{}{%
566 \givenname~\name \> (\ifcsvfirstrow{first entry!!}{following entry})
567 }
568 \end{dispExample}
569 The deprecated, but still available alias for this command is
570 \docAuxCommand{csviffirstrow}.
571 \end{docCommand}
572
573 \medskip
574
575
576 \begin{docCommand}{csvfilteraccept}{}
577 All following consistent data lines will be accepted and processed.
578 This command overwrites all previous filter settings and may be used
579 inside \refKey{csvsim/full filter} to implement
580 an own filtering rule together with |\csvfilterreject|.
581 \begin{dispExample}
582 \csvreader[
583 autotabular,
584 full filter = \IfCsvsimStrEqualTF{\csvcoliv}{m}{\csvfilteraccept}{\csvfilterreject}
585 ]{grade.csv}{}{%
586 \csvlinetotablerow
587 }
588 \end{dispExample}
589 \end{docCommand}
590
591
592 \begin{docCommand}{csvfilterreject}{}
593 All following data lines will be ignored.
594 This command overwrites all previous filter settings.
595 \end{docCommand}
596
597
598 \begin{docCommand}{csvline}{}
599 This macro contains the current and unprocessed data line.
600 \begin{dispExample}
601 \csvreader[
602 no head,
603 tabbing,
604 table head = {\textit{line XX:}\=\kill}
605 ]{grade.csv}{}{%
606 \textit{line \thecsvrow:} \> \csvline
607 }
608 \end{dispExample}
609 \end{docCommand}
610
611
612 \clearpage
613 \begin{docCommand}[doc updated=2022-01-11]{csvlinetotablerow}{}
614 Typesets the current processed data line with |&| between the entries.
615 This macro is \emph{expandable}.
616 \end{docCommand}
617
618
619 \begin{docCommands}{
620 { doc name = thecsvrow , doc updated = 2021-06-25 },
621 { doc name = g_csvsim_row_int, doc new = 2021-06-25 }
622 }
623 Typesets the current data line number. This is the
624 current number of accepted data lines without the header line.
625 Despite of the name, there is no associated \LaTeX\ counter |csvrow|,
626 but \refCom{thecsvrow} accesses the \LaTeX3 integer
627 \refCom{g_csvsim_row_int}.
628 \end{docCommands}
629
630
631 \begin{docCommands}[doc new=2021-06-25]{
632 { doc name = thecsvcolumncount },
633 { doc name = g_csvsim_columncount_int }
634 }
635 Typesets the number of columns of the current CSV file. This number
636 is either computed from the first valid line (header or data) or
637 given by \refKey{csvsim/column count}.
638 Despite of the name, there is no associated \LaTeX\ counter |csvcolumncount|,
639 but \refCom{thecsvcolumncount} accesses the \LaTeX3 integer
640 \refCom{g_csvsim_columncount_int}.
641 \begin{dispExample}
642 \csvreader{grade.csv}{}{}%
643 The last file consists of \thecsvcolumncount{} columns and
644 \thecsvrow{} accepted data lines. The total number of lines
645 is \thecsvinputline{}.
646 \end{dispExample}
647 \end{docCommands}
648
649
650 \begin{docCommands}{
651 { doc name = thecsvinputline , doc updated = 2021-06-25 },
652 { doc name = g_csvsim_inputline_int, doc new = 2021-06-25 }
653 }
654 Typesets the current file line number. This is the
655 current number of all data lines including the header line and all
656 lines filtered out.
657 Despite of the name, there is no associated \LaTeX\ counter |csvinputline|,
658 but \refCom{thecsvinputline} accesses the \LaTeX3 integer
659 \refCom{g_csvsim_inputline_int}.
660 \begin{dispExample}
661 \csvreader[
662 no head,
663 filter test = \ifnumequal{\thecsvinputline}{3}
664 ]{grade.csv}{}{%
665 The line with number \thecsvinputline\ contains: \csvline
666 }
667 \end{dispExample}
668 \end{docCommands}
669
670
671
672 \clearpage
673 \section{Macros for Automatic Survey Tables}\label{sec:autotable}%
674
675 The following |\csvauto...| commands are intended for quick data overview
676 with \emph{limited} formatting potential.
677 The most customizable |\csvauto...| commands are
678 \refCom{csvautotabularray} and friends.
679
680 For full control see Subsection~\ref{subsec:tabsupport} on page \pageref{subsec:tabsupport}
681 for the general table options in combination with \refCom{csvreader} and
682 \refCom{csvloop}.
683
684 \begin{docCommands}[
685 doc parameter = \oarg{options}\marg{file name}
686 ]
687 {
688 { doc name = csvautotabular },
689 { doc name = csvautotabular*, doc new = 2021-06-25 }
690 }
691 |\csvautotabular| or |\csvautotabular*|
692 is an abbreviation for the application of the option key
693 \refKey{csvsim/autotabular} or \refKey{csvsim/autotabular*}
694 together with other \meta{options} to \refCom{csvloop}.
695 This macro reads the whole CSV file denoted by \meta{file name}
696 with an automated formatting.
697 The star variant treats the first line as data line and not as header line.
698 \begin{dispExample}
699 \csvautotabular*{grade.csv}
700 \end{dispExample}
701 \begin{dispExample}
702 \csvautotabular[filter equal={\csvcoliv}{f}]{grade.csv}
703 \end{dispExample}
704 \end{docCommands}
705
706
707
708 \begin{docCommands}[
709 doc parameter = \oarg{options}\marg{file name}
710 ]
711 {
712 { doc name = csvautolongtable },
713 { doc name = csvautolongtable*, doc new = 2021-06-25 }
714 }
715 |\csvautolongtable| or |\csvautolongtable*|
716 is an abbreviation for the application of the option key
717 \refKey{csvsim/autolongtable} or \refKey{csvsim/autolongtable*}
718 together with other \meta{options} to \refCom{csvloop}.
719 This macro reads the whole CSV file denoted by \meta{file name}
720 with an automated formatting.
721 For application, the package \refPkg{longtable} is required which has to be
722 loaded in the preamble.
723 The star variant treats the first line as data line and not as header line.
724 \begin{dispListing}
725 \csvautolongtable{grade.csv}
726 \end{dispListing}
727 \csvautolongtable{grade.csv}
728 \end{docCommands}
729
730
731 \clearpage
732
733
734 \begin{docCommands}[
735 doc parameter = \oarg{options}\marg{file name}
736 ]
737 {
738 { doc name = csvautobooktabular },
739 { doc name = csvautobooktabular*, doc new = 2021-06-25 }
740 }
741 |\csvautobooktabular| or |\csvautobooktabular*|
742 is an abbreviation for the application of the option key
743 \refKey{csvsim/autobooktabular} or \refKey{csvsim/autobooktabular*}
744 together with other \meta{options} to \refCom{csvloop}.
745 This macro reads the whole CSV file denoted by \meta{file name}
746 with an automated formatting.
747 For application, the package \refPkg{booktabs} is required which has to be
748 loaded in the preamble.
749 The star variant treats the first line as data line and not as header line.
750 \begin{dispExample}
751 \csvautobooktabular{grade.csv}
752 \end{dispExample}
753 \end{docCommands}
754
755
756 \begin{docCommands}[
757 doc parameter = \oarg{options}\marg{file name}
758 ]
759 {
760 { doc name = csvautobooklongtable },
761 { doc name = csvautobooklongtable*, doc new = 2021-06-25 }
762 }
763 |\csvautobooklongtable| or |\csvautobooklongtable*|
764 is an abbreviation for the application of the option key
765 \refKey{csvsim/autobooklongtable} or \refKey{csvsim/autobooklongtable*}
766 together with other \meta{options} to \refCom{csvloop}.
767 This macro reads the whole CSV file denoted by \meta{file name}
768 with an automated formatting.
769 For application, the packages \refPkg{booktabs} and \refPkg{longtable} are required which have to be
770 loaded in the preamble.
771 The star variant treats the first line as data line and not as header line.
772 \begin{dispListing}
773 \csvautobooklongtable{grade.csv}
774 \end{dispListing}
775 \csvautobooklongtable{grade.csv}
776 \end{docCommands}
777
778
779 \clearpage
780
781 \begin{docCommands}[
782 doc parameter = \oarg{options}\marg{file name}\oarg{taboptions 1}\oarg{taboptions 2},
783 ]
784 {
785 { doc name = csvautotabularray, doc new and updated={2023-10-13}{2023-10-17}, },
786 { doc name = csvautotabularray* },
787 { doc name = csvautolongtabularray },
788 { doc name = csvautolongtabularray* },
789 }
790 These macros are abbreviations for the application of the option keys
791 \refKey{csvsim/autotabularray}, \refKey{csvsim/autotabularray*},\\
792 \refKey{csvsim/autolongtabularray}, or \refKey{csvsim/autolongtabularray*}
793 together with other \meta{options} to \refCom{csvloop}.
794 These macros read the whole CSV file denoted by \meta{file name}
795 with an automated formatting.
796 For application, the package \refPkg{tabularray} is required which has to be
797 loaded in the preamble.
798 \refCom{csvautotabularray} uses the \docAuxEnvironment*{tblr} environment and
799 \refCom{csvautolongtabularray} uses the \docAuxEnvironment*{longtblr} environment.
800 The star variants treat the first line as data line and not as header line.\par
801 Options to the table environments from \refPkg{tabularray} may be given
802 by either setting \refKey{csvsim/generic table options} or
803 using \meta{taboptions 1} and \meta{taboptions 2}.\par
804 The default setting is
805 \begin{dispListing}
806 generic table options =
807 { {
808 row{1} = {font=\bfseries,preto=\MakeUppercase},
809 hline{1,Z} = {0.08em},
810 hline{2} = {0.05em},
811 } }
812 \end{dispListing}
813 For the star variants, the default setting is
814 \begin{dispListing}
815 generic table options =
816 { {
817 hline{1,Z} = {0.08em},
818 } }
819 \end{dispListing}
820
821 Examples:
822
823 \begin{dispExample}
824 \csvautotabularray{grade.csv}
825 \end{dispExample}
826
827 \begin{dispExample}
828 \csvautotabularray[table centered,
829 generic table options =
830 {{
831 row{odd} = {red!85!gray!7},
832 row{1} = {bg=red!85!gray, fg=white,
833 font=\bfseries, preto=\MakeUppercase},
834 }}
835 ] {grade.csv}
836 \end{dispExample}
837
838 \clearpage
839
840 Alternatively to \refKey{csvsim/generic table options}
841 (and overruling this option), one may give options to
842 \docAuxEnvironment*{tblr} or \docAuxEnvironment*{longtblr}
843 using \meta{taboptions 1} and \meta{taboptions 2}.
844 If \meta{taboptions 2} is \emph{not present}, then \meta{taboptions 1}
845 is used as
846 mandatory argument (\refPkg{tabularray} inner specification).
847 Otherwise, \meta{taboptions 1} is used as
848 optional argument (\refPkg{tabularray} outer specification)
849 and
850 \meta{taboptions 2} as
851 mandatory argument (\refPkg{tabularray} inner specification).
852
853 \begin{dispExample}
854 \csvautotabularray[table centered]
855 {grade.csv}
856 [
857 row{odd} = {red!85!gray!7},
858 row{1} = {bg=red!85!gray, fg=white,
859 font=\bfseries, preto=\MakeUppercase},
860 ]
861 \end{dispExample}
862
863
864 \begin{dispExample}
865 \csvautotabularray[table centered]
866 {grade.csv}
867 [
868 tall,
869 caption = {My table},
870 remark{Note} = {My remark},
871 ]
872 [
873 row{odd} = {red!85!gray!7},
874 row{1} = {bg=red!85!gray, fg=white,
875 font=\bfseries, preto=\MakeUppercase},
876 ]
877 \end{dispExample}
878
879
880 \end{docCommands}
881
882
883 \clearpage
884 \section{Option Keys}\label{sec:schluessel}%
885 For the \meta{options} in \refCom{csvreader} respectively \refCom{csvloop}
886 the following |l3keys| keys can be applied. The \meta{module} name |csvsim| is not
887 to be used inside these macros.
888
889
890 \subsection{Command Definition}%--------%[[
891
892 \begin{docCsvKey}{before reading}{=\meta{code}}{no default, initially empty}
893 Sets the \meta{code} to be executed before the CSV file is opened.
894 \end{docCsvKey}
895
896 \begin{docCsvKey}{after head}{=\meta{code}}{no default, initially empty}
897 Sets the \meta{code} to be executed after the header line is read.
898 \refCom{thecsvcolumncount} and header entries are available.
899 \end{docCsvKey}
900
901 \begin{docCsvKey}{before filter}{=\meta{code}}{no default, initially empty}
902 Sets the \meta{code} to be executed after reading and consistency checking
903 of a data line. It is executed before any filter condition is checked,
904 see e.g. \refKey{csvsim/filter ifthen} and
905 also see \refKey{csvsim/full filter}.
906 No additions to the input stream should be given here.
907 All line entries are available.
908 \end{docCsvKey}
909
910 \begin{docCsvKey}[][doc new=2021-07-06]{after filter}{=\meta{code}}{no default, initially empty}
911 Sets the \meta{code} to be executed for an accepted line after
912 \refKey{csvsim/late after line} and before \refKey{csvsim/before line}.
913 All line entries are available.
914 No additions to the input stream should be given here. \meta{code} may
915 contain processing of data content to generate new values.
916 \end{docCsvKey}
917
918 \begin{docCsvKey}{late after head}{=\meta{code}}{no default, initially empty}
919 Sets the \meta{code} to be executed after reading and disassembling
920 of the first accepted data line.
921 These operations are executed before further processing of this line.
922 \meta{code} should not refer to any data content, but may be something
923 like |\\|.
924 \end{docCsvKey}
925
926 \begin{docCsvKey}{late after line}{=\meta{code}}{no default, initially empty}
927 Sets the \meta{code} to be executed after reading and disassembling
928 of the next accepted data line (after \refKey{csvsim/before filter}).
929 These operations are executed before further processing of this line.
930 \meta{code} should not refer to any data content, but may be something
931 like |\\|.
932 \refKey{csvsim/late after line} overwrites
933 \refKey{csvsim/late after first line} and
934 \refKey{csvsim/late after last line}.
935 Note that table options like \refKey{csvsim/tabular} set this key to |\\|
936 automatically.
937 \end{docCsvKey}
938
939
940 \begin{docCsvKey}{late after first line}{=\meta{code}}{no default, initially empty}
941 Sets the \meta{code} to be executed after reading and disassembling
942 of the second accepted data line instead of \refKey{csvsim/late after line}.
943 \meta{code} should not refer to any data content.
944 This key has to be set after \refKey{csvsim/late after line}.
945 \end{docCsvKey}
946
947
948 \begin{docCsvKey}{late after last line}{=\meta{code}}{no default, initially empty}
949 Sets the \meta{code} to be executed after processing of the last
950 accepted data line instead of \refKey{csvsim/late after line}.
951 \meta{code} should not refer to any data content.
952 This key has to be set after \refKey{csvsim/late after line}.
953 \end{docCsvKey}
954
955
956 \begin{docCsvKey}{before line}{=\meta{code}}{no default, initially empty}
957 Sets the \meta{code} to be executed after \refKey{csvsim/after filter}
958 and before \refKey{csvsim/command}.
959 All line entries are available.
960 \refKey{csvsim/before line} overwrites
961 \refKey{csvsim/before first line}.
962 \end{docCsvKey}
963
964
965 \begin{docCsvKey}{before first line}{=\meta{code}}{no default, initially empty}
966 Sets the \meta{code} to be executed instead of \refKey{csvsim/before line}
967 for the first accepted data line.
968 All line entries are available.
969 This key has to be set after \refKey{csvsim/before line}.
970 \end{docCsvKey}
971
972 \pagebreak
973
974 \begin{docCsvKey}{command}{=\meta{code}}{no default, initially \cs{csvline}}
975 Sets the \meta{code} to be executed for every accepted data line.
976 It is executed between \refKey{csvsim/before line} and \refKey{csvsim/after line}.
977 \refKey{csvsim/command} describes the main processing of the line
978 entries. \refCom{csvreader} sets \refKey{csvsim/command} as mandatory
979 parameter.
980 \end{docCsvKey}
981
982 \begin{docCsvKey}{after line}{=\meta{code}}{no default, initially empty}
983 Sets the \meta{code} to be executed for every accepted data line
984 after \refKey{csvsim/command}.
985 All line entries are still available.
986 \refKey{csvsim/after line} overwrites \refKey{csvsim/after first line}.
987 \end{docCsvKey}
988
989
990 \begin{docCsvKey}{after first line}{=\meta{code}}{no default, initially empty}
991 Sets the \meta{code} to be executed instead of \refKey{csvsim/after line}
992 for the first accepted data line.
993 All line entries are still available.
994 This key has to be set after \refKey{csvsim/after line}.
995 \end{docCsvKey}
996
997 \begin{docCsvKey}{after reading}{=\meta{code}}{no default, initially empty}
998 Sets the \meta{code} to be executed after the CSV file is closed.
999 \end{docCsvKey}
1000
1001 \bigskip
1002
1003 The following example illustrates the sequence of command execution.
1004 Note that \refKey{csvsim/command} is set by the mandatory last
1005 parameter of \refCom{csvreader}.
1006
1007 \begin{dispExample}
1008 \csvreader[
1009 before reading = \meta{before reading}\\,
1010 after head = \meta{after head},
1011 before filter = \\\meta{before filter},
1012 after filter = \meta{after filter},
1013 late after head = \meta{late after head},
1014 late after line = \meta{late after line},
1015 late after first line = \meta{late after first line},
1016 late after last line = \\\meta{late after last line},
1017 before line = \meta{before line},
1018 before first line = \meta{before first line},
1019 after line = \meta{after line},
1020 after first line = \meta{after first line},
1021 after reading = \\\meta{after reading}
1022 ]{grade.csv}{name=\name}{\textbf{\name}}%
1023 \end{dispExample}
1024
1025 Additional command definition keys are provided for the supported tables,
1026 see Section~\ref{subsec:tabsupport} from page~\pageref{subsec:tabsupport}.
1027
1028 \clearpage
1029 \subsection{Header Processing and Column Name Assignment}%
1030
1031 \begin{docCsvKey}[doc updated=2024-09-18]{head}{\colOpt{=true\textbar false}}{default |true|, initially |true|}
1032 If this key is set, the first non-empty line of the CSV file is treated as a header
1033 line which can be used for column name assignments.
1034 \end{docCsvKey}
1035
1036
1037 \begin{docCsvKey}{no head}{}{no value}
1038 Abbreviation for |head=false|, i.\,e. the first non-empty line of the CSV file is
1039 treated as data line.
1040 Note that this option cannot be used in combination with
1041 the |\csvauto...| commands like \refCom{csvautotabular}, etc.
1042 Instead, there are \emph{star} variants like \refCom{csvautotabular*} to
1043 process files without header line.
1044 See Section~\ref{noheader} on page~\pageref{noheader} for examples.
1045 \end{docCsvKey}
1046
1047 \begin{docCsvKey}{column names}{=\marg{assignments}}{no default, initially empty}
1048 Adds some new \meta{assignments} of macros to columns in key–value syntax.
1049 Existing assignments are kept.\par
1050 The \meta{assignments} are given as comma-separated list of key–value pairs
1051 \mbox{\meta{name}|=|\meta{macro}}. Here, \meta{name} is an entry from the
1052 header line \emph{or} the arabic number of the addressed column.
1053 \meta{macro} is some \TeX\ macro which gets the content of the addressed column.
1054 \begin{dispListing}
1055 column names = {name=\surname, givenname=\firstname, grade=\grade}
1056 \end{dispListing}
1057 \end{docCsvKey}
1058
1059
1060 \begin{docCsvKey}{column names reset}{}{no value}
1061 Clears all assignments of macros to columns.
1062 \end{docCsvKey}
1063
1064
1065 \begin{docCsvKey}{head to column names}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1066 If this key is set, the entries of the header line are used automatically
1067 as macro names for the columns. This option can be used only, if
1068 the header entries do not contain spaces and special characters to be
1069 used as feasible \LaTeX\ macro names.
1070 Note that the macro definition is \emph{global} and may therefore override
1071 existing macros for the rest of the document. Adding
1072 \refKey{csvsim/head to column names prefix} may help to avoid unwanted
1073 overrides.
1074 \end{docCsvKey}
1075
1076
1077 \begin{docCsvKey}[][doc new=2019-07-16]{head to column names prefix}{=\meta{text}}{no default, initially empty}
1078 The given \meta{text} is prefixed to the name of all macros generated by
1079 \refKey{csvsim/head to column names}. For example, if you use the settings
1080 \begin{dispListing}
1081 head to column names,
1082 head to column names prefix=MY,
1083 \end{dispListing}
1084 a header entry |section| will generate the corresponding macro
1085 |\MYsection| instead of destroying the standard \LaTeX\ |\section| macro.
1086 \end{docCsvKey}
1087
1088
1089 \begin{docCsvKey}[][doc new=2022-02-01]{column names detection}{\colOpt{=true\textbar false}}{default |true|, initially |true|}
1090 If this key is set, the header line is detected for names which can be used
1091 for \refKey{csvsim/column names} and \refKey{csvsim/head to column names}.
1092 Otherwise, these options are not functional.\\
1093 This key can and should be set to |false|, if the header line contains
1094 macros or characters not allowed inside \LaTeX\ control sequences, because
1095 otherwise compilation error are to be expected.
1096 \end{docCsvKey}
1097
1098
1099 \clearpage
1100 \subsection{Consistency Check}\label{sec:consistency}%
1101
1102 \begin{docCsvKey}{check column count}{\colOpt{=true\textbar false}}{default |true|, initially |true|}
1103 This key defines, whether the number of entries in a data line is checked against
1104 an expected value or not.\\
1105 If |true|, every non consistent line is ignored without announcement.\\
1106 If |false|, every line is accepted and may produce an error during
1107 further processing.
1108 \end{docCsvKey}
1109
1110
1111 \begin{docCsvKey}{no check column count}{}{no value}
1112 Abbreviation for |check column count=false|.
1113 \end{docCsvKey}
1114
1115
1116 \begin{docCsvKey}[][doc updated=2021-06-24]{column count}{=\meta{number}}{no default, initially |0|}
1117 Sets the \meta{number} of feasible entries per data line.
1118 If \refKey{csvsim/column count} is set to |0|, the number of entries of
1119 the first non-empty line determines the column count (automatic detection).
1120
1121 This setting is only useful in connection with \refKey{csvsim/no head},
1122 since \meta{number} would be replaced by the number of entries in the
1123 header line otherwise.
1124 \end{docCsvKey}
1125
1126
1127 \begin{docCsvKey}{on column count error}{=\meta{code}}{no default, initially empty}
1128 \meta{code} to be executed for unfeasible data lines.
1129 \end{docCsvKey}
1130
1131
1132 \begin{docCsvKey}{warn on column count error}{}{style, no value}
1133 Display of a warning for unfeasible data lines.
1134 \end{docCsvKey}
1135
1136
1137 \clearpage
1138 \subsection{Filtering}\label{subsec:filtering}%
1139
1140 Applying a \emph{filter} means that data lines are only processed / displayed,
1141 if they fulfill a given \emph{condition}.
1142
1143 The following string compare filters \refKey{csvsim/filter strcmp} and
1144 \refKey{csvsim/filter equal} are identical by logic, but differ in implementation.
1145
1146 \begin{docCsvKey}[][doc updated=2022-10-21]{filter strcmp}{=\marg{stringA}\marg{stringB}}{no default}
1147 Only lines where \meta{stringA} and \meta{stringB} are equal after expansion
1148 are accepted.
1149 The implementation is done with \docAuxCommand*{str_if_eq_p:ee}.
1150 \begin{dispExample}
1151 % \usepackage{booktabs}
1152 \csvreader[
1153 head to column names,
1154 tabular = llll,
1155 table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
1156 table foot = \bottomrule,
1157 filter strcmp = {\gender}{f}, %>> list only female persons <<
1158 ]{grade.csv}{}{%
1159 \thecsvrow & \slshape\name, \givenname & \matriculation & \grade
1160 }
1161 \end{dispExample}
1162 \end{docCsvKey}
1163
1164
1165 \begin{docCsvKey}[][doc updated=2022-10-21]{filter not strcmp}{=\marg{stringA}\marg{stringB}}{no default}
1166 Only lines where \meta{stringA} and \meta{stringB} are not equal after expansion
1167 are accepted.
1168 The implementation is done with \docAuxCommand*{str_if_eq_p:ee}.
1169 \end{docCsvKey}
1170
1171
1172 \begin{docCsvKey}{filter equal}{=\marg{stringA}\marg{stringB}}{no default}
1173 Only lines where \meta{stringA} and \meta{stringB} are equal after expansion
1174 are accepted.
1175 The implementation is done with the \refPkg{ifthen} package (loading required!).
1176 \end{docCsvKey}
1177
1178
1179 \begin{docCsvKey}{filter not equal}{=\marg{stringA}\marg{stringB}}{no default}
1180 Only lines where \meta{stringA} and \meta{stringB} are not equal after expansion
1181 are accepted.
1182 The implementation is done with the \refPkg{ifthen} package (loading required!).
1183 \end{docCsvKey}
1184
1185
1186 \enlargethispage*{1cm}
1187 \begin{docCsvKey}[][doc new and updated={2021-06-25}{2022-10-21}]{filter fp}{=\marg{floating point comparison}}{no default}
1188 Only data lines which fulfill a \LaTeX3 \meta{floating point comparison}
1189 are accepted. The evaluation is done using \docAuxCommand*{fp_compare_p:n}.
1190 \begin{dispExample}
1191 % \usepackage{booktabs}
1192 \csvreader[
1193 head to column names,
1194 tabular = llll,
1195 table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
1196 table foot = \bottomrule,
1197 %>> list only matriculation numbers greater than 20000
1198 % and grade less than 4.0 <<
1199 filter fp = { \matriculation > 20000 && \grade < 4.0 },
1200 ]{grade.csv}{}{%
1201 \thecsvrow & \slshape\name, \givenname & \matriculation & \grade
1202 }
1203 \end{dispExample}
1204 \end{docCsvKey}
1205
1206
1207 \clearpage
1208
1209 \begin{docCsvKey}[][doc new and updated={2021-06-25}{2022-10-21}]{filter bool}{=\marg{boolean expression}}{no default}
1210 Only data lines which fulfill a \LaTeX3 \meta{boolean expression} are accepted.
1211 Note that such an \meta{boolean expression} needs expl3 code.
1212 To preprocess the data line before testing the \meta{boolean expression},
1213 the option key \refKey{csvsim/before filter} can be used.
1214 \begin{dispExample}
1215 % For convenience, we save the filter
1216 \ExplSyntaxOn
1217 %>> list only matriculation numbers greater than 20000, list only men <<
1218 \csvstyle{myfilter}
1219 {
1220 filter~bool =
1221 {
1222 \int_compare_p:n { \matriculation > 20000 } &&
1223 \str_if_eq_p:ee { \gender }{ m }
1224 }
1225 }
1226 \ExplSyntaxOff
1227
1228 \csvreader[
1229 head to column names,
1230 tabular = llll,
1231 table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
1232 table foot = \bottomrule,
1233 myfilter
1234 ]{grade.csv}{}{%
1235 \thecsvrow & \slshape\name, \givenname & \matriculation & \grade
1236 }
1237 \end{dispExample}
1238 \end{docCsvKey}
1239
1240 \medskip
1241 \begin{docCommand}[doc new=2021-06-25]{csvfilterbool}{\marg{key}\marg{boolean expression}}
1242 Defines a new |l3keys| meta key which applies \refKey{csvsim/filter bool}
1243 with the given \meta{boolean expression}.
1244 \begin{dispExample}
1245 % For convenience, we save the filter
1246 \ExplSyntaxOn
1247 %>> list only matriculation numbers greater than 20000, list only men <<
1248 \csvfilterbool{myfilter}
1249 {
1250 \int_compare_p:n { \matriculation > 20000 } &&
1251 \str_if_eq_p:ee { \gender }{ m }
1252 }
1253 \ExplSyntaxOff
1254
1255 \csvreader[
1256 head to column names,
1257 tabular = llll,
1258 table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
1259 table foot = \bottomrule,
1260 myfilter
1261 ]{grade.csv}{}{%
1262 \thecsvrow & \slshape\name, \givenname & \matriculation & \grade
1263 }
1264 \end{dispExample}
1265 \end{docCommand}
1266
1267
1268 \clearpage
1269
1270 The following filter options are \emph{appendable} to the expl3 based
1271 filter options:
1272 \begin{itemize}
1273 \item \refKey{csvsim/filter strcmp}
1274 \item \refKey{csvsim/filter not strcmp}
1275 \item \refKey{csvsim/filter fp}
1276 \item \refKey{csvsim/filter bool}
1277 \end{itemize}
1278
1279 \begin{docCsvKeys}[
1280 doc parameter = {=\marg{stringA}\marg{stringB}},
1281 doc description = {no default},
1282 doc new = {2022-10-21}
1283 ]
1284 {
1285 { doc name = and filter strcmp },
1286 { doc name = or filter strcmp },
1287 }
1288 Like \refKey{csvsim/filter strcmp}, but appended to a required existing
1289 expl3 based filter with \emph{and} (|&&|) resp. \emph{or} (\texttt{\textbar\textbar}).
1290
1291 \begin{dispExample}
1292 \csvreader[
1293 head to column names,
1294 tabular = llll,
1295 table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
1296 table foot = \bottomrule,
1297 filter fp = {\matriculation>20000},
1298 and filter strcmp = {\gender}{m},
1299 ]{grade.csv}{}{%
1300 \thecsvrow & \slshape\name, \givenname & \matriculation & \grade
1301 }
1302 \end{dispExample}
1303 \end{docCsvKeys}
1304
1305
1306 \begin{docCsvKeys}[
1307 doc parameter = {=\marg{stringA}\marg{stringB}},
1308 doc description = {no default},
1309 doc new = {2022-10-21}
1310 ]
1311 {
1312 { doc name = and filter not strcmp },
1313 { doc name = or filter not strcmp },
1314 }
1315 Like \refKey{csvsim/filter not strcmp}, but appended to a required existing
1316 expl3 based filter with \emph{and} (|&&|) resp. \emph{or} (\texttt{\textbar\textbar}).
1317 \end{docCsvKeys}
1318
1319
1320 \begin{docCsvKeys}[
1321 doc parameter = {=\marg{floating point comparison}},
1322 doc description = {style, no default},
1323 doc new = {2022-10-21}
1324 ]
1325 {
1326 { doc name = and filter fp },
1327 { doc name = or filter fp },
1328 }
1329 Like \refKey{csvsim/filter fp}, but appended to a required existing
1330 expl3 based filter with \emph{and} (|&&|) resp. \emph{or} (\texttt{\textbar\textbar}).
1331 \begin{dispExample}
1332 % \usepackage{booktabs}
1333 \csvreader[
1334 head to column names,
1335 tabular = llll,
1336 table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
1337 table foot = \bottomrule,
1338 %>> list only matriculation numbers greater than 20000 and grade less than 4.0 <<
1339 filter fp = { \matriculation > 20000 },
1340 and filter fp = { \grade < 4.0 },
1341 ]{grade.csv}{}{%
1342 \thecsvrow & \slshape\name, \givenname & \matriculation & \grade
1343 }
1344 \end{dispExample}
1345 \end{docCsvKeys}
1346
1347 \enlargethispage*{1cm}
1348 \begin{docCsvKeys}[
1349 doc parameter = {=\marg{boolean expression}},
1350 doc description = {style, no default},
1351 doc new = {2022-10-21}
1352 ]
1353 {
1354 { doc name = and filter bool },
1355 { doc name = or filter bool },
1356 }
1357 Like \refKey{csvsim/filter bool}, but appended to a required existing
1358 expl3 based filter with \emph{and} (|&&|) resp. \emph{or} (\texttt{\textbar\textbar}).
1359 \end{docCsvKeys}
1360
1361
1362 \clearpage
1363
1364 \begin{docCsvKey}[][doc new=2016-07-01]{filter test}{=\meta{condition}}{no default}
1365 Only data lines which fulfill a logical \meta{condition} are accepted.
1366 For the \meta{condition}, every single test normally employed like
1367 \begin{dispListing}
1368 \iftest{some testing}{true}{false}
1369 \end{dispListing}
1370 can be used as
1371 \begin{dispListing}
1372 filter test=\iftest{some testing},
1373 \end{dispListing}
1374 For |\iftest|, tests from the \refPkg{etoolbox} package like
1375 |\ifnumcomp|, |\ifdimgreater|, etc. and from \Fullref{sec:stringtests} can be used.
1376 Also, arbitrary own macros fulfilling this signature can be applied.
1377 \begin{dispExample}
1378 % \usepackage{etoolbox,booktabs}
1379 \csvreader[
1380 head to column names,
1381 tabular = llll,
1382 table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
1383 table foot = \bottomrule,
1384 %>> list only matriculation numbers greater than 20000 <<
1385 filter test = \ifnumgreater{\matriculation}{20000},
1386 ]{grade.csv}{}{%
1387 \thecsvrow & \slshape\name, \givenname & \matriculation & \grade
1388 }
1389 \end{dispExample}
1390 \end{docCsvKey}
1391
1392
1393 \medskip
1394 \begin{docCsvKey}[][doc new=2016-07-01]{filter expr}{=\meta{boolean expression}}{no default}
1395 Only data lines which fulfill a \meta{boolean expression} are accepted.
1396 Every \meta{boolean expression}
1397 from the \refPkg{etoolbox} package is feasible (package loading required!).
1398 To preprocess the data line before testing the \meta{boolean expression},
1399 the option key \refKey{csvsim/before filter} can be used.
1400 \begin{dispExample}
1401 % \usepackage{etoolbox,booktabs}
1402 \csvreader[
1403 head to column names,
1404 tabular = llll,
1405 table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
1406 table foot = \bottomrule,
1407 %>> list only matriculation numbers greater than 20000
1408 % and grade less than 4.0 <<
1409 filter expr = { test{\ifnumgreater{\matriculation}{20000}}
1410 and test{\ifdimless{\grade pt}{4.0pt}} },
1411 ]{grade.csv}{}{%
1412 \thecsvrow & \slshape\name, \givenname & \matriculation & \grade
1413 }
1414 \end{dispExample}
1415 \end{docCsvKey}
1416
1417
1418 \clearpage
1419 \begin{docCsvKey}[][doc new=2016-07-01]{filter ifthen}{=\meta{boolean expression}}{no default}
1420 Only data lines which fulfill a \meta{boolean expression} are accepted.
1421 For the \meta{boolean expression}, every term from the \refPkg{ifthen} package
1422 is feasible (package loading required!).
1423 To preprocess the data line before testing the \meta{boolean expression},
1424 the option key \refKey{csvsim/before filter} can be used.
1425
1426 \begin{dispExample}
1427 % \usepackage{ifthen,booktabs}
1428 \csvreader[
1429 head to column names,
1430 tabular = llll,
1431 table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
1432 table foot = \bottomrule,
1433 %>> list only female persons <<
1434 filter ifthen=\equal{\gender}{f},
1435 ]{grade.csv}{}{%
1436 \thecsvrow & \slshape\name, \givenname & \matriculation & \grade
1437 }
1438 \end{dispExample}
1439 \end{docCsvKey}
1440
1441
1442 \begin{docCsvKey}{no filter}{}{no value, initially set}
1443 Clears a set filter.
1444 \end{docCsvKey}
1445
1446
1447 \begin{docCsvKey}{filter accept all}{}{no value, initially set}
1448 Alias for |no filter|. All consistent data lines are accepted.
1449 \end{docCsvKey}
1450
1451
1452 \begin{docCsvKey}{filter reject all}{}{no value}
1453 All data line are ignored.
1454 \end{docCsvKey}
1455
1456
1457 \begin{docCsvKey}[][doc new=2016-07-01]{full filter}{=\meta{code}}{no default}
1458 Technically, this key is an alias for \refKey{csvsim/before filter}.
1459 Philosophically, \refKey{csvsim/before filter} computes something before
1460 a filter condition is set, but \refKey{csvsim/full filter} should implement
1461 the full filtering. Especially, \refCom{csvfilteraccept} or
1462 \refCom{csvfilterreject} \emph{should} be set inside the \meta{code}.
1463 \begin{dispExample}
1464 % \usepackage{etoolbox,booktabs}
1465 \csvreader[
1466 head to column names,
1467 tabular = llll,
1468 table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
1469 table foot = \bottomrule,
1470 %>> list only matriculation numbers greater than 20000
1471 % and grade less than 4.0 <<
1472 full filter = \ifnumgreater{\matriculation}{20000}
1473 {\ifdimless{\grade pt}{4.0pt}{\csvfilteraccept}{\csvfilterreject}}
1474 {\csvfilterreject},
1475 ]{grade.csv}{}{%
1476 \thecsvrow & \slshape\name, \givenname & \matriculation & \grade
1477 }
1478 \end{dispExample}
1479 \end{docCsvKey}
1480
1481
1482
1483 %]]
1484
1485 \clearpage
1486 \subsection{Line Range}\label{subsec:linerange}
1487
1488 Applying a \emph{line range} means to select certain line numbers to be
1489 displayed. These line numbers are not necessarily line numbers of
1490 the input file, see \refCom{thecsvinputline}, but line numbers of
1491 type \refCom{thecsvrow}.
1492
1493 For example, if a \emph{filter} was applied, see \Fullref{subsec:filtering},
1494 and 42 lines are accepted, a \emph{range} could select the first 20 of them or
1495 line 10 to 30 of the accepted lines.
1496
1497
1498 \begin{docCsvKey}[][doc new and updated={2021-06-29}{2022-09-21}]{range}{=\brackets{\meta{range1},\meta{range2},\meta{range3},... }}{no default, initially empty}
1499 Defines a comma-separated list of line ranges. If a line number \refCom{thecsvrow}
1500 satisfies one or more of the given \meta{range1}, \meta{range2}, \ldots,
1501 the corresponding line is processed and displayed.
1502 If \refKey{csvsim/range} is set to empty, all lines are accepted.
1503
1504 Every \meta{range} can
1505 corresponds to one of the following variants:
1506 \begin{tabbing}
1507 \hspace*{2cm}\=\kill
1508 \texttt{\meta{a}-\meta{b}} \> meaning line numbers \meta{a} to \meta{b}.\\
1509 \texttt{\meta{a}-} \> meaning line numbers \meta{a} to |\c_max_int|=2 147 483 647.\\
1510 \texttt{-\meta{b}} \> meaning line numbers 1 to \meta{b}.\\
1511 \texttt{-} \> meaning line numbers 1 to 2 147 483 647 (inefficient; don't use).\\
1512 \texttt{\meta{a}} \> meaning line numbers \meta{a} to \meta{a} (i.e. only \meta{a}).\\
1513 \texttt{\meta{a}+\meta{d}} \> meaning line numbers \meta{a} to \meta{a}$+$\meta{d}$-1$.\\
1514 \texttt{\meta{a}+} \> meaning line numbers \meta{a} to \meta{a} (i.e. only \meta{a}).\\
1515 \texttt{+\meta{d}} \> meaning line numbers 1 to \meta{d}.\\
1516 \texttt{+} \> meaning line numbers 1 to 1 (i.e. only 1; weird).\\
1517 \end{tabbing}
1518
1519 \begin{dispExample}
1520 % \usepackage{booktabs}
1521 \csvreader[
1522 head to column names,
1523 range = 2-3,
1524 tabular = llll,
1525 table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
1526 table foot = \bottomrule,
1527 ]{grade.csv}{}{%
1528 \thecsvrow & \slshape\name, \givenname & \matriculation & \grade
1529 }
1530 \end{dispExample}
1531
1532
1533 \begin{dispExample}
1534 % \usepackage{booktabs}
1535 \csvreader[
1536 head to column names,
1537 range = 3-,
1538 tabular = llll,
1539 table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
1540 table foot = \bottomrule,
1541 ]{grade.csv}{}{%
1542 \thecsvrow & \slshape\name, \givenname & \matriculation & \grade
1543 }
1544 \end{dispExample}
1545
1546
1547 \begin{dispExample}
1548 % \usepackage{booktabs}
1549 \csvreader[
1550 head to column names,
1551 range = 2+2,
1552 tabular = llll,
1553 table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
1554 table foot = \bottomrule,
1555 ]{grade.csv}{}{%
1556 \thecsvrow & \slshape\name, \givenname & \matriculation & \grade
1557 }
1558 \end{dispExample}
1559
1560 \begin{dispExample}
1561 % \usepackage{booktabs}
1562 \csvreader[
1563 head to column names,
1564 range = {2,4},
1565 tabular = llll,
1566 table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
1567 table foot = \bottomrule,
1568 ]{grade.csv}{}{%
1569 \thecsvrow & \slshape\name, \givenname & \matriculation & \grade
1570 }
1571 \end{dispExample}
1572
1573 To select the last $n$ lines, you have to know or count the line numbers first.
1574 The following example displays the last three line numbers:
1575
1576 \begin{dispExample}
1577 % \usepackage{booktabs}
1578 \csvreader{grade.csv}{}{}% count line numbers
1579 \csvreader[
1580 head to column names,
1581 range = {\thecsvrow-2}-,
1582 tabular = llll,
1583 table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
1584 table foot = \bottomrule,
1585 ]{grade.csv}{}{%
1586 \thecsvrow & \slshape\name, \givenname & \matriculation & \grade
1587 }
1588 \end{dispExample}
1589
1590 \end{docCsvKey}
1591
1592
1593
1594 \clearpage
1595 \subsection{Table Support}\label{subsec:tabsupport}%--------%[[
1596
1597 \subsubsection{Predefined Tables}\label{subsubsec:table_predef}
1598
1599 \begin{docCsvKey}{tabular}{=\meta{table format}}{style, no default}
1600 Surrounds the CSV processing with |\begin{tabular}|\marg{table format}
1601 at begin and with |\end{tabular}| at end.
1602 Additionally, the commands defined by the key–values of
1603 \refKey{csvsim/before table}, \refKey{csvsim/table head}, \refKey{csvsim/table foot},
1604 and \refKey{csvsim/after table} are executed at the appropriate places.
1605 \refKey{csvsim/late after line} is set to \cs{}\cs{}.
1606 \end{docCsvKey}
1607
1608
1609 \begin{docCsvKey}{centered tabular}{=\meta{table format}}{style, no default}
1610 Like \refKey{csvsim/tabular} but inside an additional |center| environment.
1611 \end{docCsvKey}
1612
1613
1614 \begin{docCsvKey}{longtable}{=\meta{table format}}{style, no default}
1615 Like \refKey{csvsim/tabular} but for the |longtable| environment.
1616 This requires the package \refPkg{longtable} (not loaded automatically).
1617 \end{docCsvKey}
1618
1619
1620 \begin{docCsvKey}{tabbing}{}{style, no value}
1621 Like \refKey{csvsim/tabular} but for the |tabbing| environment.
1622 \end{docCsvKey}
1623
1624
1625 \begin{docCsvKey}{centered tabbing}{}{style, no value}
1626 Like \refKey{csvsim/tabbing} but inside an additional |center| environment.
1627 \end{docCsvKey}
1628
1629
1630 \begin{docCsvKey}[][doc new=2021-07-06]{tabularray}{=\meta{table format}}{style, no default}
1631 Like \refKey{csvsim/tabular} but for the |tblr| environment.
1632 This requires the package \refPkg{tabularray} (not loaded automatically).
1633 This also sets \refKey{csvsim/collect data} since this kind of table
1634 needs collected content, see \Fullref{sec:datacollection}.
1635 Note that \refKey{csvsim/after reading} is set to use the collected
1636 data immediately. See \Fullref{sec:tabularray} for examples.
1637 \end{docCsvKey}
1638
1639
1640 \begin{docCsvKey}[][doc new=2021-07-23]{long tabularray}{=\meta{table format}}{style, no default}
1641 Like \refKey{csvsim/tabularray} but using the |longtblr| environment
1642 from the package \refPkg{tabularray} (not loaded automatically).
1643 \end{docCsvKey}
1644
1645
1646 \begin{docCsvKey}[][doc new=2021-07-06]{centered tabularray}{=\meta{table format}}{style, no default}
1647 Like \refKey{csvsim/tabularray} but inside an additional |center| environment.
1648 \end{docCsvKey}
1649
1650
1651 \begin{docCsvKey}{no table}{}{style, no value}
1652 Deactivates |tabular|-like environments activated by
1653 \refKey{csvsim/tabular}, \refKey{csvsim/longtable}, etc.
1654 Note that not all settings of \refKey{csvsim/tabularray} are reverted.
1655 \end{docCsvKey}
1656
1657
1658 \clearpage
1659 \subsubsection{Additional Options for Tables}\label{subsubsec:table_options}
1660
1661 \begin{docCsvKey}{before table}{=\meta{code}}{no default, initially empty}
1662 Sets the \meta{code} to be executed before the begin of |tabular|-like environments,
1663 i.e. immediately before |\begin{tabular}|, etc.
1664 \end{docCsvKey}
1665
1666
1667 \begin{docCsvKey}{table head}{=\meta{code}}{no default, initially empty}
1668 Sets the \meta{code} to be executed after the begin of |tabular|-like environments,
1669 i.e. immediately after |\begin{tabular}|, etc.
1670 \end{docCsvKey}
1671
1672
1673 \begin{docCsvKey}{table foot}{=\meta{code}}{no default, initially empty}
1674 Sets the \meta{code} to be executed before the end of |tabular|-like environments,
1675 i.e. immediately before |\end{tabular}|, etc.
1676 \end{docCsvKey}
1677
1678
1679 \begin{docCsvKey}{after table}{=\meta{code}}{no default, initially empty}
1680 Sets the \meta{code} to be executed after the end of |tabular|-like environments,
1681 i.e. immediately after |\end{tabular}|, etc.
1682 \end{docCsvKey}
1683
1684
1685 \begin{docCsvKey}[][doc new=2021-09-09]{table centered}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1686 If |true|, the table is put inside an additional |center| environment.
1687 This environment begins before \refKey{csvsim/before table}
1688 and ends after \refKey{csvsim/after table}. The predefined |tabular|-like environments
1689 from Section~\fullref{subsubsec:table_predef} use this option internally,
1690 i.e. \mbox{|centered tabular={ccc}|} is identical to
1691 \mbox{|tabular={ccc}, table centered|}.
1692 \end{docCsvKey}
1693
1694
1695 \clearpage
1696 \subsubsection{Generic Tables}\label{subsubsec:table_generic}
1697 In Section~\fullref{subsubsec:table_predef}, several |tabular|-like environments
1698 are described with predefined keys. The following keys allow to use further
1699 |tabular|-like environments with configurable names and options.
1700
1701
1702 \begin{docCsvKey}[][doc new=2021-09-09]{generic table}{=\meta{name}}{no default, initially empty}
1703 Surrounds the CSV processing with \cs{begin}\marg{name}
1704 at begin and with \cs{end}\marg{name} at end.
1705 Additionally, the commands defined by the key–values of
1706 \refKey{csvsim/before table}, \refKey{csvsim/table head}, \refKey{csvsim/table foot},
1707 and \refKey{csvsim/after table} are executed at the appropriate places.
1708 \refKey{csvsim/late after line} is set to \cs{}\cs{}.\par
1709 If the environment \meta{name} takes options, these have to be set using
1710 \refKey{csvsim/generic table options}.
1711
1712 \begin{dispListing}
1713 % The `tabular` environment would be used like the following example
1714 ...
1715 generic table = tabular,
1716 generic table options = {{ccllrr}},
1717 ...
1718 \end{dispListing}
1719 \end{docCsvKey}
1720
1721
1722 \begin{docCsvKey}[][doc new and updated={2021-09-09}{2023-12-18}]{generic collected table}{=\meta{name}}{no default, initially empty}
1723 Like \refKey{csvsim/generic table} but for environments which need
1724 collected content, e.g. |tblr| from package \refPkg{tabularray}, see \Fullref{sec:datacollection}.
1725 Note that \refKey{csvsim/consume collected data} is set to |true| to
1726 use the collected data immediately.
1727
1728 \begin{dispListing}
1729 % The `tblr` environment from package `tabularray` would be used
1730 % like the following example
1731 ...
1732 generic collected table = tblr,
1733 generic table options = {{rowsep=1mm, colsep=5mm}},
1734 ...
1735 \end{dispListing}
1736 \end{docCsvKey}
1737
1738
1739 \begin{docCsvKey}[][doc new=2021-09-09]{generic table options}{=\marg{code}}{no default, initially empty}
1740 Places \meta{code} immediately after \cs{begin}\marg{name} set up with
1741 \refKey{csvsim/generic table} or \refKey{csvsim/generic collected table}.
1742 \meta{code} may contain any parameters the environment \meta{name} needs to have.
1743 \textbf{\color{red!50!black}You are strongly advised to use an extra pair of
1744 curly brackets \marg{code} around \meta{code}}, because the outer pair of braces is
1745 removed during option processing, see examples above.
1746 \begin{dispListing}
1747 % Environment without parameters:
1748 generic table options =,
1749 % Environment with a mandatory parameter:
1750 generic table options = {{parameter}},
1751 % Environment with an optional and a mandatory parameter:
1752 generic table options = {[optional]{mandatory}},
1753 % Environment with two mandatory parameters:
1754 generic table options = {{mandatory 1}{mandatory 2}},
1755 \end{dispListing}
1756
1757 \end{docCsvKey}
1758
1759
1760 \clearpage
1761 \subsubsection{General Survey Tables}\label{subsubsec:table_survey}
1762
1763 The following |auto| options are the counterparts for the respective quick
1764 overview commands like \refCom{csvautotabular}, see Section~\ref{sec:autotable}.
1765 They are listed for
1766 completeness, but are unlikely to be used directly.
1767
1768 \begin{docCsvKeys}[
1769 doc parameter = {=\meta{file name}},
1770 doc description = no default,
1771 ]
1772 {
1773 { doc name = autotabular, doc updated = {2022-02-01} },
1774 { doc name = autotabular* },
1775 }
1776 Reads the whole CSV file denoted \meta{file name} with an automated formatting.
1777 The star variant treats the first line as data line and not as header line.
1778 \end{docCsvKeys}
1779
1780
1781 \begin{docCsvKeys}[
1782 doc parameter = {=\meta{file name}},
1783 doc description = no default,
1784 ]
1785 {
1786 { doc name = autolongtable, doc updated = {2022-02-01} },
1787 { doc name = autolongtable* },
1788 }
1789 Reads the whole CSV file denoted \meta{file name} with an automated formatting
1790 using the required |longtable| package.
1791 The star variant treats the first line as data line and not as header line.
1792 \end{docCsvKeys}
1793
1794
1795 \begin{docCsvKeys}[
1796 doc parameter = {=\meta{file name}},
1797 doc description = no default,
1798 ]
1799 {
1800 { doc name = autobooktabular, doc updated = {2022-02-01} },
1801 { doc name = autobooktabular* },
1802 }
1803 Reads the whole CSV file denoted \meta{file name} with an automated formatting
1804 using the required |booktabs| package.
1805 The star variant treats the first line as data line and not as header line.
1806 \end{docCsvKeys}
1807
1808
1809 \begin{docCsvKeys}[
1810 doc parameter = {=\meta{file name}},
1811 doc description = no default,
1812 ]
1813 {
1814 { doc name = autobooklongtable, doc updated = {2022-02-01} },
1815 { doc name = autobooklongtable* },
1816 }
1817 Reads the whole CSV file denoted \meta{file name} with an automated formatting
1818 using the required |booktabs| and |longtable| packages.
1819 The star variant treats the first line as data line and not as header line.
1820 \end{docCsvKeys}
1821
1822
1823 \begin{docCsvKeys}[
1824 doc parameter = {=\meta{file name}},
1825 doc description = no default,
1826 doc new = {2023-10-13}
1827 ]
1828 {
1829 { doc name = autotabularray },
1830 { doc name = autotabularray* },
1831 { doc name = autolongtabularray },
1832 { doc name = autolongtabularray* },
1833 }
1834 Reads the whole CSV file denoted \meta{file name} with an automated formatting
1835 using the required |tabularray| package.
1836 \refKey{csvsim/autotabularray} uses the \docAuxEnvironment*{tblr} environment and
1837 \refKey{csvsim/autolongtabularray} uses the \docAuxEnvironment*{longtblr} environment.
1838 The star variants treat the first line as data line and not as header line.
1839 \end{docCsvKeys}
1840
1841
1842 \clearpage
1843 \subsection{Special Characters}\label{subsec:specchar}
1844 Be default, the CSV content is treated like normal \LaTeX\ text, see
1845 Subsection~\ref{macrocodexample} on page~\pageref{macrocodexample}.
1846 For example, \%~can be used to start an in-line comment.
1847 But, \TeX\ special characters of the CSV content may also be interpreted
1848 as normal characters (|\catcode| 12, other), if one or more of the following options are used.
1849
1850 \begin{docCsvKey}{respect tab}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1851 If this key is set, every
1852 tabulator sign
1853 inside the CSV content is a normal character.
1854 \end{docCsvKey}
1855
1856 \begin{docCsvKey}{respect percent}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1857 If this key is set, every
1858 percent sign \verbbox{\%}
1859 inside the CSV content is a normal character.
1860 \end{docCsvKey}
1861
1862 \begin{docCsvKey}{respect sharp}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1863 If this key is set, every
1864 sharp sign \verbbox{\#}
1865 inside the CSV content is a normal character.
1866 \end{docCsvKey}
1867
1868 \begin{docCsvKey}{respect dollar}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1869 If this key is set, every
1870 dollar sign \verbbox{\$}
1871 inside the CSV content is a normal character.
1872 \end{docCsvKey}
1873
1874 \begin{docCsvKey}{respect and}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1875 If this key is set, every
1876 and sign \verbbox{\&}
1877 inside the CSV content is a normal character.
1878 \end{docCsvKey}
1879
1880 \begin{docCsvKey}{respect backslash}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1881 If this key is set, every
1882 backslash sign \verbbox{\textbackslash}
1883 inside the CSV content is a normal character.
1884 \end{docCsvKey}
1885
1886 \begin{docCsvKey}{respect underscore}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1887 If this key is set, every
1888 underscore sign \verbbox{\_}
1889 inside the CSV content is a normal character.
1890 \end{docCsvKey}
1891
1892 \begin{docCsvKey}{respect tilde}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1893 If this key is set, every
1894 tilde sign \verbbox{\textasciitilde}
1895 inside the CSV content is a normal character.
1896 \end{docCsvKey}
1897
1898 \begin{docCsvKey}{respect circumflex}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1899 If this key is set, every
1900 circumflex sign \verbbox{\textasciicircum}
1901 inside the CSV content is a normal character.
1902 \end{docCsvKey}
1903
1904 \begin{docCsvKey}{respect leftbrace}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1905 If this key is set, every
1906 left brace sign \verbbox{\textbraceleft}
1907 inside the CSV content is a normal character.
1908 \end{docCsvKey}
1909
1910 \begin{docCsvKey}{respect rightbrace}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
1911 If this key is set, every
1912 right brace sign \verbbox{\textbraceright}
1913 inside the CSV content is a normal character.
1914 \end{docCsvKey}
1915
1916 \begin{docCsvKey}{respect all}{}{style, no value, initially unset}
1917 Set all special characters from above to normal characters. This means
1918 a quite verbatim interpretation of the CSV content.
1919 \end{docCsvKey}
1920
1921 \begin{docCsvKey}{respect none}{}{style, no value, initially set}
1922 Do not change any special character from above to normal character.
1923 \end{docCsvKey}
1924
1925 \clearpage
1926 \subsection{Separators}\label{sec:separators}%
1927 \begin{docCsvKey}{separator}{=\meta{sign}}{no default, initially |comma|}
1928 \catcode `|=12
1929 Sets the \meta{sign} which is treated as separator between the data values
1930 of a data line. Feasible values are:
1931 \begin{itemize}
1932 \item\docValue{comma}: This is the initial value with '\texttt{,}' as separator.
1933 \medskip
1934
1935 \item\docValue{semicolon}: Sets the separator to '\texttt{;}'.
1936 \begin{dispExample}
1937 % \usepackage{tcolorbox} for tcbverbatimwrite
1938 \begin{tcbverbatimwrite}{testsemi.csv}
1939 name;givenname;matriculation;gender;grade
1940 Maier;Hans;12345;m;1.0
1941 Huber;Anna;23456;f;2.3
1942 Weißbäck;Werner;34567;m;5.0
1943 \end{tcbverbatimwrite}
1944
1945 \csvautobooktabular[separator=semicolon]{testsemi.csv}
1946 \end{dispExample}
1947 \medskip
1948
1949 \item\docValue{pipe}: Sets the separator to '\texttt{|}'.
1950 \begin{dispExample}
1951 % \usepackage{tcolorbox} for tcbverbatimwrite
1952 \begin{tcbverbatimwrite}{pipe.csv}
1953 name|givenname|matriculation|gender|grade
1954 Maier|Hans|12345|m|1.0
1955 Huber|Anna|23456|f|2.3
1956 Weißbäck|Werner|34567|m|5.0
1957 \end{tcbverbatimwrite}
1958
1959 \csvautobooktabular[separator=pipe]{pipe.csv}
1960 \end{dispExample}
1961 \medskip
1962
1963 \item\docValue{tab}: Sets the separator to the tabulator sign.
1964 Automatically, \refKey{csvsim/respect tab} is set also.
1965
1966 \clearpage
1967 \item\docValue{space}:\tcbdocmarginnote{\tcbdocnew{2023-05-08}}
1968 Sets the separator to space(s).
1969 \begin{dispExample}
1970 % \usepackage{tcolorbox} for tcbverbatimwrite
1971 \begin{tcbverbatimwrite}{space.csv}
1972 name givenname matriculation gender grade
1973 Maier Hans 12345 m 1.0
1974 Huber Anna 23456 f 2.3
1975 Weißbäck Werner 34567 m 5.0
1976 \end{tcbverbatimwrite}
1977
1978 \csvautobooktabular[separator=space]{space.csv}
1979 \end{dispExample}
1980 Note that leading spaces are ignored and multiple spaces are treated as one space.
1981 To denote an empty data cell insert \verb+{}+, e.g. \verb*+1 {} 3+.
1982 \end{itemize}
1983 \end{docCsvKey}
1984
1985 \clearpage
1986 \subsection{Miscellaneous}%
1987
1988 \begin{docCsvKey}{every csv}{}{style, initially empty}
1989 A meta key (style) definition which is used for every following CSV file.
1990 This definition can be overwritten with user code.
1991 \begin{dispListing}
1992 % Sets a warning message for unfeasible data lines.
1993 \csvstyle{every csv}{warn on column count error}
1994 \end{dispListing}
1995 \end{docCsvKey}
1996
1997 \begin{docCsvKey}{default}{}{style}
1998 A style definition which is used for every following CSV file which
1999 resets all settings to default values\footnote{\texttt{default} is used
2000 because of the global nature of most settings.}.
2001 This key should not be used or changed by the user if there is not a
2002 really good reason (and you know what you do).
2003 \end{docCsvKey}
2004
2005
2006 \begin{docCsvKey}{file}{=\meta{file name}}{no default, initially |unknown.csv|}
2007 Sets the \meta{file name} of the CSV file to be processed.
2008 \refCom{csvreader} sets this option by a mandatory parameter.
2009 \end{docCsvKey}
2010
2011
2012 \begin{docCsvKey}{preprocessed file}{=\meta{file name}}{no default, initially \texttt{\textbackslash\detokenize{jobname_sorted.csv}}}
2013 Sets the \meta{file name} of the CSV file which is the output of a
2014 preprocessor.
2015 \end{docCsvKey}
2016
2017
2018 \begin{docCsvKey}{preprocessor}{=\meta{macro}}{no default}
2019 Defines a preprocessor for the given CSV file.
2020 The \meta{macro} has to have two mandatory arguments. The first argument
2021 is the original CSV file which is set by \refKey{csvsim/file}.
2022 The second argument is the preprocessed CSV file
2023 which is set by \refKey{csvsim/preprocessed file}.\par\smallskip
2024 Typically, the \meta{macro} may call an external program which preprocesses
2025 the original CSV file (e.\,g. sorting the file) and creates the
2026 preprocessed CSV file. The later file is used by \refCom{csvreader}
2027 or \refCom{csvloop}.
2028 \begin{dispListing}
2029 \newcommand{\mySortTool}[2]{%
2030 % call to an external program to sort file #1 with resulting file #2
2031 }
2032
2033 \csvreader[%
2034 preprocessed file = \jobname_sorted.csv,
2035 preprocessor = \mySortTool,
2036 ]{some.csv}{}{%
2037 % do something
2038 }
2039 \end{dispListing}
2040 See Subsection~\ref{sec:Sorting} on page~\pageref{sec:Sorting} for a
2041 concrete sorting preprocessing implemented with an external tool.
2042 \end{docCsvKey}
2043
2044
2045 \begin{docCsvKey}{no preprocessing}{}{style, no value, initially set}
2046 Clears any preprocessing, i.\,e. preprocessing is switched of.
2047 \end{docCsvKey}
2048
2049
2050
2051 \clearpage
2052 \subsection{Sorting}\label{sec:Sorting}%
2053 \TeX/\LaTeX\ was not born under a sorting planet. |csvsimple-l3| provides no
2054 sorting of data lines by \LaTeX-methods since sorting can be done much faster
2055 and much better by external tools.
2056
2057 First, one should consider the appropriate \emph{place} for sorting:
2058 \begin{itemize}
2059 \item CSV files may be sorted by a tool \emph{before} the \LaTeX\ document is processed
2060 at all. If the CSV data is not likely to change, this is the most efficient method.
2061 \item CSV files may be sorted by a tool every time before the \LaTeX\ document is compiled.
2062 This could be automated by a shell script or some processing tool like |arara|.
2063 \item CSV files may be sorted on-the-fly by a tool during compilation of
2064 a \LaTeX\ document. This is the most elegant but not the most efficient way.
2065 \end{itemize}
2066
2067 The first two methods are decoupled from anything concerning |csvsimple-l3|.
2068 For the third method, the \refKey{csvsim/preprocessor} option is made for.
2069 This allows to access an external tool for sorting.
2070 \emph{Which tool} is your choice.
2071
2072 \csvsorter\ was written as a companion tool for |csvsimple|.
2073 It is an open source Java command-line tool for sorting CSV files, available at\\
2074 \url{https://T-F-S.github.io/csvsorter/}\quad or\quad
2075 \url{https://github.com/T-F-S/csvsorter}
2076
2077 It can be
2078 used for all three sorting approaches described above.
2079 There is special support for on-the-fly sorting with \csvsorter\ using the
2080 following options.
2081
2082 \begin{enumerate}\bfseries
2083 \item To use the sorting options, you have to install \csvsorter\ before!
2084 \item You have to give permission to call external tools during
2085 compilation, i.\,e.\ the command-line options for |latex| have to include
2086 |-shell-escape|.
2087 \end{enumerate}
2088
2089 \bigskip
2090
2091 \begin{docCsvKey}{csvsorter command}{=\meta{system command}}{no default, initially |csvsorter|}
2092 The \meta{system command} specifies the system call for \csvsorter\ (without the options).
2093 If \csvsorter\ was completely installed following its documentation, there is
2094 nothing to change here. If the |csvsorter.jar| file is inside the same
2095 directory as the \LaTeX\ source file, you may configure:% preferrably inside the preamble:
2096 \begin{dispListing}
2097 \csvset{csvsorter command=java -jar csvsorter.jar}
2098 \end{dispListing}
2099 \end{docCsvKey}
2100
2101 \begin{docCsvKey}{csvsorter configpath}{=\meta{path}}{no default, initially |.|}
2102 Sorting with \csvsorter\ is done using XML configuration files. If these files
2103 are not stored inside the same directory as the \LaTeX\ source file, a
2104 \meta{path} to access them can be configured:
2105 \begin{dispListing}
2106 \csvset{csvsorter configpath=xmlfiles}
2107 \end{dispListing}
2108 Here, the configuration files would be stored in a subdirectory named |xmlfiles|.
2109 \end{docCsvKey}
2110
2111 \begin{docCsvKey}{csvsorter log}{=\meta{file name}}{no default, initially |csvsorter.log|}
2112 Sets the log file of \csvsorter\ to the given \meta{file name}.
2113 \begin{dispListing}
2114 \csvset{csvsorter log=outdir/csvsorter.log}
2115 \end{dispListing}
2116 Here, the log file is written to a subdirectory named |outdir|.
2117 \end{docCsvKey}
2118
2119 \clearpage
2120 \begin{docCsvKey}{csvsorter token}{=\meta{file name}}{no default, initially |\textbackslash jobname.csvtoken|}
2121 Sets \meta{file name} as token file. This is an auxiliary file which
2122 communicates the success of \csvsorter\ to |csvsimple|.
2123 \begin{dispListing}
2124 \csvset{csvsorter log=outdir/\jobname.csvtoken}
2125 \end{dispListing}
2126 Here, the token file is written to a subdirectory named |outdir|.
2127 \end{docCsvKey}
2128
2129
2130 \begin{docCsvKey}{sort by}{=\meta{file name}}{style, initially unset}
2131 The \meta{file name} denotes an XML configuration file for \csvsorter.
2132 Setting this option inside \refCom{csvreader} or
2133 \refCom{csvloop} will issue a system call to \csvsorter.
2134 \begin{itemize}
2135 \item \csvsorter\ uses the given CSV file as input file.
2136 \item \csvsorter\ uses \meta{file name} as configuration file.
2137 \item The output CSV file is denoted by \refKey{csvsim/preprocessed file}
2138 which is by default \texttt{\textbackslash\detokenize{jobname_sorted.csv}}.
2139 This output file is this actual file processed by \refCom{csvreader} or \refCom{csvloop}.
2140 \item \csvsorter\ also generates a log file denoted by \refKey{csvsim/csvsorter log} which is by default |csvsorter.log|.
2141 \end{itemize}
2142
2143 \par\medskip\textbf{First example:}
2144 To sort our example |grade.csv| file according to |name| and |givenname|, we
2145 use the following XML configuration file. Since \csvsorter\ uses double quotes
2146 as default brackets for column values, we remove bracket recognition to avoid
2147 a clash with the escaped umlauts of the example CSV file.\par\smallskip
2148
2149 \xmllisting{namesort}
2150 \begin{dispExample}
2151 % \usepackage{booktabs}
2152 \csvreader[
2153 head to column names,
2154 sort by = namesort.xml,
2155 tabular = >{\color{red}}lllll,
2156 table head = \toprule Name & Given Name & Matriculation & Gender & Grade\\\midrule,
2157 table foot = \bottomrule
2158 ]{grade.csv}{}{%
2159 \csvlinetotablerow
2160 }
2161 \end{dispExample}
2162
2163 \clearpage\textbf{Second example:}
2164 To sort our example |grade.csv| file according to |grade|, we
2165 use the following XML configuration file. Further, persons with the same |grade|
2166 are sorted by |name| and |givenname|. Since \csvsorter\ uses double quotes
2167 as default brackets for column values, we remove bracket recognition to avoid
2168 a clash with the escaped umlauts of the example CSV file.\par\smallskip
2169
2170 \xmllisting{gradesort}
2171 \begin{dispExample}
2172 % \usepackage{booktabs}
2173 \csvreader[
2174 head to column names,
2175 sort by = gradesort.xml,
2176 tabular = llll>{\color{red}}l,
2177 table head = \toprule Name & Given Name & Matriculation & Gender & Grade\\\midrule,
2178 table foot = \bottomrule
2179 ]{grade.csv}{}{%
2180 \csvlinetotablerow
2181 }
2182 \end{dispExample}
2183
2184 \clearpage\textbf{Third example:}
2185 To generate a matriculation/grade list, we sort our example |grade.csv| file
2186 using the following XML configuration file.
2187 Again, since \csvsorter\ uses double quotes
2188 as default brackets for column values, we remove bracket recognition to avoid
2189 a clash with the escaped umlauts of the example CSV file.\par\smallskip
2190
2191 \xmllisting{matriculationsort}
2192 \begin{dispExample}
2193 % \usepackage{booktabs}
2194 \csvreader[
2195 head to column names,
2196 sort by = matriculationsort.xml,
2197 tabular = >{\color{red}}ll,
2198 table head = \toprule Matriculation & Grade\\\midrule,
2199 table foot = \bottomrule
2200 ]{grade.csv}{}{%
2201 \matriculation & \grade
2202 }
2203 \end{dispExample}
2204 \end{docCsvKey}
2205
2206
2207 \clearpage
2208 \begin{docCsvKey}{new sorting rule}{=\marg{name}\marg{file name}}{style, initially unset}
2209 This is a convenience option to generate a new shortcut for often used
2210 \refKey{csvsim/sort by} applications. It also adds a more semantic touch.
2211 The new shortcut option is
2212 \tcbox[on line,size=small,colback=white,colframe=red]{|sort by| \meta{name}} which expands to
2213 \tcbox[on line,size=small,colback=white,colframe=red]{|sort by=|\marg{file name}}.\par\medskip
2214
2215 Consider the following example:
2216 \begin{dispExample}
2217 \csvautotabular[sort by=namesort.xml]{grade.csv}
2218 \end{dispExample}
2219 A good place for setting up a new sorting rule would be inside the preamble:
2220
2221 \csvset{new sorting rule={name}{namesort.xml}}
2222 \begin{dispListing}
2223 \csvset{new sorting rule={name}{namesort.xml}}
2224 \end{dispListing}
2225
2226 Now, we can use the new rule:
2227 \begin{dispExample}
2228 \csvautotabular[sort by name]{grade.csv}
2229 \end{dispExample}
2230 \end{docCsvKey}
2231
2232
2233 \begin{docCommand}[doc new=2021-06-28]{csvsortingrule}{\marg{name}\marg{file name}}
2234 Identical in function to \refKey{csvsim/new sorting rule}, see above.
2235 A good place for setting up a new sorting rule would be inside the preamble:
2236
2237 \csvsortingrule{name}{namesort.xml}
2238 \begin{dispListing}
2239 \csvsortingrule{name}{namesort.xml}
2240 \end{dispListing}
2241
2242 Now, we can use the new rule:
2243 \begin{dispExample}
2244 \csvautotabular[sort by name]{grade.csv}
2245 \end{dispExample}
2246 \end{docCommand}
2247
2248
2249
2250 \clearpage
2251 \subsection{Data Collection}\label{sec:datacollection}
2252
2253 |csvsimple-l3| reads and processes a CSV file line by line. Accordingly, the \TeX{}
2254 input stream is filled line by line.
2255 Although this is an efficient procedure, for some applications like tables with
2256 the \refPkg{tabularray} package, collecting the data from the CSV file into a macro is needed.
2257 This macro can be given to the target application for further processing.
2258
2259
2260 \begin{docCsvKey}[][doc new and updated={2021-07-06}{2023-10-17}]{collect data}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
2261 |csvsimple-l3| provides limited and experimental support to collect the input data
2262 from the CSV file plus user additions into a macro named \refCom{csvdatacollection}.
2263 Setting \refKey{csvsim/collect data} adds the contents of the following keys
2264 to \refCom{csvdatacollection}:
2265 \begin{itemize}
2266 \item\refKey{csvsim/after head}
2267 \item\refKey{csvsim/after first line}
2268 \item\refKey{csvsim/after line}
2269 \item\refKey{csvsim/before first line}
2270 \item\refKey{csvsim/before line}
2271 \item\refKey{csvsim/late after first line}
2272 \item\refKey{csvsim/late after head}
2273 \item\refKey{csvsim/late after last line}
2274 \item\refKey{csvsim/late after line}
2275 \end{itemize}
2276 Also, the \emph{expanded} content of
2277 \begin{itemize}
2278 \item\refKey{csvsim/command}
2279 \end{itemize}
2280 is added to \refCom{csvdatacollection}
2281 (depending on \refKey{csvsim/consume collected data} and \refKey{csvsim/data collection}).
2282 Note that for \refKey{csvsim/command} special care has to be taken
2283 \emph{what} should be protected from expansion and \emph{what not}.
2284 Observe the following hints for \refKey{csvsim/command}:
2285 \begin{itemize}
2286 \item For data macros like |\csvcoli| use |\csvexpval\csvcoli| to add
2287 the \emph{value} of this macro to \refCom{csvdatacollection}.
2288 This is optional, if |\csvcoli| contains numbers or text without active
2289 characters, but essential, if it contains macros.
2290 \item \refCom{csvlinetotablerow} is to be used \emph{without} |\csvexpval|.
2291 \item For macros like |\textbf| use |\csvexpnot\textbf| to \emph{prevent}
2292 expansion.
2293 \item Using computations or not expandable conditionals may likely cause
2294 compilation errors.
2295 \end{itemize}
2296
2297 \begin{dispExample}
2298 \csvreader[
2299 collect data,
2300 head to column names,
2301 late after line=\\,
2302 late after last line=,
2303 ]{grade.csv}{}{%
2304 \thecsvrow. \csvexpval\givenname\ \csvexpnot\textbf{\csvexpval\name}
2305 }
2306 Collected data:\par
2307 \csvdatacollection
2308 \end{dispExample}
2309
2310 Note that data collection is \emph{limited} to some special cases and does not
2311 allow to save all possible content. Table options like \refKey{csvsim/longtable}
2312 are generally not supported with the important exception of \refKey{csvsim/tabularray}
2313 which uses \refKey{csvsim/collect data} automatically.\par
2314 See \Fullref{sec:tabularray} for examples.
2315 \end{docCsvKey}
2316
2317 \clearpage
2318
2319 \begin{docCsvKey}[][doc new={2023-12-18}]{consume collected data}{\colOpt{=true\textbar false}}{default |true|, initially |false|}
2320 If set to |false|, the collected data of a CSV file processed with
2321 \refKey{csvsim/collect data} is saved into \refCom{csvdatacollection}.\par
2322 Otherwise, if set to |true|, the collected data is not saved, but directly used
2323 after reading the CSV file, see \refKey{csvsim/generic collected table}.
2324 After usage, the collected data is cleared, i.e. \refCom{csvdatacollection} is emptied.
2325 \end{docCsvKey}
2326
2327
2328 \begin{docCsvKey}[][doc new and updated={2021-07-06}{2024-05-16}]{data collection}{=\meta{macro}}{no default, initially \refCom{csvdatacollection}}
2329 Sets the collection macro to an alternative for \refCom{csvdatacollection}.
2330 \begin{dispListing}
2331 data collection = \myData, % instead of \csvdatacollection
2332 \end{dispListing}
2333 Note that until version 2.6.0 (2024/01/19), \refKey{csvsim/data collection} was
2334 not reset to the default \refCom{csvdatacollection} for following CSV files,
2335 but it is now.
2336 \end{docCsvKey}
2337
2338
2339 \begin{docCommand}[doc new=2021-07-06]{csvdatacollection}{}
2340 Macro which contains the collected data of a CSV file processed with
2341 \refKey{csvsim/collect data}. This macro name can be changed by
2342 setting \refKey{csvsim/data collection}.
2343 \end{docCommand}
2344
2345
2346 \begin{docCommand}[doc new and updated={2021-07-06}{2023-12-17}]{csvexpval}{\meta{macro}}
2347 Recovers the content of the given \meta{macro} and prevents further
2348 expansion. This is a wrapper for \docAuxCommand*{exp_not:o}.
2349 Alternatively, |\expandonce| from \refPkg{etoolbox} could be used.
2350 \end{docCommand}
2351
2352
2353 \begin{docCommand}[doc new=2021-07-06]{csvexpnot}{\meta{macro}}
2354 Prevents the expansion of the given \meta{macro}. This is a wrapper
2355 for \docAuxCommand*{exp_not:N}.
2356 Alternatively, |\noexpand| could be used.
2357 \end{docCommand}
2358
2359 The following macros can only be used inside keys which are \emph{not}
2360 collected to \refCom{csvdatacollection}, e.g. inside \refKey{csvsim/after filter}.
2361
2362 \begin{docCommand}[doc new and updated={2021-07-06}{2023-12-17}]{csvcollectn}{\marg{code}}
2363 Appends the given \meta{code} to \refCom{csvdatacollection}.\\
2364 This corresponds to \docAuxCommand*{tl_build_gput_right:Nn}.
2365 \end{docCommand}
2366
2367
2368 \begin{docCommands}[
2369 doc parameter = \marg{code}
2370 ]
2371 {
2372 { doc name = csvcollecte, doc new and updated = {2021-07-06}{2023-12-18} },
2373 { doc name = csvcollectx }
2374 }
2375 Appends the expansion of the given \meta{code} to \refCom{csvdatacollection}.\\
2376 This corresponds to \docAuxCommand*{tl_build_gput_right:Ne}.\\
2377 \refCom{csvcollectx} is an alias for \refCom{csvcollecte} and is kept for backward compatibility.
2378 \end{docCommands}
2379
2380 \begin{docCommand}[doc new and updated={2021-07-06}{2023-12-17}]{csvcollectV}{\meta{macro}}
2381 Appends the content of the given \meta{macro} to \refCom{csvdatacollection}.\\
2382 This corresponds to \docAuxCommand*{tl_build_gput_right:Ne} and \docAuxCommand*{exp_not:o} for
2383 \meta{macro}.
2384 \end{docCommand}
2385
2386
2387 \clearpage
2388 \section{String and Number Tests}\label{sec:stringtests}%
2389
2390 The following string and number tests are, to some extent, provided for
2391 backward compatibility.
2392 Mainly, they are wrappers for corresponding |expl3| conditionals.
2393 Therefore, you are encouraged to use the following CamelCase macros
2394 like \refCom{IfCsvsimStrEqualTF} which provide by their name insight
2395 to the underlying |expl3| functions. The lowercase variants are kept
2396 for backward compatibility.
2397 \medskip
2398
2399
2400 \begin{docCommands}[
2401 doc parameter = \marg{string A}\marg{string B}\marg{true}\marg{false}
2402 ]
2403 {
2404 { doc name = IfCsvsimStrEqualTF, doc new and updated = {2016-07-01}{2023-12-19} },
2405 { doc name = ifcsvstrcmp, color command=black }
2406 }
2407 Compares two strings and executes \meta{true} if they are equal, and \meta{false} otherwise.
2408 The comparison is done using |\str_if_eq:eeTF|.
2409 \refCom{IfCsvsimStrEqualTF} is expandable.
2410 Typically, this is the preferred function for many use cases.
2411 \end{docCommands}
2412
2413
2414 \begin{docCommand}[doc new and updated={2016-07-01}{2021-06-28},color command=black]{ifcsvnotstrcmp}{\marg{string A}\marg{string B}\marg{true}\marg{false}}
2415 Compares two strings and executes \meta{true} if they are \emph{not} equal, and \meta{false} otherwise.
2416 The implementation uses \refCom{IfCsvsimStrEqualTF}.
2417 \refCom{ifcsvnotstrcmp} is expandable.
2418 Consider using \refCom{IfCsvsimStrEqualTF} alternatively.
2419 \end{docCommand}
2420
2421
2422 \begin{docCommands}[
2423 doc parameter = \marg{token list A}\marg{token list B}\marg{true}\marg{false}
2424 ]
2425 {
2426 { doc name = IfCsvsimTlEqualTF, doc new and updated = {2016-07-01}{2023-12-19} },
2427 { doc name = ifcsvstrequal, color command=black }
2428 }
2429 Compares two token lists and executes \meta{true} if they are equal, and \meta{false} otherwise.
2430 The comparison is done using |\tl_if_eq:eeTF|.
2431 \refCom{IfCsvsimTlEqualTF} is not expandable.
2432 If you have no special reason for using a token list comparison, where
2433 characters and category codes of those characters are compared, you may
2434 rather choose \refCom{IfCsvsimStrEqualTF}.
2435 \end{docCommands}
2436
2437
2438
2439 \begin{docCommands}[
2440 doc parameter = \marg{token list A}\marg{token list B}\marg{true}\marg{false}
2441 ]
2442 {
2443 { doc name = IfCsvsimTlProtectedEqualTF, doc new and updated = {2016-07-01}{2023-12-19} },
2444 { doc name = ifcsvprostrequal, color command=black }
2445 }
2446 Compares two token lists and executes \meta{true} if they are equal, and \meta{false} otherwise.
2447 The token lists are expanded with |\protected@edef|
2448 in the test, i.e. parts of the
2449 token lists which are protected stay unexpanded.
2450 The comparison is done using |\tl_if_eq:NNTF|.
2451 \refCom{IfCsvsimTlProtectedEqualTF} is not expandable.
2452 \end{docCommands}
2453
2454
2455 \begin{docCommands}[
2456 doc parameter = \marg{floating point comparison}\marg{true}\marg{false}
2457 ]
2458 {
2459 { doc name = IfCsvsimFpCompareTF, doc new and updated = {2021-06-28}{2023-12-19} },
2460 { doc name = ifcsvfpcmp, color command=black }
2461 }
2462 Evaluates the given \meta{floating point comparison}
2463 and executes \meta{true} or \meta{false} appropriately.
2464 The evaluation is done using \docAuxCommand*{fp_compare:nTF}.\\
2465 Basically, a \meta{floating point comparison} consists of
2466 \mbox{\meta{fp expr\textsubscript{1}} \meta{relation} \meta{fp expr\textsubscript{2}}},
2467 like \mbox{$x<y$}, but \docAuxCommand*{fp_compare:nTF} even allows a chain of comparisons.
2468 \refCom{IfCsvsimFpCompareTF} is expandable.
2469 \end{docCommands}
2470
2471
2472 \begin{docCommands}[
2473 doc parameter = \marg{integer comparison}\marg{true}\marg{false}
2474 ]
2475 {
2476 { doc name = IfCsvsimIntCompareTF, doc new and updated = {2021-06-28}{2023-12-19} },
2477 { doc name = ifcsvintcmp, color command=black }
2478 }
2479 Evaluates the given \meta{integer comparison}
2480 and executes \meta{true} or \meta{false} appropriately.
2481 The evaluation is done using \docAuxCommand*{int_compare:nTF}.\\
2482 Basically, a \meta{integer comparison} consists of
2483 \mbox{\meta{int expr\textsubscript{1}} \meta{relation} \meta{int expr\textsubscript{2}}},
2484 like \mbox{$x<y$}, but \docAuxCommand*{int_compare:nTF} even allows a chain of comparisons.
2485 \refCom{IfCsvsimIntCompareTF} is expandable.
2486 \end{docCommands}
2487
2488
2489
2490 \clearpage
2491 \section{Hooks}\label{sec:hooks}%
2492 The following hook(s) are present following \LaTeX's hook management.
2493
2494 \begin{description}
2495 \item[\textcolor{DarkViolet}{\ttfamily csvsimple/csvline}]
2496 \tcbdocmarginnote{\tcbdocnew{2023-05-08}}
2497 This hook adds code after reading
2498 a line into \refCom{csvline} and before processing this line.
2499 The token list \refCom{csvline} may be manipulated with a global assignment.\par
2500 The following example replaces every \verb+"..."+ by \verb+{...}+ to
2501 approximate double-quote processing within \LaTeX. Still, masking of double-quotes
2502 or nesting will not work.
2503 \begin{dispListing}
2504 \AddToHook{csvsimple/csvline}
2505 {
2506 \tl_set_eq:NN \l_tmpa_tl \csvline
2507 \regex_replace_all:nnN { "([^"]+)" } { {\1} } \l_tmpa_tl
2508 \tl_gset_eq:NN \csvline \l_tmpa_tl
2509 }
2510 \end{dispListing}
2511
2512 \end{description}
2513
2514
2515 \clearpage
2516 \section{Examples}%
2517
2518 \subsection{A Serial Letter}%
2519 In this example, a serial letter is to be written to all persons with
2520 addresses from the following CSV file. Deliberately, the file content is
2521 not given in very pretty format.
2522
2523 %-- file embedded for simplicity --
2524 \begin{tcbverbatimwrite}{address.csv}
2525 name,givenname,gender,degree,street,zip,location,bonus
2526 Maier,Hans,m,,Am Bachweg 17,10010,Hopfingen,20
2527 % next line with a comma in curly braces
2528 Huber,Erna,f,Dr.,{Moosstraße 32, Hinterschlag},10020,Örtingstetten,30
2529 Weißbäck,Werner,m,Prof. Dr.,Brauallee 10,10030,Klingenbach,40
2530 % this line is ignored %
2531 Siebener , Franz,m, , Blaumeisenweg 12 , 10040 , Pardauz , 50
2532 % preceding and trailing spaces in entries are removed %
2533 Schmitt,Anton,m,,{\AE{}lfred-Esplanade, T\ae{}g 37}, 10050,\OE{}resung,60
2534 \end{tcbverbatimwrite}
2535 %-- end embedded file --
2536
2537 \csvlisting{address}
2538
2539 Firstly, we survey the file content quickly using
2540 |\csvautotabular|.
2541 As can be seen, unfeasible lines are ignored automatically.
2542
2543 \begin{dispExample}
2544 \tiny\csvautotabular{address.csv}
2545 \end{dispExample}
2546
2547 Now, we create the serial letter where every feasible data line produces
2548 an own page. Here, we simulate the page by a |tcolorbox| (from the package
2549 |tcolorbox|).
2550 For the gender specific salutations, an auxiliary macro |\ifmale| is
2551 introduced.
2552
2553 \begin{dispExample}
2554 % this example requires the tcolorbox package
2555 \newcommand{\ifmale}[2]{\IfCsvsimStrEqualTF{\gender}{m}{#1}{#2}}
2556
2557 \csvreader[head to column names]{address.csv}{}{%
2558 \begin{tcolorbox}[colframe=DarkGray,colback=White,arc=0mm,width=(\linewidth-2pt)/2,
2559 equal height group=letter,before=,after=\hfill,fonttitle=\bfseries,
2560 adjusted title={Letter to \name}]
2561 \IfCsvsimStrEqualTF{\degree}{}{\ifmale{Mr.}{Ms.}}{\degree}~\givenname~\name\\
2562 \street\\\zip~\location
2563 \tcblower
2564 {\itshape Dear \ifmale{Sir}{Madam},}\\
2565 we are pleased to announce you a bonus value of \bonus\%{}
2566 which will be delivered to \location\ soon.\\\ldots
2567 \end{tcolorbox}}
2568 \end{dispExample}
2569
2570
2571
2572 \clearpage
2573 \subsection{A Graphical Presentation}\label{sec:examgrapghpres}%
2574 For this example, we use some artificial statistical data given by a CSV file.
2575
2576 %-- file embedded for simplicity --
2577 \begin{tcbverbatimwrite}{data.csv}
2578 land,group,amount
2579 Bayern,A,1700
2580 Baden-Württemberg,A,2300
2581 Sachsen,B,1520
2582 Thüringen,A,1900
2583 Hessen,B,2100
2584 \end{tcbverbatimwrite}
2585 %-- end embedded file --
2586
2587 \csvlisting{data}
2588
2589 Firstly, we survey the file content using
2590 |\csvautobooktabular|.
2591
2592 \begin{dispExample}
2593 % needs the booktabs package
2594 \csvautobooktabular{data.csv}
2595 \end{dispExample}
2596
2597 The amount values are presented in the following diagram by bars where
2598 the group classification is given using different colors.
2599
2600 \begin{dispExample}
2601 % This example requires the package tikz
2602 \begin{tikzpicture}[Group/A/.style={left color=red!10,right color=red!20},
2603 Group/B/.style={left color=blue!10,right color=blue!20}]
2604 \csvreader[head to column names]{data.csv}{}{%
2605 \begin{scope}[yshift=-\thecsvrow cm]
2606 \path [draw,Group/\group] (0,-0.45)
2607 rectangle node[font=\bfseries] {\amount} (\amount/1000,0.45);
2608 \node[left] at (0,0) {\land};
2609 \end{scope} }
2610 \end{tikzpicture}
2611 \end{dispExample}
2612
2613
2614 \clearpage
2615 It would be nice to sort the bars by length, i.\,e.\ to sort the CSV file
2616 by the |amount| column. If the \csvsorter\ program is properly installed,
2617 see Subsection~\ref{sec:Sorting} on page~\pageref{sec:Sorting},
2618 this can be done with the following configuration file for \csvsorter:
2619
2620 \xmllisting{amountsort}
2621
2622 Now, we just have to add an option |sort by=amountsort.xml|:
2623 \begin{dispExample}
2624 % This example requires the package tikz
2625 % Also, the CSV-Sorter tool has to be installed
2626 \begin{tikzpicture}[Group/A/.style={left color=red!10,right color=red!20},
2627 Group/B/.style={left color=blue!10,right color=blue!20}]
2628 \csvreader[head to column names,sort by=amountsort.xml]{data.csv}{}{%
2629 \begin{scope}[yshift=-\thecsvrow cm]
2630 \path [draw,Group/\group] (0,-0.45)
2631 rectangle node[font=\bfseries] {\amount} (\amount/1000,0.45);
2632 \node[left] at (0,0) {\land};
2633 \end{scope} }
2634 \end{tikzpicture}
2635 \end{dispExample}
2636
2637
2638
2639
2640 \clearpage
2641 Next, we create a pie chart by calling |\csvreader| twice.
2642 In the first step, the total sum of amounts is computed, and in the second
2643 step the slices are drawn.
2644
2645 \begin{dispExample}
2646 % Modified example from www.texample.net for pie charts
2647 % This example needs the packages tikz, xcolor, calc
2648 \definecolorseries{myseries}{rgb}{step}[rgb]{.95,.85,.55}{.17,.47,.37}
2649 \resetcolorseries{myseries}%
2650
2651 % a pie slice
2652 \newcommand{\slice}[4]{
2653 \pgfmathsetmacro{\midangle}{0.5*#1+0.5*#2}
2654 \begin{scope}
2655 \clip (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;
2656 \colorlet{SliceColor}{myseries!!+}%
2657 \fill[inner color=SliceColor!30,outer color=SliceColor!60] (0,0) circle (1cm);
2658 \end{scope}
2659 \draw[thick] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;
2660 \node[label=\midangle:#4] at (\midangle:1) {};
2661 \pgfmathsetmacro{\temp}{min((#2-#1-10)/110*(-0.3),0)}
2662 \pgfmathsetmacro{\innerpos}{max(\temp,-0.5) + 0.8}
2663 \node at (\midangle:\innerpos) {#3};
2664 }
2665
2666 % sum of amounts
2667 \csvreader[before reading=\def\mysum{0}]{data.csv}{amount=\amount}{%
2668 \pgfmathsetmacro{\mysum}{\mysum+\amount}%
2669 }
2670
2671 % drawing of the pie chart
2672 \begin{tikzpicture}[scale=3]%
2673 \def\mya{0}\def\myb{0}
2674 \csvreader[head to column names]{data.csv}{}{%
2675 \let\mya\myb
2676 \pgfmathsetmacro{\myb}{\myb+\amount}
2677 \slice{\mya/\mysum*360}{\myb/\mysum*360}{\amount}{\land}
2678 }
2679 \end{tikzpicture}%
2680 \end{dispExample}
2681
2682
2683 \clearpage
2684 Finally, the filter option is demonstrated by separating the groups A and B.
2685 Every item is piled upon the appropriate stack.
2686
2687 \begin{dispExample}
2688 \newcommand{\drawGroup}[2]{%
2689 \def\mya{0}\def\myb{0}
2690 \node[below=3mm] at (2.5,0) {\bfseries Group #1};
2691 \csvreader[head to column names,filter equal={\group}{#1}]{data.csv}{}{%
2692 \let\mya\myb
2693 \pgfmathsetmacro{\myb}{\myb+\amount}
2694 \path[draw,top color=#2!25,bottom color=#2!50]
2695 (0,\mya/1000) rectangle node{\land\ (\amount)} (5,\myb/1000);
2696 }}
2697
2698 \begin{tikzpicture}
2699 \fill[gray!75] (-1,0) rectangle (13,-0.1);
2700 \drawGroup{A}{red}
2701 \begin{scope}[xshift=7cm]
2702 \drawGroup{B}{blue}
2703 \end{scope}
2704 \end{tikzpicture}
2705
2706 \end{dispExample}
2707
2708
2709 \clearpage
2710 \subsection{Macro code inside the data}\label{macrocodexample}%
2711
2712 If needed, the data file may contain macro code.
2713
2714 %-- file embedded for simplicity --
2715 \begin{tcbverbatimwrite}{macrodata.csv}
2716 type,description,content
2717 M,A nice \textbf{formula}, $\displaystyle \int\frac{1}{x} = \ln|x|+c$
2718 G,A \textcolor{red}{colored} ball, {\tikz \shadedraw [shading=ball] (0,0) circle (.5cm);}
2719 M,\textbf{Another} formula, $\displaystyle \lim\limits_{n\to\infty} \frac{1}{n}=0$
2720 \end{tcbverbatimwrite}
2721 %-- end embedded file --
2722
2723 \csvlisting{macrodata}
2724
2725 Firstly, we survey the file content using
2726 |\csvautobooktabular|.
2727
2728 \begin{dispExample}
2729 \csvautobooktabular{macrodata.csv}
2730 \end{dispExample}
2731
2732
2733 \begin{dispExample}
2734 \csvstyle{my enumerate}{head to column names,
2735 before reading=\begin{enumerate},after reading=\end{enumerate}}
2736
2737 \csvreader[my enumerate]{macrodata.csv}{}{%
2738 \item \description:\par\content}
2739
2740 \bigskip
2741 Now, formulas only:
2742 \csvreader[my enumerate,filter strcmp={\type}{M}]{macrodata.csv}{}{%
2743 \item \description:\qquad\content}
2744 \end{dispExample}
2745
2746 \clearpage
2747 \subsection{Tables with Number Formatting}\label{numberformatting}%
2748
2749 We consider a file with numerical data which should be pretty-printed.
2750
2751 %-- file embedded for simplicity --
2752 \begin{tcbverbatimwrite}{data_numbers.csv}
2753 month, dogs, cats
2754 January, 12.50,12.3e5
2755 February, 3.32, 8.7e3
2756 March, 43, 3.1e6
2757 April, 0.33, 21.2e4
2758 May, 5.12, 3.45e6
2759 June, 6.44, 6.66e6
2760 July, 123.2,7.3e7
2761 August, 12.3, 5.3e4
2762 September,2.3, 4.4e4
2763 October, 6.5, 6.5e6
2764 November, 0.55, 5.5e5
2765 December, 2.2, 3.3e3
2766 \end{tcbverbatimwrite}
2767
2768 \csvlisting{data_numbers}
2769
2770 \medskip
2771
2772 The \refPkg{siunitx} package provides a huge amount of formatting options for
2773 numbers. A good and robust way to apply formatting by \refPkg{siunitx} inside
2774 tables generated by |csvsimple-l3| is the |\tablenum| macro from
2775 \refPkg{siunitx}.
2776
2777 \begin{dispExample}
2778 % \usepackage{siunitx,array,booktabs}
2779 \csvreader[
2780 head to column names,
2781 before reading = \begin{center}\sisetup{table-number-alignment=center},
2782 tabular = cc,
2783 table head = \toprule \textbf{Cats} & \textbf{Dogs} \\\midrule,
2784 table foot = \bottomrule,
2785 after reading = \end{center}
2786 ]{data_numbers.csv}{}{%
2787 \tablenum[table-format=2.2e1]{\cats} & \tablenum{\dogs}
2788 }
2789 \end{dispExample}
2790
2791 \clearpage
2792
2793 It is also possible to create on-the-fly tables using calculations of
2794 the given data. The following example shows cat values bisected and
2795 dog values doubled.
2796
2797 \begin{dispExample}
2798 % \usepackage{siunitx,array,booktabs,xfp}
2799 \csvreader[
2800 head to column names,
2801 before reading = \begin{center}\sisetup{table-number-alignment=center},
2802 tabular = cccc,
2803 table head = \toprule \textbf{Cats} & \textbf{Dogs}
2804 & \textbf{Halfcats} & \textbf{Doubledogs} \\\midrule,
2805 table foot = \bottomrule,
2806 after reading = \end{center}
2807 ]{data_numbers.csv}{}{%
2808 \tablenum[table-format=2.2e1]{\cats} & \tablenum{\dogs}
2809 & \tablenum[exponent-mode=scientific, round-precision=3,
2810 round-mode=places, table-format=1.3e1]{\fpeval{\cats/2}}
2811 & \tablenum{\fpeval{\dogs*2}}
2812 }
2813 \end{dispExample}
2814
2815
2816 \clearpage
2817
2818 The |siunitx| package also provides a new column type |S|
2819 which can align material using a number of different strategies.
2820 Special care is needed, if the \emph{first} or the \emph{last} column is to be formatted with
2821 the column type |S|. The number detection of |siunitx| is disturbed by
2822 the line reading code of |csvsimple-l3| which actually is present at the
2823 first and last column. To avoid this problem, the utilization of
2824 |\tablenum| is appropriate, see above.
2825 Alternatively, a very nifty workaround suggested by Enrico Gregorio is to
2826 add an invisible dummy column with |c@{}| as first column
2827 and |@{}c| as last column:
2828
2829 \begin{dispExample}
2830 % \usepackage{siunitx,array,booktabs}
2831 \csvreader[
2832 head to column names,
2833 before reading = \begin{center}\sisetup{table-number-alignment=center},
2834 tabular = {c@{}S[table-format=2.2e1]S@{}c},
2835 table head = \toprule & \textbf{Cats} & \textbf{Dogs} & \\\midrule,
2836 table foot = \bottomrule,
2837 after reading = \end{center}
2838 ]{data_numbers.csv}{}{%
2839 & \cats & \dogs &
2840 }
2841 \end{dispExample}
2842
2843
2844
2845
2846 \clearpage
2847 Now, the preceding table shall be sorted by the \emph{cats} values.
2848 If the \csvsorter\ program is properly installed,
2849 see Subsection~\ref{sec:Sorting} on page~\pageref{sec:Sorting},
2850 this can be done with the following configuration file for \csvsorter:
2851
2852 \xmllisting{catsort}
2853
2854 Now, we just have to add an option |sort by=catsort.xml|:
2855 \begin{dispExample}
2856 % \usepackage{siunitx,array,booktabs}
2857 % Also, the CSV-Sorter tool has to be installed
2858 \csvreader[
2859 head to column names,
2860 sort by = catsort.xml,
2861 before reading = \begin{center}\sisetup{table-number-alignment=center},
2862 tabular = lcc,
2863 table head = \toprule \textbf{Month} & \textbf{Dogs} & \textbf{Cats} \\\midrule,
2864 table foot = \bottomrule,
2865 after reading = \end{center}
2866 ]{data_numbers.csv}{}{%
2867 \month & \tablenum{\dogs} & \tablenum[table-format=2.2e1]{\cats}
2868 }
2869 \end{dispExample}
2870
2871
2872 \clearpage
2873 \subsection{CSV data without header line}\label{noheader}%
2874 CSV files with a header line are more semantic than files without header,
2875 but it's no problem to work with headless files.
2876
2877 For this example, we use again some artificial statistical data given by a CSV file
2878 but this time without header.
2879
2880 %-- file embedded for simplicity --
2881 \begin{tcbverbatimwrite}{data_headless.csv}
2882 Bayern,A,1700
2883 Baden-Württemberg,A,2300
2884 Sachsen,B,1520
2885 Thüringen,A,1900
2886 Hessen,B,2100
2887 \end{tcbverbatimwrite}
2888 %-- end embedded file --
2889
2890 \csvlisting{data_headless}
2891
2892 Note that you cannot use the \refKey{csvsim/no head} option for the auto tabular
2893 commands.
2894 If no options are given, the first line is interpreted as header line
2895 which gives an unpleasant result:
2896
2897 \begin{dispExample}
2898 \csvautobooktabular{data_headless.csv}
2899 \end{dispExample}
2900
2901 To get the expected result, the \emph{star} versions of the auto tabular
2902 commands can be used.
2903
2904 \begin{dispExample}
2905 \csvautobooktabular*{data_headless.csv}
2906 \end{dispExample}
2907
2908 This example can be extended to insert a table head for this headless data:
2909
2910 \begin{dispExample}
2911 \csvautobooktabular*[
2912 table head=\toprule\bfseries Land & \bfseries Group
2913 & \bfseries Amount\\\midrule
2914 ]{data_headless.csv}
2915 \end{dispExample}
2916
2917
2918 \clearpage
2919
2920 For the normal \refCom{csvreader} command, the \refKey{csvsim/no head} option
2921 should be applied. Of course, we cannot use \refKey{csvsim/head to column names}
2922 because there is no head, but the columns can be addressed by their numbers:
2923
2924 \begin{dispExample}
2925 \csvreader[
2926 no head,
2927 tabular = lr,
2928 table head = \toprule\bfseries Land & \bfseries Amount\\\midrule,
2929 table foot = \bottomrule]
2930 {data_headless.csv}
2931 { 1=\land, 3=\amount }
2932 {\land & \amount}
2933 \end{dispExample}
2934
2935
2936
2937 \clearpage
2938 \subsection{Tables with \texttt{tabularray}}\label{sec:tabularray}%
2939
2940 The \refPkg{tabularray} package gives extended control for generating
2941 tables. \refKey{csvsim/tabularray} and \refKey{csvsim/centered tabularray}
2942 support such tables. A distinctiveness is that for \refPkg{tabularray}
2943 data from a CSV file has to be \emph{collected} first (into a macro)
2944 and applied afterwards. The process is hidden from the user view, but
2945 has to be taken into account when \refKey{csvsim/command} is set up,
2946 see \Fullref{sec:datacollection}.
2947
2948 The following examples uses |data.csv| from \Fullref{sec:examgrapghpres}.
2949
2950 \begin{dispExample}
2951 % \usepackage{tabularray,siunitx,xfp}
2952 \csvreader[
2953 head to column names,
2954 centered tabularray =
2955 {
2956 rowsep = 1mm,
2957 colsep = 5mm,
2958 rows = {blue7},
2959 hlines = {2pt, white},
2960 vlines = {2pt, white},
2961 row{1} = {bg=azure3, fg=white, font=\bfseries\large, 8mm},
2962 },
2963 table head = {\SetCell[c=4]{c} Important Data Table \\},
2964 ]{data.csv}{}{
2965 \IfCsvsimStrEqualTF{\group}{A}{\csvexpnot\SetRow{brown7}}{}
2966 \csvexpnot\SetCell{bg=purple7}
2967 \csvexpval\land
2968 & \csvexpval\group
2969 & \csvexpval\amount
2970 & \tablenum[exponent-mode=scientific, round-precision=3,
2971 round-mode=places, table-format=1.3e1]{\fpeval{pi*\amount}}
2972 }
2973 \end{dispExample}
2974
2975 Note in the example above that
2976 \begin{itemize}
2977 \item \refKey{csvsim/table head} is \emph{collected} unexpanded, i.e.
2978 |\SetCell| has not to be protected. On the other hand, CSV data could not
2979 be used here.
2980 \item \refKey{csvsim/command} is \emph{collected} expanded. This is identical
2981 to the mandatory last argument of \refCom{csvreader}.
2982 \begin{itemize}
2983 \item Therefore, expansion of |\SetRow|, |\SetCell|, etc. is prevented by \refCom{csvexpnot}.
2984 \item The \emph{values} (content) of |\land|, |\group|, etc. are recovered by
2985 \refCom{csvexpval}.
2986 \item |\IfCsvsimStrEqualTF| and |\fpeval| are \emph{expandable} and therefore the
2987 results of these commands are \emph{collected}.
2988 \item |\tablenum| from \refPkg{siunitx} is a robust command and therefore
2989 needs no protection. If you are not sure, if a command is robust or not, it
2990 does not hurt add the prefix \refCom{csvexpnot}, i.e. use |\csvexpnot\tablenum|.
2991 \end{itemize}
2992 \end{itemize}
2993
2994 \clearpage
2995
2996 Filters and line ranges can be used for \refPkg{tabularray} and all
2997 data collections without restriction:
2998
2999 \begin{dispExample}
3000 % \usepackage{tabularray}
3001 Display group `A` only:\par
3002 \csvreader[
3003 head to column names,
3004 filter strcmp = {\group}{A},
3005 centered tabularray =
3006 {
3007 rowsep = 1mm,
3008 colsep = 5mm,
3009 column{1} = {r, fg=yellow5, colsep=2pt},
3010 column{2} = {r, yellow8!10, font=\bfseries},
3011 column{3} = {l, yellow8},
3012 hlines = {2pt, white},
3013 },
3014 ]{data.csv}{}{
3015 \thecsvrow
3016 & \csvexpval\land
3017 & \csvexpval\amount
3018 }
3019 \end{dispExample}
3020
3021
3022 \begin{dispExample}
3023 % \usepackage{tabularray}
3024 Display data from line 3 on:\par
3025 \csvreader[
3026 head to column names,
3027 range = 3-,
3028 centered tabularray =
3029 {
3030 rowsep = 1mm,
3031 colsep = 5mm,
3032 column{1} = {r, fg=violet5, colsep=2pt},
3033 column{2} = {r, violet8!10, font=\bfseries},
3034 column{3} = {l, violet8},
3035 hlines = {2pt, white},
3036 },
3037 ]{data.csv}{}{
3038 \thecsvrow
3039 & \csvexpval\land
3040 & \csvexpval\amount
3041 }
3042 \end{dispExample}
3043
3044
3045 \clearpage
3046
3047 The following example uses \refCom{csvautotabularray} to display the whole
3048 table. Note that the \refPkg{tabularray} options are given as last
3049 optional argument.
3050
3051 \begin{dispExample}
3052 % \usepackage{tabularray}
3053 \csvautotabularray[table centered]{data.csv}
3054 [
3055 row{odd} = {blue!85!gray!7},
3056 row{1} = {blue!50!gray!25, font=\bfseries, preto=\MakeUppercase},
3057 hline{1,Z} = {0.1em, blue!50!black},
3058 hline{2} = {blue!50!black}
3059 ]
3060 \end{dispExample}
3061
3062
3063
3064 \clearpage
3065 \subsection{Imported CSV data}\label{sec:importeddata}%
3066 If data is imported from other applications, there is not always a choice
3067 to format in comma-separated values with curly brackets.
3068
3069 Consider the following example data file:
3070
3071 %-- file embedded for simplicity --
3072 \begin{tcbverbatimwrite}{imported.csv}
3073 "name";"address";"email"
3074 "Frank Smith";"Yellow Road 123, Brimblsby";"[email protected]"
3075 "Mary May";"Blue Alley 2a, London";"[email protected]"
3076 "Hans Meier";"Hauptstraße 32, Berlin";"[email protected]"
3077 \end{tcbverbatimwrite}
3078 %-- end embedded file --
3079
3080 \csvlisting{imported}
3081
3082 If the \csvsorter\ program is properly installed,
3083 see Subsection~\ref{sec:Sorting} on page~\pageref{sec:Sorting},
3084 this can be transformed on-the-fly
3085 with the following configuration file for \csvsorter:
3086
3087 \xmllisting{transform}
3088
3089 Now, we just have to add an option |sort by=transform.xml| to transform
3090 the input data. Here, we actually do not sort.
3091
3092 \begin{dispExample}
3093 % \usepackage{booktabs,array}
3094 % Also, the CSV-Sorter tool has to be installed
3095 \newcommand{\Header}[1]{\normalfont\bfseries #1}
3096
3097 \csvreader[
3098 sort by = transform.xml,
3099 tabular = >{\itshape}ll>{\ttfamily}l,
3100 table head = \toprule\Header{Name} & \Header{Address} & \Header{email}\\\midrule,
3101 table foot = \bottomrule
3102 ]
3103 {imported.csv}{}
3104 {\csvlinetotablerow}
3105 \end{dispExample}
3106
3107 The file which is generated on-the-fly and which is actually read by
3108 |csvsimple-l3| is the following:
3109
3110 \tcbinputlisting{docexample,listing style=tcbdocumentation,fonttitle=\bfseries,
3111 listing only,listing file=\jobname_sorted._csv}
3112
3113
3114 \clearpage
3115 \subsection{Encoding}\label{encoding}%
3116 If the CSV file has a different encoding than the \LaTeX\ source file,
3117 then special care is needed.
3118
3119 \begin{itemize}
3120 \item The most obvious treatment is to change the encoding of the CSV file
3121 or the \LaTeX\ source file to match the other one (every good editor
3122 supports such a conversion). This is the easiest choice, if there a no
3123 good reasons against such a step. E.g., unfortunately, several tools
3124 under Windows need the CSV file to be |cp1252| encoded while
3125 the \LaTeX\ source file may need to be |utf8| encoded.
3126
3127 \item The |inputenc| package allows to switch the encoding inside the
3128 document, say from |utf8| to |cp1252|. Just be aware that you should only
3129 use pure ASCII for additional texts inside the switched region.
3130 \begin{dispListing}
3131 % !TeX encoding=UTF-8
3132 % ....
3133 \usepackage[utf8]{inputenc}
3134 % ....
3135 \begin{document}
3136 % ....
3137 \inputencoding{latin1}% only use ASCII from here, e.g. "Uberschrift
3138 \csvreader[%...
3139 ]{data_cp1252.csv}{%...
3140 }{% ....
3141 }
3142 \inputencoding{utf8}
3143 % ....
3144 \end{document}
3145 \end{dispListing}
3146
3147 \item As a variant to the last method, the encoding switch can be done
3148 using options from |csvsimple-l3|:
3149 \begin{dispListing}
3150 % !TeX encoding=UTF-8
3151 % ....
3152 \usepackage[utf8]{inputenc}
3153 % ....
3154 \begin{document}
3155 % ....
3156 % only use ASCII from here, e.g. "Uberschrift
3157 \csvreader[%...
3158 before reading=\inputencoding{latin1},
3159 after reading=\inputencoding{utf8},
3160 ]{data_cp1252.csv}{%...
3161 }{% ....
3162 }
3163 % ....
3164 \end{document}
3165 \end{dispListing}
3166
3167 \pagebreak\item
3168 If the \csvsorter\ program is properly installed,
3169 see Subsection~\ref{sec:Sorting} on page~\pageref{sec:Sorting},
3170 the CSV file can be re-encoded on-the-fly
3171 with the following configuration file for \csvsorter:
3172
3173 \xmllisting{encoding}
3174
3175 \begin{dispListing}
3176 % !TeX encoding=UTF-8
3177 % ....
3178 \usepackage[utf8]{inputenc}
3179 % ....
3180 \begin{document}
3181 % ....
3182 \csvreader[%...
3183 sort by=encoding.xml,
3184 ]{data_cp1252.csv}{%...
3185 }{% ....
3186 }
3187 % ....
3188 \end{document}
3189 \end{dispListing}
3190
3191
3192 \end{itemize}
3193
3194
3195 \clearpage
3196
3197 \printindex
3198
3199 \end{document}