1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
|
% \LaTeX-Main\
%% The LaTeX package csvsimple - version 1.05 (2012/03/12)
%% csvsimple.sty: Simple LaTeX CSV file processing
%%
%% -------------------------------------------------------------------------------------------
%% Copyright (c) 2008-2012 by Prof. Dr. Dr. Thomas F. Sturm <thomas dot sturm at unibw dot de>
%% -------------------------------------------------------------------------------------------
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%% http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%%
%% This work has the LPPL maintenance status `author-maintained'.
%%
%% This work consists of all files listed in README
%%
\documentclass[a4paper,11pt]{ltxdoc}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage[english]{babel}
\usepackage{lmodern,parskip,array,ifthen,calc,makeidx}
\usepackage{amsmath,amssymb}
\usepackage[svgnames,table,hyperref]{xcolor}
\usepackage{tikz}
\usepackage[pdftex,bookmarks,raiselinks,pageanchor,hyperindex,colorlinks]{hyperref}
\urlstyle{sf}
\usepackage[a4paper,left=2.5cm,right=2.5cm,top=1.5cm,bottom=1.5cm,
marginparsep=5mm,marginparwidth=10mm,
headheight=0mm,headsep=0cm,
footskip=1.5cm,includeheadfoot]{geometry}
\usepackage{fancyhdr}
\fancyhf{}
\fancyfoot[C]{\thepage}%
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\pagestyle{fancy}
\tolerance=2000%
\setlength{\emergencystretch}{20pt}%
\RequirePackage{csquotes}
\RequirePackage[style=numeric-comp,sorting=nyt,
maxnames=8,minnames=8,abbreviate=false,backend=biber]{biblatex}
\DeclareFieldFormat{url}{\newline\url{#1}}%
\DeclareListFormat{language}{}%
\setlength{\bibitemsep}{\smallskipamount}
\addbibresource{\jobname.bib}
\usepackage[documentation]{tcolorbox}
\usepackage{longtable}
\usepackage{csvsimple}
\tcbmakedocSubKey{docCsvKey}{csv}
\newcommand{\csvlisting}[1]{%
\tcbinputlisting{docexample,listing style=tcbdocumentation,
listing only,title={CSV file \texttt{#1.csv}},listing file=#1.csv}}
\makeatletter
\newcommand{\auxCommand}[1]{\disp@com{\cs{#1}}\index@Com{#1}}
\makeatother
\hypersetup{
pdftitle={Manual for the csvsimple package},
pdfauthor={Thomas F. Sturm},
pdfsubject={csv file processing with LaTeX},
pdfkeywords={csv file, comma separated values, key value syntax}
}
\def\version{1.05}%
\def\datum{2012/03/12}%
\makeindex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{center}
\vspace*{5mm}\par
{\bfseries\LARGE The \texttt{csvsimple} package\par}
{\large Version \version\ (\datum)\par}\bigskip
{\large Thomas F.~Sturm%
\footnote{Prof.~Dr.~Dr.~Thomas F.~Sturm, Institut f\"{u}r Mathematik und Informatik,
Universit\"{a}t der Bundeswehr M\"{u}nchen, D-85577 Neubiberg, Germany;
email: \href{mailto:thomas.sturm@unibw.de}{thomas.sturm@unibw.de}} }
\end{center}
\bigskip
\begin{absquote}
\begin{center}\bfseries Abstract\end{center}
|csvsimple| provides a simple \LaTeX\ interface for the processing of files with
comma separated values (CSV). |csvsimple| relies heavily on the key value
syntax from |pgfkeys| which results (hopefully) in an easy way of usage.
Filtering and table generation is especially supported. Since the package
is considered as a lightweight tool, there is no support for data sorting
or data base storage.
\end{absquote}
\tableofcontents
\section{Introduction}%
The |csvsimple| package is applied to the processing of
CSV\footnote{CSV file: file with comma separated values.} files.
This processing is controlled by key value assignments according to the
syntax of |pgfkeys| \cite{tantau:2010c}. Sample applications of the package
are tabular lists, serial letters, and charts.
An alternative to |csvsimple| is the |datatool| package \cite{talbot:2009a}
which provides considerably more functions like exchange of separator and
delimiter symbols or sorting of data.
|csvsimple| has a different approach for the user interface and
is deliberately restricted to some basic functions with fast
processing speed.
\subsection{Loading the Package}
The package |csvsimple| loads the packages
|pgfkeys| \cite{tantau:2010c} and |ifthen| \cite{carlisle:2001b}.
|csvsimple| itself is loaded in the usual manner in the preamble:
\begin{dispListing}
\usepackage{csvsimple}
\end{dispListing}
\subsection{First Steps}
Every line of a processable CSV file has to contain an identical amount of
comma separated values. The curly braces |{}| of \TeX\ groups can be used
to mask a block which may contain commas not to be processed as separators.
The first line of such a CSV file is usually but not necessarily a header line
which contains the identifiers for each column.
%-- file embedded for simplicity --
\begin{tcbverbatimwrite}{grade.csv}
name,givenname,matriculation,gender,grade
Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Wei\ss{}b\"{a}ck,Werner,34567,m,5.0
\end{tcbverbatimwrite}
%-- end embedded file --
\csvlisting{grade}
The most simple way to display a CSV file in tabular form is the processing
with the \refCom{csvautotabular} command.
\begin{dispExample}
\csvautotabular{grade.csv}
\end{dispExample}
Typically, one would use \refCom{csvreader} instead of |\csvautotabular| to
gain full control over the interpretation of the included data.
In the following example, the entries of the header line are automatically
assigned to \TeX\ macros which may be used deliberately.
\begin{dispExample}
\begin{tabular}{|l|c|}\hline%
\bfseries Person & \bfseries Matr.~No.
\csvreader[head to column names]{grade.csv}{}%
{\\\givenname\ \name & \matriculation}%
\\\hline
\end{tabular}
\end{dispExample}
\newpage
|\csvreader| is controlled by a plenty of options. For example, for table
applications line breaks are easily inserted by
\refKey{/csv/late after line}. This defines a macro execution just before
the following line.
Additionally, the assignment of columns to \TeX\ macros is shown in a non automated
way.
\begin{dispExample}
\begin{tabular}{|r|l|c|}\hline%
& Person & Matr.~No.\\\hline\hline
\csvreader[late after line=\\\hline]%
{grade.csv}{name=\name,givenname=\firstname,matriculation=\matnumber}%
{\thecsvrow & \firstname~\name & \matnumber}%
\end{tabular}
\end{dispExample}
An even more comfortable way to create a table is setting
appropriate option keys. Note, that this gives you the possibility to create a
|pgfkeys| style which contains the whole table creation.
\begin{dispExample}
\csvreader[tabular=|r|l|c|,
table head=\hline & Person & Matr.~No.\\\hline\hline,
late after line=\\\hline]%
{grade.csv}{name=\name,givenname=\firstname,matriculation=\matnumber}%
{\thecsvrow & \firstname~\name & \matnumber}%
\end{dispExample}
The next example shows such a style definition with the convenience macro
\refCom{csvstyle}. Here, we see again the automated assignment of header
entries to column names by \refKey{/csv/head to column names}.
For this, the header entries have to be without spaces and special characters.
But you can always assign entries to canonical macro names by hand like in the examples
above.
\begin{dispExample}
\csvstyle{myTableStyle}{tabular=|r|l|c|,
table head=\hline & Person & Matr.~No.\\\hline\hline,
late after line=\\\hline,
head to column names}
\csvreader[myTableStyle]{grade.csv}{}%
{\thecsvrow & \givenname~\name & \matriculation}%
\end{dispExample}
\newpage
Another way to address columns is to use their roman numbers.
The direct addressing is done by |\csvcoli|, |\csvcolii|, |\csvcoliii|, \ldots:
\begin{dispExample}
\csvreader[tabular=|r|l|c|,
table head=\hline & Person & Matr.~No.\\\hline\hline,
late after line=\\\hline]%
{grade.csv}{}%
{\thecsvrow & \csvcolii~\csvcoli & \csvcoliii}%
\end{dispExample}
And yet another method to assign macros to columns is to use arabic numbers
for the assignment:
\begin{dispExample}
\csvreader[tabular=|r|l|c|,
table head=\hline & Person & Matr.~No.\\\hline\hline,
late after line=\\\hline]%
{grade.csv}{1=\name,2=\firstname,3=\matnumber}%
{\thecsvrow & \firstname~\name & \matnumber}%
\end{dispExample}
For recurring applications, the |pgfkeys| syntax allows to create own styles
for a consistent and centralized design. The following example is easily
modified to obtain more or less option settings.
\begin{dispExample}
\csvset{myStudentList/.style={%
tabular=|r|l|c|,
table head=\hline & Person & #1\\\hline\hline,
late after line=\\\hline,
column names={name=\name,givenname=\firstname}
}}
\csvreader[myStudentList={Matr.~No.}]{grade.csv}{matriculation=\matnumber}%
{\thecsvrow & \firstname~\name & \matnumber}%
\hfill%
\csvreader[myStudentList={Grade}]{grade.csv}{grade=\grade}%
{\thecsvrow & \firstname~\name & \grade}%
\end{dispExample}
\newpage
Alternatively, column names can be set by \refCom{csvnames}
and style definitions by \refCom{csvstyle}.
With this, the last example is rewritten as follows:
\begin{dispExample}
\csvnames{myNames}{1=\name,2=\firstname,3=\matnumber,5=\grade}
\csvstyle{myStudentList}{tabular=|r|l|c|,
table head=\hline & Person & #1\\\hline\hline,
late after line=\\\hline, myNames}
\csvreader[myStudentList={Matr.~No.}]{grade.csv}{}%
{\thecsvrow & \firstname~\name & \matnumber}%
\hfill%
\csvreader[myStudentList={Grade}]{grade.csv}{}%
{\thecsvrow & \firstname~\name & \grade}%
\end{dispExample}
The data lines of a CSV file can also be filtered. In the following example,
a certificate is printed only for students with grade unequal to 5.0.
\begin{dispExample}
\csvreader[filter not equal={\grade}{5.0}]%
{grade.csv}{1=\name,2=\firstname,3=\matnumber,4=\gender,5=\grade}%
{\begin{center}\Large\bfseries Certificate in Mathematics\end{center}
\large\ifthenelse{\equal{\gender}{f}}{Ms.}{Mr.}
\firstname~\name, matriculation number \matnumber, has passed the test
in mathematics with grade \grade.\par\ldots\par
}%
\end{dispExample}
\clearpage
\section{Macros for the Processing of CSV Files}\label{sec:makros}%
\begin{docCommand}{csvreader}{\oarg{options}\marg{file name}\marg{assignments}\marg{command list}}
|\csvreader| reads the file denoted by \meta{file name} line by line.
Every line of the file has to contain an identical amount of
comma separated values. The curly braces |{}| of \TeX\ groups can be used
to mask a block which may contain commas not to be processed as separators.\smallskip
The first line of such a CSV file is by default but not necessarily
processed as a header line which contains the identifiers for each column.
The entries of this line can be used to give \meta{assignments} to \TeX\ macros
to address the columns. The number of entries of this first line
determines the accepted number of entries for all following lines.
Every line which contains a higher or lower number of entries is ignored
during standard processing.\smallskip
The \meta{assignments} are given by key value pairs
\mbox{\meta{name}|=|\meta{macro}}. Here, \meta{name} is an entry from the
header line \emph{or} the arabic number of the addressed column.
\meta{macro} is some \TeX\ macro which gets the content of the addressed column.\smallskip
The \meta{command list} is executed for every accepted data line. Inside the
\meta{command list} is applicable:
\begin{itemize}
\item \auxCommand{thecsvrow} or the counter |csvrow| which contains the number of the
current data line (starting with 1).
\item \auxCommand{csvcoli}, \auxCommand{csvcolii}, \auxCommand{csvcoliii}, \ldots,
which contain the contents of the column entries of the current data line.
Alternatively can be used:
\item \meta{macro} from the \meta{assignments} to have a logical
addressing of a column entry.
\end{itemize}
Note, that the \meta{command list} is allowed to contain |\par| and
that all macro definitions are made global to be used for table applications.\smallskip
The processing of the given CSV file can be controlled by various
\meta{options} given as key value list. The feasible option keys
are described in section \ref{sec:schluessel} from page \pageref{sec:schluessel}.
\begin{dispExample}
\csvreader[tabular=|r|l|l|, table head=\hline, late after line=\\,
late after last line=\\\hline]{grade.csv}%
{name=\name,givenname=\firstname,grade=\grade}%
{\grade & \firstname~\name & \csvcoliii}
\end{dispExample}
Mainly, the |\csvreader| command consists of a \refCom{csvloop} macro with
following parameters:\par
|\csvloop{|\meta{options}|, file=|\meta{file name}|, column names=|\meta{assignments}|,|\\
\hspace*{2cm} |command=|\meta{command list}|}|\par
Therefore, the application of the keys \refKey{/csv/file} and \refKey{/csv/command}
is useless for |\csvreader|.
\end{docCommand}
\begin{docCommand}{csvloop}{\marg{options}}
Usually, \refCom{csvreader} may be preferred instead of |\csvloop|.
\refCom{csvreader} is based on |\csvloop| which takes a mandatory list of
\meta{options} in key value syntax.
This list of \meta{options} controls the total processing. Especially,
it has to contain the CSV file name.
\begin{dispExample}
\csvloop{file={grade.csv}, column names={name=\name}, command=\name,
before reading={List of students:\ },
late after line={{,}\ }, late after last line=.}
\end{dispExample}
\end{docCommand}
\clearpage
\begin{docCommand}{csvautotabular}{\marg{file name}}
|\csvautotabular| is an abbreviation for the application of the option key
\refKey{/csv/autotabular}.
This macro reads the whole CSV file denoted by \meta{file name}
with an automated formatting.
\begin{dispExample}
\csvautotabular{grade.csv}
\end{dispExample}
\end{docCommand}
\begin{docCommand}{csvautolongtable}{\marg{file name}}
|csvautolongtable| is an abbreviation for the application of the option key
\refKey{/csv/autolongtable}.
This macro reads the whole CSV file denoted by \meta{file name}
with an automated formatting.
For application, the package |longtable| is required which has to be
loaded in the preamble.
\end{docCommand}
\begin{docCommand}{csvset}{\marg{options}}
Sets \meta{options} for every following
\refCom{csvreader} and \refCom{csvloop}. For example, this command may
be used for style definitions.
\begin{dispExample}
\csvset{grade list/.style=
{column names={name=\name,givenname=\firstname,grade=\grade}},
passed/.style={filter not equal={\grade}{5.0}} }
The following students passed the test in mathematics:
\csvreader[grade list,passed]{grade.csv}{}{\firstname\ \name\ (\grade); }%
\end{dispExample}
\end{docCommand}
\begin{docCommand}{csvstyle}{\marg{Stilname}\marg{options}}
Abbreviation for |\csvset{|\meta{style name}|/.style=|\marg{options}|}|
to define a new style.
\end{docCommand}
\begin{docCommand}{csvnames}{\marg{Stilname}\marg{Zuweisungsliste}}
Abbreviation for |\csvset{|\meta{style name}|/.style={column names=|\marg{assignments}|}}|
to define additional \meta{assignments} of macros to columns.
\begin{dispExample}
\csvnames{grade list}{name=\name,givenname=\firstname,grade=\grade}
\csvstyle{passed}{filter not equal={\grade}{5.0}}
The following students passed the test in mathematics:
\csvreader[grade list,passed]{grade.csv}{}{\firstname\ \name\ (\grade); }%
\end{dispExample}
\end{docCommand}
\begin{docCommand}{csvheadset}{\marg{assignments}}
For some special cases, this command can be used to change the
\meta{assignments} of macros to columns during execution of
\refCom{csvreader} and \refCom{csvloop}.
\begin{dispExample}
\csvreader{grade.csv}{}%
{ \csvheadset{name=\n} \fbox{\n}
\csvheadset{givenname=\n} \ldots\ \fbox{\n} }%
\end{dispExample}
\end{docCommand}
\clearpage
\begin{docCommand}{csviffirstrow}{\marg{then macros}\marg{else macros}}
Inside the command list of \refCom{csvreader}, the \meta{then macros}
are executed for the first data line, and the \meta{else macros}
are executed for all following lines.
\begin{dispExample}
\csvreader[tabbing, head to column names, table head=\hspace*{3cm}\=\kill]%
{grade.csv}{}%
{\givenname~\name \> (\csviffirstrow{first entry!!}{following entry})}
\end{dispExample}
\end{docCommand}
\begin{docCommand}{csvifoddrow}{\marg{then macros}\marg{else macros}}
Inside the command list of \refCom{csvreader}, the \meta{then macros}
are executed for odd-numbered data lines, and the \meta{else macros}
are executed for even-numbered lines.
\begin{dispExample}
\csvreader[head to column names,tabular=|l|l|l|l|,
table head=\hline\bfseries \# & \bfseries Name & \bfseries Grade\\\hline,
late after line=\\, late after last line=\\\hline]{grade.csv}{}{%
\csvifoddrow{\slshape\thecsvrow & \slshape\name, \givenname & \slshape\grade}%
{\bfseries\thecsvrow & \bfseries\name, \givenname & \bfseries\grade}}
\end{dispExample}
The |\csvifoddrow| macro may be used for striped tables:
\begin{dispExample}
% This example needs the xcolor package
\csvreader[head to column names,tabular=rlcc,
table head=\hline\rowcolor{red!50!black}\color{white}\# & \color{white}Person
& \color{white}Matr.~No. & \color{white}Grade,
late after head=\\\hline\rowcolor{yellow!50},
late after line=\csvifoddrow{\\\rowcolor{yellow!50}}{\\\rowcolor{red!25}}]%
{grade.csv}{}%
{\thecsvrow & \givenname~\name & \matriculation & \grade}%
\end{dispExample}
Alternatively, |\rowcolors| from the |xcolor| package can be used for this
purpose:
\begin{dispExample}
% This example needs the xcolor package
\csvreader[tabular=rlcc, before table=\rowcolors{2}{red!25}{yellow!50},
table head=\hline\rowcolor{red!50!black}\color{white}\# & \color{white}Person
& \color{white}Matr.~No. & \color{white}Grade\\\hline,
late after line=\\,head to column names]{grade.csv}{}%
{\thecsvrow & \givenname~\name & \matriculation & \grade}%
\end{dispExample}
\end{docCommand}
\clearpage
\begin{docCommand}{csvfilteraccept}{}
All following consistent data lines will be accepted and processed.
This command overwrites all previous filter settings and may be used
inside \refKey{/csv/before filter} to implement
an own filtering rule together with |\csvfilterreject|.
\begin{dispExample}
\csvreader[autotabular,
before filter=\ifthenelse{\equal{\csvcoliv}{m}}{\csvfilteraccept}{\csvfilterreject}
]{grade.csv}{}{\csvlinetotablerow}%
\end{dispExample}
\end{docCommand}
\begin{docCommand}{csvfilterreject}{}
All following data lines will be ignored.
This command overwrites all previous filter settings.
\end{docCommand}
\begin{docCommand}{csvline}{}
This macro contains the current and unprocessed data line.
\begin{dispExample}
\csvreader[no head, tabbing, table head=\textit{line XX:}\=\kill]%
{grade.csv}{}{\textit{line \thecsvrow:} \> \csvline}%
\end{dispExample}
\end{docCommand}
\begin{docCommand}{thecsvrow}{}
Typesets the current data line number. This is the
current number of accepted data lines without the header line.
The \LaTeX\ counter |csvrow| can be addressed directly in the usual way,
e.\,g. by |\roman{csvrow}|.
\end{docCommand}
\begin{docCommand}{thecsvinputline}{}
Typesets the current file line number. This is the
current number of all data lines including the header line.
The \LaTeX\ counter |csvinputline| can be addressed directly in the usual way,
e.\,g. by |\roman{csvinputline}|.
\begin{dispExample}
\csvreader[no head, filter equal={\thecsvinputline}{3}]%
{grade.csv}{}%
{The line with number \thecsvinputline\ contains: \csvline}%
\end{dispExample}
\end{docCommand}
\begin{docCommand}{csvlinetotablerow}{}
Typesets the current processed data line with |&| between the entries.
Most users will never apply this command.
\end{docCommand}
\clearpage
\section{Option Keys}\label{sec:schluessel}%
For the \meta{options} in \refCom{csvreader} respectively \refCom{csvloop}
the following |pgf| keys can be applied. The key tree path |/csv/| is not
to be used inside these macros.
\subsection{Command Definition}%--------%[[
\begin{docCsvKey}{before reading}{=\meta{macros}}{no default, initially empty}
Sets the \meta{macros} to be executed before the CSV file is processed.
\end{docCsvKey}
\begin{docCsvKey}{after head}{=\meta{macros}}{no default, initially empty}
Sets the \meta{macros} to be executed after the header line is read.
\end{docCsvKey}
\begin{docCsvKey}{before filter}{=\meta{macros}}{no default, initially empty}
Sets the \meta{macros} to be executed after reading and consistency checking
of a data line. They are executed before any filter condition is checked,
see \refKey{/csv/filter}.
\end{docCsvKey}
\begin{docCsvKey}{late after head}{=\meta{macros}}{no default, initially empty}
Sets the \meta{macros} to be executed after reading and disassembling
of the first accepted data line. They are executed before further processing
of this line.
\end{docCsvKey}
\begin{docCsvKey}{late after line}{=\meta{macros}}{no default, initially empty}
Sets the \meta{macros} to be executed after reading and disassembling
of the next accepted data line (after \refKey{/csv/before filter}).
They are executed before further processing of this next line.
|late after line| overwrites |late after first line| and |late after last line|.
\end{docCsvKey}
\begin{docCsvKey}{late after first line}{=\meta{macros}}{no default, initially empty}
Sets the \meta{macros} to be executed after reading and disassembling
of the second accepted data line instead of \refKey{/csv/late after line}.
This key has to be set after |late after line|.
\end{docCsvKey}
\begin{docCsvKey}{late after last line}{=\meta{macros}}{no default, initially empty}
Sets the \meta{macros} to be executed after processing of the last
accepted data line instead of \refKey{/csv/late after line}.
This key has to be set after |late after line|.
\end{docCsvKey}
\begin{docCsvKey}{before line}{=\meta{macros}}{no default, initially empty}
Sets the \meta{macros} to be executed after \refKey{/csv/late after line}
and before \refKey{/csv/command}.
|before line| overwrites |before first line|.
\end{docCsvKey}
\begin{docCsvKey}{before first line}{=\meta{macros}}{no default, initially empty}
Sets the \meta{macros} to be executed instead of \refKey{/csv/before line}
for the first accepted data line.
This key has to be set after |before line|.
\end{docCsvKey}
\begin{docCsvKey}{command}{=\meta{macros}}{no default, initially \cs{csvline}}
Sets the \meta{macros} to be executed for every accepted data line.
They are executed between \refKey{/csv/before line} and \refKey{/csv/after line}.
\end{docCsvKey}
\begin{docCsvKey}{after line}{=\meta{macros}}{no default, initially empty}
Sets the \meta{macros} to be executed for every accepted data line
after \refKey{/csv/command}.
|after line| overwrites |after first line|.
\end{docCsvKey}
\begin{docCsvKey}{after first line}{=\meta{macros}}{no default, initially empty}
Sets the \meta{macros} to be executed instead of \refKey{/csv/after line}
for the first accepted data line.
This key has to be set after |after line|.
\end{docCsvKey}
\begin{docCsvKey}{after reading}{=\meta{macros}}{no default, initially empty}
Sets the \meta{macros} to be executed after the CSV file is processed.
\end{docCsvKey}
\begin{dispExample}
\csvreader[
before reading = \meta{before reading}\\,
after head = \meta{after head},
before filter = \\\meta{before filter},
late after head = \meta{late after head},
late after line = \meta{late after line},
late after first line = \meta{late after first line},
late after last line = \\\meta{late after last line},
before line = \meta{before line},
before first line = \meta{before first line},
after line = \meta{after line},
after first line = \meta{after first line},
after reading = \\\meta{after reading}
]{grade.csv}{name=\name}{\textbf{\name}}%
\end{dispExample}
Additional command definition keys are provided for the supported tables,
see section \ref{subsec:tabsupport} from page
\pageref{subsec:tabsupport}.
\subsection{Header Processing and Column Name Assignment}%
\begin{docCsvKey}{head}{\colOpt{=\meta{boolean value}}}{default |true|, initially |true|}
If this key is set, the first line of the CSV file is treated as a header
line which can be used for column name assignments.
\end{docCsvKey}
\begin{docCsvKey}{no head}{}{no value}
Abbreviation for |head=false|, i.\,e. the first line of the CSV file is
treated as data line.
\end{docCsvKey}
\begin{docCsvKey}{column names}{=\meta{assignments}}{no default, initially empty}
Adds some new \meta{assignments} of macros to columns in key value syntax.
Existing assignments are kept.
\end{docCsvKey}
\begin{docCsvKey}{column names reset}{}{no value}
Clears all assignments of macros to columns.
\end{docCsvKey}
\begin{docCsvKey}{head to column names}{\colOpt{=\meta{boolean value}}}{default |true|, initially |false|}
If this key is set, the entries of the header line are used automatically
as macro names for the columns. This option can be used only, if
the header entries do not contain spaces and special characters to be
used as feasible \LaTeX\ macro names.
\end{docCsvKey}
\clearpage
\subsection{Consistency Check and Filtering}%
\begin{docCsvKey}{check column count}{\colOpt{=\meta{boolean value}}}{default |true|, initially |true|}
This key defines, if the number of entries in a data line is checked against
an expected value.\\
If |true|, every non consistent line is ignored without announcement.\\
If |false|, every line is accepted and may produce an error during
further processing.
\end{docCsvKey}
\begin{docCsvKey}{no check column count}{}{no value}
Abbreviation for |check column count=false|.
\end{docCsvKey}
\begin{docCsvKey}{column count}{=\meta{number}}{no default}
Sets the \meta{number} of feasible entries per data line.
This setting is only useful in connection with \refKey{/csv/no head},
since \meta{number} would be replaced by the number of entries in the
header line otherwise.
\end{docCsvKey}
\begin{docCsvKey}{on column count error}{=\meta{macros}}{no default, initially empty}
\meta{macros} to be executed for unfeasible data lines.
\end{docCsvKey}
\begin{docCsvKey}{warn on column count error}{}{style, no value}
Display of a warning for unfeasible data lines.
\end{docCsvKey}
\begin{docCsvKey}{filter}{=\meta{condition}}{no default}
Only data lines which fulfill a logical \meta{condition} are accepted.
For the \meta{condition}, every term from the |ifthen| package \cite{carlisle:2001b}
is feasible.
To preprocess the data line before testing the \meta{condition},
the option key \refKey{/csv/before filter} can be used.
\end{docCsvKey}
\begin{docCsvKey}{no filter}{}{no value, initially set}
Clears a set filter.
\end{docCsvKey}
\begin{docCsvKey}{filter accept all}{}{no value, initially set}
Alias for |no filter|. All consistent data lines are accepted.
\end{docCsvKey}
\begin{docCsvKey}{filter reject all}{}{no value}
All data line are ignored.
\end{docCsvKey}
\begin{docCsvKey}{filter equal}{=\marg{string A}\marg{string B}}{style, no default}
Only lines where \meta{string A} and \meta{string B} are equal after expansion
are accepted.
\end{docCsvKey}
\begin{docCsvKey}{filter not equal}{=\marg{string A}\marg{string B}}{style, no default}
Only lines where \meta{string A} and \meta{string B} are not equal after expansion
are accepted.
\end{docCsvKey}
%]]
\clearpage
\subsection{Table Support}\label{subsec:tabsupport}%--------%[[
\begin{docCsvKey}{tabular}{=\meta{table format}}{style, no default}
Surrounds the CSV processing with |\begin{tabular}|\marg{table format}
at begin and with |\end{tabular}| at end.
Additionally, the commands defined by the key values of
\refKey{/csv/before table}, \refKey{/csv/table head}, \refKey{/csv/table foot},
and \refKey{/csv/after table} are executed at the appropriate places.
\end{docCsvKey}
\begin{docCsvKey}{centered tabular}{=\meta{table format}}{style, no default}
Like \refKey{/csv/tabular} but inside an additional |center| environment.
\end{docCsvKey}
\begin{docCsvKey}{longtable}{=\meta{table format}}{style, no default}
Like \refKey{/csv/tabular} but for the |longtable| environment.
This requires the package |longtable| (not loaded automatically).
\end{docCsvKey}
\begin{docCsvKey}{tabbing}{}{style, no value}
Like \refKey{/csv/tabular} but for the |tabbing| environment.
\end{docCsvKey}
\begin{docCsvKey}{centered tabbing}{}{style, no value}
Like \refKey{/csv/tabbing} but inside an additional |center| environment.
\end{docCsvKey}
\begin{docCsvKey}{no table}{}{style, no value}
Deactivates |tabular|, |longtable|, and |tabbing|.
\end{docCsvKey}
\begin{docCsvKey}{before table}{=\meta{macros}}{no default, initially empty}
Sets the \meta{macros} to be executed before |\begin{tabular}| or before |\begin{longtable}|
or before |\begin{tabbing}|, respectively.
\end{docCsvKey}
\begin{docCsvKey}{table head}{=\meta{macros}}{no default, initially empty}
Sets the \meta{macros} to be executed after |\begin{tabular}| or after |\begin{longtable}|
or after |\begin{tabbing}|, respectively.
\end{docCsvKey}
\begin{docCsvKey}{table foot}{=\meta{macros}}{no default, initially empty}
Sets the \meta{macros} to be executed before |\end{tabular}| or before |\end{longtable}|
or before |\end{tabbing}|, respectively.
\end{docCsvKey}
\begin{docCsvKey}{after table}{=\meta{macros}}{no default, initially empty}
Sets the \meta{macros} to be executed after |\end{tabular}| or after |\end{longtable}|
or after |\end{tabbing}|, respectively.
\end{docCsvKey}
\begin{docCsvKey}{autotabular}{=\meta{file name}}{no default}
Reads the whole CSV file denoted \meta{file name} with an automated formatting.
\end{docCsvKey}
\begin{docCsvKey}{autolongtable}{=\meta{file name}}{no default}
Reads the whole CSV file denoted \meta{file name} with an automated formatting
using the required |longtable| package.
\end{docCsvKey}
\clearpage
\subsection{Miscellaneous}%
\begin{docCsvKey}{every csv}{}{style, initially empty}
A style definition which is used for every following CSV file.
This definition can be overwritten with user code.
\begin{dispListing}
% Sets a warning message for unfeasible data lines.
\csvset{every csv/.style={warn on column count error}}
% Alternatively:
\csvstyle{every csv}{warn on column count error}
\end{dispListing}
\end{docCsvKey}
\begin{docCsvKey}{default}{}{style}
A style definition which is used for every following CSV file which
resets all settings to default values\footnote{\texttt{default} is used
because of the global nature of most settings.}.
This key should not be used or changed by the user if there is not a
really good reason (and you know what you do).
\end{docCsvKey}
\begin{docCsvKey}{file}{=\meta{file name}}{no default, initially |unknown.csv|}
Sets the \meta{file name} of the CSV file to be processed.
\end{docCsvKey}
\begin{docCsvKey}{preprocessed file}{=\meta{file name}}{no default, initially unused}
Sets the \meta{file name} of the CSV file which is the output of a
preprocessor.
\end{docCsvKey}
\begin{docCsvKey}{preprocessor}{=\meta{macro}}{no default}
Defines a preprocessor for the given CSV file.
The \meta{macro} has to have two mandatory arguments. The first argument
is the original CSV file which is set by \refKey{/csv/file}.
The second argument is the preprocessed CSV file
which is set by \refKey{/csv/preprocessed file}.\par\smallskip
Typically, the \meta{macro} may call an external program which preprocesses
the original CSV file (e.\,g. sorting the file) and creates the
preprocessed CSV file. The later file is used by \refCom{csvreader}
or \refCom{csvloop}.
\begin{dispListing}
\newcommand{\mySortTool}[2]{%
% call to an external program to sort file #1 with resulting file #2
}
\csvreader[%
preprocessed file=\jobname_sorted.csv,
preprocessor=\mySortTool,
]{some.csv}{}{%
% do something
}
\end{dispListing}
\end{docCsvKey}
\begin{docCsvKey}{no preprocessing}{}{style, no value, initially set}
Clears any preprocessing, i.\,e. preprocessing is switched of.
\end{docCsvKey}
\clearpage
\section{Examples}%
\subsection{A Serial Letter}%
In this example, a serial letter is to be written to all persons with
addresses from the following CSV file. Deliberately, the file content is
not given in very pretty format.
%-- file embedded for simplicity --
\begin{tcbverbatimwrite}{address.csv}
name,givenname,gender,degree,street,zip,location,bonus
Maier,Hans,m,,Am Bachweg 17,10010,Hopfingen,20
% next line with a comma in curly braces
Huber,Erna,f,Dr.,{Moosstra\ss{}e 32, Hinterschlag},10020,\"{O}rtingstetten,30
Wei\ss{}b\"{a}ck,Werner,m,Prof. Dr.,Brauallee 10,10030,Klingenbach,40
% this line is ignored %
Siebener , Franz,m, , Blaumeisenweg 12 , 10040 , Pardauz , 50
% preceding and trailing spaces in entries are removed %
Schmitt,Anton,m,,{\AE{}lfred-Esplanade, T\ae{}g 37}, 10050,\OE{}resung,60
\end{tcbverbatimwrite}
%-- end embedded file --
\csvlisting{address}
Firstly, we survey the file content quickly using
|\csvautotabular|.
As can be seen, unfeasible lines are ignored automatically.
\begin{dispExample}
\tiny\csvautotabular{address.csv}
\end{dispExample}
Now, we create the serial letter where every feasible data line produces
an own page. Here, we simulate the page by a |tcolorbox| (from the package
|tcolorbox|).
For the gender specific salutations, an auxiliary macro |\ifmale| is
introduced.
\begin{dispExample}
% this example requires the tcolorbox package
\newcommand{\ifmale}[2]{\ifthenelse{\equal{\gender}{m}}{#1}{#2}}
\csvreader[head to column names]{address.csv}{}{%
\begin{tcolorbox}[colframe=DarkGray,colback=White,arc=0mm,width=(\linewidth-2pt)/2,
before=,after=\hfill,title={Letter to \name},fonttitle=\bfseries]
\ifthenelse{\equal{\degree}{}}{\ifmale{Mr.}{Ms.}}{\degree}~\givenname~\name\\
\street\\\zip~\location
\tcblower
{\itshape Dear \ifmale{Sir}{Madam},}\\
we are pleased to announce you a bonus value of \bonus\%{}
which will be delivered to \location\ soon.\\\ldots
\end{tcolorbox}}
\end{dispExample}
\clearpage
\subsection{A Graphical Presentation}%
For this example, we use some artificial statistical data given by a CSV file.
%-- file embedded for simplicity --
\begin{tcbverbatimwrite}{data.csv}
land,group,amount
Bayern,A,1700
Baden-W\"{u}rttemberg,A,2300
Sachsen,B,1520
Th\"{u}ringen,A,1900
Hessen,B,2100
\end{tcbverbatimwrite}
%-- end embedded file --
\csvlisting{data}
Firstly, we survey the file content using
|\csvautotabular|.
\begin{dispExample}
\csvautotabular{data.csv}
\end{dispExample}
The amount values are presented in the following diagram by bars where
the group classification is given using different colors.
\begin{dispExample}
% This example requires the package tikz
\begin{tikzpicture}[Group/A/.style={left color=red!10,right color=red!20},
Group/B/.style={left color=blue!10,right color=blue!20}]
\csvreader[head to column names]{data.csv}{}{%
\begin{scope}[yshift=-\thecsvrow cm]
\path [draw,Group/\group] (0,-0.45)
rectangle node[font=\bfseries] {\amount} (\amount/1000,0.45);
\node[left] at (0,0) {\land};
\end{scope} }
\end{tikzpicture}
\end{dispExample}
\clearpage
Next, we create a pie chart by calling |\csvreader| twice.
In the first step, the total sum of amounts is computed, and in the second
step the slices are drawn.
\begin{dispExample}
% Modified example from www.texample.net for pie charts
% This example needs the packages tikz, xcolor, calc
\definecolorseries{myseries}{rgb}{step}[rgb]{.95,.85,.55}{.17,.47,.37}
\resetcolorseries{myseries}%
% a pie slice
\newcommand{\slice}[4]{
\pgfmathsetmacro{\midangle}{0.5*#1+0.5*#2}
\begin{scope}
\clip (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;
\colorlet{SliceColor}{myseries!!+}%
\fill[inner color=SliceColor!30,outer color=SliceColor!60] (0,0) circle (1cm);
\end{scope}
\draw[thick] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;
\node[label=\midangle:#4] at (\midangle:1) {};
\pgfmathsetmacro{\temp}{min((#2-#1-10)/110*(-0.3),0)}
\pgfmathsetmacro{\innerpos}{max(\temp,-0.5) + 0.8}
\node at (\midangle:\innerpos) {#3};
}
% sum of amounts
\csvreader[before reading=\def\mysum{0}]{data.csv}{amount=\amount}{%
\pgfmathsetmacro{\mysum}{\mysum+\amount}%
}
% drawing of the pie chart
\begin{tikzpicture}[scale=3]%
\def\mya{0}\def\myb{0}
\csvreader[head to column names]{data.csv}{}{%
\let\mya\myb
\pgfmathsetmacro{\myb}{\myb+\amount}
\slice{\mya/\mysum*360}{\myb/\mysum*360}{\amount}{\land}
}
\end{tikzpicture}%
\end{dispExample}
\clearpage
Finally, the filter option is demonstrated by separating the groups A and B.
Every item is piled upon the appropriate stack.
\begin{dispExample}
\newcommand{\drawGroup}[2]{%
\def\mya{0}\def\myb{0}
\node[below=3mm] at (2.5,0) {\bfseries Group #1};
\csvreader[head to column names,filter equal={\group}{#1}]{data.csv}{}{%
\let\mya\myb
\pgfmathsetmacro{\myb}{\myb+\amount}
\path[draw,top color=#2!25,bottom color=#2!50]
(0,\mya/1000) rectangle node{\land\ (\amount)} (5,\myb/1000);
}}
\begin{tikzpicture}
\fill[gray!75] (-1,0) rectangle (13,-0.1);
\drawGroup{A}{red}
\begin{scope}[xshift=7cm]
\drawGroup{B}{blue}
\end{scope}
\end{tikzpicture}
\end{dispExample}
% Actually, it is not a good idea to include the references like this!
% Do not follow this bad example ...
\begin{tcbverbatimwrite}{\jobname.bib}
@manual{tantau:2010c,
author = {Till Tantau},
title = {The TikZ and PGF Packages},
subtitle = {Manual for version 2.10},
url = {http://mirror.ctan.org/graphics/pgf/base/doc/generic/pgf/pgfmanual.pdf},
date = {2010-10-25},
}
@manual{carlisle:2001b,
author = {David Carlisle},
title = {The ifthen package},
url = {http://mirror.ctan.org/macros/latex/base/},
Xnote = {},
day = 26,
month = may,
year = 2001,
language = {english}
}
@manual{talbot:2009a,
author = {Nicola L. C. Talbot},
title = {datatool v 2.03: Databases and data manipulation},
url = {http://mirror.ctan.org/macros/latex/contrib/datatool/datatool.pdf},
date = {2009-11-15},
}
\end{tcbverbatimwrite}
\printbibliography[heading=bibintoc]
\printindex
\end{document}
|