Csvsimple l3
Csvsimple l3
Thomas F. Sturm1
https://www.ctan.org/pkg/csvsimple
https://github.com/T-F-S/csvsimple
Abstract
csvsimple(-l3) provides a simple LATEX interface for the processing of files with
comma separated values (CSV). csvsimple-l3 relies heavily on the key value syntax
from l3keys which results 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.
1
Prof. Dr. Dr. Thomas F. Sturm, Institut für Mathematik und Informatik, Universität der Bundeswehr
München, D-85577 Neubiberg, Germany; email: thomas.sturm@unibw.de
1
Contents
1 Introduction 3
1.1 Loading the Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 First Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3 Option Keys 16
3.1 Command Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.2 Header Processing and Column Name Assignment . . . . . . . . . . . . . . . . . 18
3.3 Consistency Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.4 Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.5 Line Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.6 Table Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.6.1 Predefined Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.6.2 Additional Options for Tables . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.6.3 Generic Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.6.4 General Survey Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.7 Special Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.8 Separators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.9 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.10 Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.11 Data Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
5 Examples 41
5.1 A Serial Letter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
5.2 A Graphical Presentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5.3 Macro code inside the data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
5.4 Tables with Number Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
5.5 CSV data without header line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
5.6 Tables with tabularray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
5.7 Imported CSV data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
5.8 Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Index 60
2
1 Introduction
The csvsimple-l3 package is applied to the processing of CSV2 files. This processing is con-
trolled by key value assignments according to the syntax of l3keys. Sample applications of the
package are tabular lists, serial letters, and charts.
An alternative to csvsimple-l3 is the datatool package which provides considerably more
functions and allows sorting of data by LATEX. csvsimple-l3 has a different approach for the
user interface and is deliberately restricted to some basic functions with fast processing speed.
Mind the following restrictions:
• Sorting is not supported directly but can be done with external tools, see Section 3.10 on
page 33.
• Values are expected to be comma separated, but the package provides support for other
separators, see Section 3.8 on page 31.
• Values are expected to be either not quoted or quoted with curly braces {} of TEX groups.
Other quotes like doublequotes are not supported directly, but can be achieved with ex-
ternal tools, see Section 5.7 on page 57.
• Every data line is expected to contain the same amount of values. Unfeasible data lines
are silently ignored by default, but this can be configured, see Section 3.3 on page 19.
\usepackage[l3]{csvsimple}
% or alternatively (not simultaneously!)
\usepackage{csvsimple-l3}
Not automatically loaded, but used for many examples are the packages longtable, booktabs,
ifthen, and etoolbox.
2
CSV file: file with comma separated values.
3
1.2 First Steps
Every line of a processable CSV file has to contain an identical amount of comma3 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.
CSV file «grade.csv»
name,givenname,matriculation,gender,grade
Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Weißbäck,Werner,34567,m,5.0
Bauer,Maria,19202,f,3.3
The most simple way to display a CSV file in tabular form is the processing with the
\csvautotabular→ P. 10 command.
\csvautotabular{grade.csv}
Typically, one would use \csvreader→ P. 9 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{tabular}{|l|c|}\hline%
\bfseries Person & \bfseries Matr.~No.
\csvreader[
head to column names
]{grade.csv}{}{%
\\\givenname\ \name & \matriculation
}%
\\\hline
\end{tabular}
3
See /csvsim/separator→ P. 31 for other separators than comma.
4
\csvreader is controlled by a plenty of options. For example, for table applications line breaks
are easily inserted by /csvsim/late after line→ P. 16 . 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{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}
An even more comfortable and preferrable way to create a table is setting appropriate option
keys. Note, that this gives you the possibility to create a meta key (called style here) which
contains the whole table creation using \csvstyle→ P. 12 or keys_define:nn from l3keys.
\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
}%
5
The next example shows such a style definition with the convenience macro \csvstyle→ P. 12 .
Here, we see again the automated assignment of header entries to column names by
/csvsim/head to column names→ P. 18 . For this, the header entries have to be without spaces
and special characters. But you can always assign entries to canonical macro names manually
like in the examples above. Here, we also add a /csvsim/head to column names prefix→ P. 18
to avoid macro name clashes.
\csvstyle{myTableStyle}{
tabular = |r|l|c|,
table head = \hline & Person & Matr.~No.\\\hline\hline,
late after line = \\\hline,
head to column names,
head to column names prefix = MY,
}
\csvreader[myTableStyle]
{grade.csv}{}{%
\thecsvrow & \MYgivenname~\MYname & \MYmatriculation
}
Another way to address columns is to use their roman numbers. The direct addressing is done
by \csvcoli, \csvcolii, \csvcoliii, . . . :
\csvreader[
tabular = |r|l|c|,
table head = \hline & Person & Matr.~No.\\\hline\hline,
late after line = \\\hline
]{grade.csv}{}{%
\thecsvrow & \csvcolii~\csvcoli & \csvcoliii
}
And yet another method to assign macros to columns is to use arabic numbers for the assignment:
\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
}
6
For recurring applications, the l3keys syntax allows to create own meta options (styles) for a
consistent and centralized design. The following example is easily modified to obtain more or
less option settings.
\csvstyle{myStudentList}{%
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
}
7
Alternatively, column names can be set by \csvnames→ P. 12 and style definitions by
\csvstyle→ P. 12 . With this, the last example is rewritten as follows:
\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
}
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.
\csvreader[
filter not strcmp={\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\ifcsvstrcmp{\gender}{f}{Ms.}{Mr.}
\firstname~\name, matriculation number \matnumber, has passed the test
in mathematics with grade \grade.\par\ldots\par
}%
Certificate in Mathematics
Mr. Hans Maier, matriculation number 12345, has passed the test in mathematics
with grade 1.0.
...
Certificate in Mathematics
Ms. Anna Huber, matriculation number 23456, has passed the test in mathematics
with grade 2.3.
...
Certificate in Mathematics
Ms. Maria Bauer, matriculation number 19202, has passed the test in mathematics
with grade 3.3.
...
8
2 Macros for the Processing of CSV Files
\csvreader[⟨options⟩]{⟨file name⟩}{⟨assignments⟩}{⟨command list⟩}
\csvreader reads the file denoted by ⟨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.
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 ⟨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.
The ⟨assignments⟩ are given as comma separated list of key value pairs ⟨name⟩=⟨macro⟩.
Here, ⟨name⟩ is an entry from the header line or the arabic number of the addressed column.
⟨macro⟩ is some TEX macro which gets the content of the addressed column.
The ⟨command list⟩ is executed for every accepted data line. Inside the ⟨command list⟩ is
applicable:
• \thecsvrow or the counter csvrow which contains the number of the current data line
(starting with 1).
• \csvcoli, \csvcolii, \csvcoliii, . . . , which contain the contents of the column
entries of the current data line. Alternatively can be used:
• ⟨macro⟩ from the ⟨assignments⟩ to have a logical addressing of a column entry.
Note, that the ⟨command list⟩ is allowed to contain \par and that all macro definitions
are made global to be used for table applications.
The processing of the given CSV file can be controlled by various ⟨options⟩ given as key
value list. The feasible option keys are described in section 3 from page 16.
\csvreader[
tabular = |r|l|l|,
table head = \hline,
table foot = \hline
]{grade.csv}%
{name=\name, givenname=\firstname, grade=\grade}{%
\grade & \firstname~\name & \csvcoliii
}
Mainly, the \csvreader command consists of a \csvloop→ P. 10 macro with following pa-
rameters:
\csvloop{⟨options⟩, file=⟨file name⟩, column names=⟨assignments⟩,
command=⟨command list⟩}
Therefore, the application of the keys /csvsim/file→ P. 32 and /csvsim/command→ P. 17 is
useless for \csvreader.
9
\csvloop{⟨options⟩}
Usually, \csvreader→ P. 9 may be preferred instead of \csvloop. \csvreader→ P. 9 is based
on \csvloop which takes a mandatory list of ⟨options⟩ in key value syntax. This list of
⟨options⟩ controls the total processing. Especially, it has to contain the CSV file name.
\csvloop{
file = {grade.csv},
head to column names,
command = \name,
before reading = {List of students:\ },
late after line = {{,}\ },
late after last line = .
}
The following \csvauto... commands are intended for quick data overview with limited for-
matting potential. See Subsection 3.6 on page 26 for the general table options in combination
with \csvreader→ P. 9 and \csvloop.
\csvautotabular[⟨options⟩]{⟨file name⟩}
N 2021-06-25 \csvautotabular*[⟨options⟩]{⟨file name⟩}
\csvautotabular or \csvautotabular* is an abbreviation for the application of the op-
tion key /csvsim/autotabular→ P. 29 or /csvsim/autotabular*→ P. 29 together with other
⟨options⟩ to \csvloop. This macro reads the whole CSV file denoted by ⟨file name⟩ with an
automated formatting. The star variant treats the first line as data line and not as header
line.
\csvautotabular*{grade.csv}
\csvautotabular[filter equal={\csvcoliv}{f}]{grade.csv}
10
\csvautolongtable[⟨options⟩]{⟨file name⟩}
N 2021-06-25 \csvautolongtable*[⟨options⟩]{⟨file name⟩}
\csvautolongtable or \csvautolongtable* is an abbreviation for the application of the
option key /csvsim/autolongtable→ P. 29 or /csvsim/autolongtable*→ P. 29 together with
other ⟨options⟩ to \csvloop→ P. 10 . This macro reads the whole CSV file denoted by ⟨file
name⟩ with an automated formatting. For application, the package longtable is required
which has to be loaded in the preamble. The star variant treats the first line as data line
and not as header line.
\csvautolongtable{grade.csv}
\csvautobooklongtable[⟨options⟩]{⟨file name⟩}
N 2021-06-25 \csvautobooklongtable*[⟨options⟩]{⟨file name⟩}
\csvautobooklongtable or \csvautobooklongtable* is an abbreviation for the applica-
tion of the option key /csvsim/autobooklongtable→ P. 29 or /csvsim/autobooklongtable*→ P. 29
together with other ⟨options⟩ to \csvloop→ P. 10 . This macro reads the whole CSV file
denoted by ⟨file name⟩ with an automated formatting. For application, the packages
booktabs and longtable are required which have to be loaded in the preamble. The star
variant treats the first line as data line and not as header line.
\csvautobooklongtable{grade.csv}
11
U 2021-06-25 \csvset{⟨options⟩}
Sets ⟨options⟩ for every following \csvreader→ P. 9 and \csvloop→ P. 10 . Note that most
options are set to default values at the begin of these commands and therefore cannot be
defined reasonable by \csvset. But it may be used for options like /csvsim/csvsorter
command→ P. 33 to give global settings. Also see /csvsim/every csv→ P. 32 .
\csvstyle{⟨key⟩}{⟨options⟩}
Defines a new l3keys meta key to call other keys. It is used to make abbreviations for con-
venient key set applications. The new ⟨key⟩ can take one parameter. The name \csvstyle
originates from an old version of csvsimple which used pgfkeys instead of l3keys.
\csvstyle{grade list}{
column names = {name=\name, givenname=\firstname, grade=\grade}
}
\csvstyle{passed}{
filter not strcmp = {\grade}{5.0}
}
The following students passed the test in mathematics:\\
\csvreader[grade list,passed]{grade.csv}{}{
\firstname\ \name\ (\grade);
}
\csvnames{⟨key⟩}{⟨assignments⟩}
Abbreviation for \csvstyle{⟨key⟩}{column names={⟨assignments⟩}} to define additional
⟨assignments⟩ of macros to columns.
\csvnames{grade list}{
name=\name, givenname=\firstname, grade=\grade
}
\csvstyle{passed}{
filter not strcmp = {\grade}{5.0}
}
The following students passed the test in mathematics:\\
\csvreader[grade list,passed]{grade.csv}{}{
\firstname\ \name\ (\grade);
}
12
U 2021-06-28 \ifcsvoddrow{⟨then macros⟩}{⟨else macros⟩}
Inside the command list of \csvreader→ P. 9 , the ⟨then macros⟩ are executed for odd-
numbered data lines, and the ⟨else macros⟩ are executed for even-numbered lines.
\ifcsvoddrow is expandable.
\csvreader[
head to column names,
tabular = |l|l|l|l|,
table head = \hline\bfseries \# & \bfseries Name & \bfseries Grade\\\hline,
table foot = \hline
]{grade.csv}{}{%
\ifcsvoddrow{\slshape\thecsvrow & \slshape\name, \givenname & \slshape\grade}%
{\bfseries\thecsvrow & \bfseries\name, \givenname & \bfseries\grade}
}
# Name Grade
1 Maier, Hans 1.0
2 Huber, Anna 2.3
3 Weißbäck, Werner 5.0
4 Bauer, Maria 3.3
Alternatively, \rowcolors from the xcolor package can be used for this purpose:
The deprecated, but still available alias for this command is \csvifoddrow.
13
U 2021-06-28 \ifcsvfirstrow{⟨then macros⟩}{⟨else macros⟩}
Inside the command list of \csvreader→ P. 9 , the ⟨then macros⟩ are executed for the first
data line, and the ⟨else macros⟩ are executed for all following lines. \ifcsvfirstrow is
expandable.
\csvreader[
tabbing,
head to column names,
table head = {\hspace*{3cm}\=\kill}
]{grade.csv}{}{%
\givenname~\name \> (\ifcsvfirstrow{first entry!!}{following entry})
}
The deprecated, but still available alias for this command is \csviffirstrow.
\csvfilteraccept
All following consistent data lines will be accepted and processed. This command overwrites
all previous filter settings and may be used inside /csvsim/full filter→ P. 23 to implement
an own filtering rule together with \csvfilterreject.
\csvreader[
autotabular,
full filter = \ifcsvstrcmp{\csvcoliv}{m}{\csvfilteraccept}{\csvfilterreject}
]{grade.csv}{}{%
\csvlinetotablerow
}
\csvfilterreject
All following data lines will be ignored. This command overwrites all previous filter settings.
\csvline
This macro contains the current and unprocessed data line.
\csvreader[
no head,
tabbing,
table head = {\textit{line XX:}\=\kill}
]{grade.csv}{}{%
\textit{line \thecsvrow:} \> \csvline
}
line 1: name,givenname,matriculation,gender,grade
line 2: Maier,Hans,12345,m,1.0
line 3: Huber,Anna,23456,f,2.3
line 4: Weißbäck,Werner,34567,m,5.0
line 5: Bauer,Maria,19202,f,3.3
14
U 2022-01-11 \csvlinetotablerow
Typesets the current processed data line with & between the entries. This macro is expand-
able.
U 2021-06-25 \thecsvrow
N 2021-06-25 \g_csvsim_row_int
Typesets the current data line number. This is the current number of accepted data lines
without the header line. Despite of the name, there is no associated LATEX counter csvrow,
but \thecsvrow is an accessor the LATEX3 integer \g_csvsim_row_int.
N 2021-06-25 \thecsvcolumncount
N 2021-06-25 \g_csvsim_columncount_int
Typesets the number of columns of the current CSV file. This number is either
computed from the first valid line (header or data) or given by /csvsim/column
count→ P. 19 . Despite of the name, there is no associated LATEX counter csvcolumncount,
but \thecsvcolumncount is an accessor the LATEX3 integer \g_csvsim_columncount_int.
\csvreader{grade.csv}{}{}%
The last file consists of \thecsvcolumncount{} columns and
\thecsvrow{} accepted data lines. The total number of lines
ist \thecsvinputline{}.
The last file consists of 5 columns and 4 accepted data lines. The total number of lines ist 6.
U 2021-06-25 \thecsvinputline
N 2021-06-25 \g_csvsim_inputline_int
Typesets the current file line number. This is the current number of all data lines including
the header line and all lines filtered out. Despite of the name, there is no associated
LATEX counter csvinputline, but \thecsvinputline is an accessor the LATEX3 integer
\g_csvsim_inputline_int.
\csvreader[
no head,
filter test = \ifnumequal{\thecsvinputline}{3}
]{grade.csv}{}{%
The line with number \thecsvinputline\ contains: \csvline
}
15
3 Option Keys
For the ⟨options⟩ in \csvreader→ P. 9 respectively \csvloop→ P. 10 the following l3keys keys can
be applied. The ⟨module⟩ name /csvsim/ is not to be used inside these macros.
16
/csvsim/command=⟨code⟩ (no default, initially \csvline)
Sets the ⟨code⟩ to be executed for every accepted data line. It is executed between
/csvsim/before line→ P. 16 and /csvsim/after line. /csvsim/command describes the
main processing of the line entries. \csvreader→ P. 9 sets /csvsim/command as mandatory
parameter.
/csvsim/after line=⟨code⟩ (no default, initially empty)
Sets the ⟨code⟩ to be executed for every accepted data line after /csvsim/command. All line
entries are still available. /csvsim/after line overwrites /csvsim/after first line.
/csvsim/after first line=⟨code⟩ (no default, initially empty)
Sets the ⟨code⟩ to be executed instead of /csvsim/after line for the first accepted data
line. All line entries are still available. This key has to be set after /csvsim/after line.
/csvsim/after reading=⟨code⟩ (no default, initially empty)
Sets the ⟨code⟩ to be executed after the CSV file is closed.
The following example illustrates the sequence of command execution. Note that
/csvsim/command is set by the mandatory last parameter of \csvreader→ P. 9 .
\csvreader[
before reading = \meta{before reading}\\,
after head = \meta{after head},
before filter = \\\meta{before filter},
after filter = \meta{after 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}}%
⟨before reading⟩
⟨after head⟩
⟨before filter⟩⟨late after head⟩⟨after filter⟩⟨before first line⟩Maier⟨after first line⟩
⟨before filter⟩⟨late after first line⟩⟨after filter⟩⟨before line⟩Huber⟨after line⟩
⟨before filter⟩⟨late after line⟩⟨after filter⟩⟨before line⟩Weißbäck⟨after line⟩
⟨before filter⟩⟨late after line⟩⟨after filter⟩⟨before line⟩Bauer⟨after line⟩
⟨late after last line⟩
⟨after reading⟩
Additional command definition keys are provided for the supported tables, see Section 3.6 from
page 26.
17
3.2 Header Processing and Column Name Assignment
/csvsim/head=true|false (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.
/csvsim/no head (no value)
Abbreviation for head=false, i. e. the first line of the CSV file is treated as data line.
Note that this option cannot be used in combination with the \csvauto... commands like
\csvautotabular→ P. 10 , etc. Instead, there are star variants like \csvautotabular*→ P. 10
to process files without header line. See Section 5.5 on page 52 for examples.
/csvsim/column names={⟨assignments⟩} (no default, initially empty)
Adds some new ⟨assignments⟩ of macros to columns in key value syntax. Existing assign-
ments are kept.
The ⟨assignments⟩ are given as comma separated list of key value pairs ⟨name⟩=⟨macro⟩.
Here, ⟨name⟩ is an entry from the header line or the arabic number of the addressed column.
⟨macro⟩ is some TEX macro which gets the content of the addressed column.
a header entry section will generate the corresponding macro \MYsection instead of de-
stroying the standard LATEX \section macro.
N 2022-02-01 /csvsim/column names detection=true|false (default true, initially true)
If this key is set, the header line is detected for names which can be used for /csvsim/column
names and /csvsim/head to column names. Otherwise, these options are not functional.
This key can and should be set to false, if the header line contains macros or characters
not allowed inside LATEX control sequences, because otherwise compilation error are to be
expected.
18
3.3 Consistency Check
/csvsim/check column count=true|false (default true, initially true)
This key defines, wether the number of entries in a data line is checked against an expected
value or not.
If true, every non consistent line is ignored without announcement.
If false, every line is accepted and may produce an error during further processing.
/csvsim/no check column count (no value)
Abbreviation for check column count=false.
U 2021-06-24 /csvsim/column count=⟨number⟩ (no default, initially 0)
Sets the ⟨number⟩ of feasible entries per data line. If /csvsim/column count is set to 0,
the number of entries of the first non-empty line determines the column count (automatic
detection).
This setting is only useful in connection with /csvsim/no head→ P. 18 , since ⟨number⟩ would
be replaced by the number of entries in the header line otherwise.
/csvsim/on column count error=⟨code⟩ (no default, initially empty)
⟨code⟩ to be executed for unfeasible data lines.
/csvsim/warn on column count error (style, no value)
Display of a warning for unfeasible data lines.
19
3.4 Filtering
Applying a filter means that data lines are only processed / displayed, if they fulfill a given
condition.
The following string compare filters /csvsim/filter strcmp and /csvsim/filter equal are
identical by logic, but differ in implementation.
/csvsim/filter strcmp={⟨stringA⟩}{⟨stringB⟩} (style, no default)
Only lines where ⟨stringA⟩ and ⟨stringB⟩ are equal after expansion are accepted. The
implementation is done with \ifcsvstrcmp→ P. 40 .
% \usepackage{booktabs}
\csvreader[
head to column names,
tabular = llll,
table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
table foot = \bottomrule,
filter strcmp = {\gender}{f}, %>> list only female persons <<
]{grade.csv}{}{%
\thecsvrow & \slshape\name, \givenname & \matriculation & \grade
}
% \usepackage{booktabs}
\csvreader[
head to column names,
tabular = llll,
table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
table foot = \bottomrule,
%>> list only matriculation numbers greater than 20000
% and grade less than 4.0 <<
filter fp = { \matriculation > 20000 && \grade < 4.0 },
]{grade.csv}{}{%
\thecsvrow & \slshape\name, \givenname & \matriculation & \grade
}
20
N 2021-06-25 /csvsim/filter bool=⟨boolean expression⟩ (no default)
Only data lines which fulfill a L TEX3 ⟨boolean expression⟩ are accepted. Note that such
A
an ⟨boolean expression⟩ needs expl3 code. To preprocess the data line before testing the
⟨condition⟩, the option key /csvsim/before filter→ P. 16 can be used.
\csvreader[
head to column names,
tabular = llll,
table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
table foot = \bottomrule,
myfilter
]{grade.csv}{}{%
\thecsvrow & \slshape\name, \givenname & \matriculation & \grade
}
\csvreader[
head to column names,
tabular = llll,
table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
table foot = \bottomrule,
myfilter
]{grade.csv}{}{%
\thecsvrow & \slshape\name, \givenname & \matriculation & \grade
}
21
N 2016-07-01 /csvsim/filter test=⟨condition⟩ (no default)
Only data lines which fulfill a logical ⟨condition⟩ are accepted. For the ⟨condition⟩, every
single test normally employed like
\iftest{some testing}{true}{false}
can be used as
filter test=\iftest{some testing},
For \iftest, tests from the etoolbox package like \ifnumcomp, \ifdimgreater, etc. and
from Section 4 on page 40 can be used. Also, arbitrary own macros fulfilling this signature
can be applied.
% \usepackage{etoolbox,booktabs}
\csvreader[
head to column names,
tabular = llll,
table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
table foot = \bottomrule,
%>> list only matriculation numbers greater than 20000 <<
filter test = \ifnumgreater{\matriculation}{20000},
]{grade.csv}{}{%
\thecsvrow & \slshape\name, \givenname & \matriculation & \grade
}
22
N 2016-07-01 /csvsim/filter ifthen=⟨boolean expression⟩ (no default)
Only data lines which fulfill a ⟨boolean expression⟩ are accepted. For the ⟨boolean
expression⟩, every term from the ifthen package is feasible (package loading required!).
To preprocess the data line before testing the ⟨condition⟩, the option key /csvsim/before
filter→ P. 16 can be used.
% \usepackage{ifthen,booktabs}
\csvreader[
head to column names,
tabular = llll,
table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
table foot = \bottomrule,
%>> list only female persons <<
filter ifthen=\equal{\gender}{f},
]{grade.csv}{}{%
\thecsvrow & \slshape\name, \givenname & \matriculation & \grade
}
% \usepackage{etoolbox,booktabs}
\csvreader[
head to column names,
tabular = llll,
table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
table foot = \bottomrule,
%>> list only matriculation numbers greater than 20000
% and grade less than 4.0 <<
full filter = \ifnumgreater{\matriculation}{20000}
{\ifdimless{\grade pt}{4.0pt}{\csvfilteraccept}{\csvfilterreject}}
{\csvfilterreject},
]{grade.csv}{}{%
\thecsvrow & \slshape\name, \givenname & \matriculation & \grade
}
23
3.5 Line Range
Applying a line range means to select certain line numbers to be displayed. These line numbers
are not necessarily line numbers of the input file, see \thecsvinputline→ P. 15 , but line numbers
of type \thecsvrow→ P. 15 .
For example, if a filter was applied, see Section 3.4 on page 20, and 42 lines are accepted, a
range could select the first 20 of them or line 10 to 30 of the accepted lines.
N 2021-06-29 /csvsim/range={⟨range1 ⟩,⟨range2 ⟩,⟨range3 ⟩,... } (no default, initially empty)
Defines a comma separated list of line ranges. If a line number \thecsvrow→ P. 15 satisfies
one or more of the given ⟨range1 ⟩, ⟨range2 ⟩, . . . , the corresponding line is processed and
displayed. If /csvsim/range is set to empty, all lines are accepted.
Every ⟨range⟩ can corresponds to one of the following variants:
⟨a⟩-⟨b⟩ meaning line numbers ⟨a⟩ to ⟨b⟩.
⟨a⟩- meaning line numbers ⟨a⟩ to \c_max_int=2 147 483 647.
-⟨b⟩ meaning line numbers 1 to ⟨b⟩.
- meaning line numbers 1 to 2 147 483 647 (inefficient; don’t use).
⟨a⟩ meaning line numbers ⟨a⟩ to ⟨a⟩ (i.e. only ⟨a⟩).
⟨a⟩+⟨d⟩ meaning line numbers ⟨a⟩ to ⟨a⟩+⟨d⟩−1.
⟨a⟩+ meaning line numbers ⟨a⟩ to ⟨a⟩ (i.e. only ⟨a⟩).
+⟨d⟩ meaning line numbers 1 to ⟨d⟩.
+ meaning line numbers 1 to 1 (i.e. only 1; weird).
% \usepackage{booktabs}
\csvreader[
head to column names,
range = 2-3,
tabular = llll,
table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
table foot = \bottomrule,
]{grade.csv}{}{%
\thecsvrow & \slshape\name, \givenname & \matriculation & \grade
}
% \usepackage{booktabs}
\csvreader[
head to column names,
range = 3-,
tabular = llll,
table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
table foot = \bottomrule,
]{grade.csv}{}{%
\thecsvrow & \slshape\name, \givenname & \matriculation & \grade
}
24
% \usepackage{booktabs}
\csvreader[
head to column names,
range = 2+2,
tabular = llll,
table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
table foot = \bottomrule,
]{grade.csv}{}{%
\thecsvrow & \slshape\name, \givenname & \matriculation & \grade
}
% \usepackage{booktabs}
\csvreader[
head to column names,
range = {2,4},
tabular = llll,
table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
table foot = \bottomrule,
]{grade.csv}{}{%
\thecsvrow & \slshape\name, \givenname & \matriculation & \grade
}
To select the last n lines, you have to know or count the line numbers first. The following
example displays the last three line numbers:
% \usepackage{booktabs}
\csvreader{grade.csv}{}{}% count line numbers
\csvreader[
head to column names,
range = {\thecsvrow-2}-,
tabular = llll,
table head = \toprule & \bfseries Name & \bfseries Matr & \bfseries Grade\\\midrule,
table foot = \bottomrule,
]{grade.csv}{}{%
\thecsvrow & \slshape\name, \givenname & \matriculation & \grade
}
25
3.6 Table Support
3.6.1 Predefined Tables
26
3.6.2 Additional Options for Tables
27
3.6.3 Generic Tables
In Section section 3.6.1 on page 26, several tabular-like environments are described with prede-
fined keys. The following keys allow to use further tabular-like environments with configurable
names and options.
N 2021-09-09 /csvsim/generic table=⟨name⟩ (no default, initially empty)
Surrounds the CSV processing with \begin{⟨name⟩} at begin and with \end{⟨name⟩} at
end. Additionally, the commands defined by the key values of /csvsim/before table→ P. 27 ,
/csvsim/table head→ P. 27 , /csvsim/table foot→ P. 27 , and /csvsim/after table→ P. 27
are executed at the appropriate places. /csvsim/late after line→ P. 16 is set to \\.
If the environment ⟨name⟩ takes options, these have to be set using /csvsim/generic
table options.
28
3.6.4 General Survey Tables
The following auto options are the counterparts for the respective quick overview commands like
\csvautotabular→ P. 10 . They are listed for completeness, but are unlikely to be used directly.
U 2022-02-01 /csvsim/autotabular=⟨file name⟩ (no default)
/csvsim/autotabular*=⟨file name⟩ (no default)
Reads the whole CSV file denoted ⟨file name⟩ with an automated formatting. The star
variant treats the first line as data line and not as header line.
U 2022-02-01 /csvsim/autolongtable=⟨file name⟩ (no default)
/csvsim/autolongtable*=⟨file name⟩ (no default)
Reads the whole CSV file denoted ⟨file name⟩ with an automated formatting using the
required longtable package. The star variant treats the first line as data line and not as
header line.
U 2022-02-01 /csvsim/autobooktabular=⟨file name⟩ (no default)
/csvsim/autobooktabular*=⟨file name⟩ (no default)
Reads the whole CSV file denoted ⟨file name⟩ with an automated formatting using the
required booktabs package. The star variant treats the first line as data line and not as
header line.
U 2022-02-01 /csvsim/autobooklongtable=⟨file name⟩ (no default)
/csvsim/autobooklongtable*=⟨file name⟩ (no default)
Reads the whole CSV file denoted ⟨file name⟩ with an automated formatting using the
required booktabs and longtable packages. The star variant treats the first line as data
line and not as header line.
29
3.7 Special Characters
Be default, the CSV content is treated like normal LATEX text, see Subsection 5.3 on page 47.
But, TEX special characters of the CSV content may also be interpreted as normal characters
(\catcode 12, other), if one or more of the following options are used.
/csvsim/respect tab=true|false (default true, initially false)
If this key is set, every tabulator sign inside the CSV content is a normal character.
/csvsim/respect percent=true|false (default true, initially false)
If this key is set, every percent sign "%" inside the CSV content is a normal character.
/csvsim/respect sharp=true|false (default true, initially false)
If this key is set, every sharp sign "#" inside the CSV content is a normal character.
/csvsim/respect dollar=true|false (default true, initially false)
If this key is set, every dollar sign "$" inside the CSV content is a normal character.
/csvsim/respect and=true|false (default true, initially false)
If this key is set, every and sign "&" inside the CSV content is a normal character.
/csvsim/respect backslash=true|false (default true, initially false)
If this key is set, every backslash sign "\" inside the CSV content is a normal character.
/csvsim/respect underscore=true|false (default true, initially false)
If this key is set, every underscore sign "_" inside the CSV content is a normal character.
/csvsim/respect tilde=true|false (default true, initially false)
If this key is set, every tilde sign "~" inside the CSV content is a normal character.
/csvsim/respect circumflex=true|false (default true, initially false)
If this key is set, every circumflex sign "^" inside the CSV content is a normal character.
/csvsim/respect leftbrace=true|false (default true, initially false)
If this key is set, every left brace sign "{" inside the CSV content is a normal character.
/csvsim/respect rightbrace=true|false (default true, initially false)
If this key is set, every right brace sign "}" inside the CSV content is a normal character.
/csvsim/respect all (style, no value, initially unset)
Set all special characters from above to normal characters. This means a quite verbatim
interpretation of the CSV content.
/csvsim/respect none (style, no value, initially set)
Do not change any special character from above to normal character.
30
3.8 Separators
/csvsim/separator=⟨sign⟩ (no default, initially comma)
Sets the ⟨sign⟩ which is treates as separator between the data values of a data line. Feasible
values are:
• comma: This is the initial value with ’,’ as separator.
• semicolon: Sets the separator to ’;’.
\csvautobooktabular[separator=semicolon]{testsemi.csv}
\csvautobooktabular[separator=pipe]{pipe.csv}
31
3.9 Miscellaneous
/csvsim/every csv (style, initially empty)
A meta key (style) definition which is used for every following CSV file. This definition can
be overwritten with user code.
% Sets a warning message for unfeasible data lines.
\csvstyle{every csv}{warn on column count error}
/csvsim/default (style)
A style definition which is used for every following CSV file which resets all settings to
default values4 . 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).
/csvsim/file=⟨file name⟩ (no default, initially unknown.csv)
Sets the ⟨file name⟩ of the CSV file to be processed. \csvreader→ P. 9 sets this option by a
mandatory parameter.
/csvsim/preprocessed file=⟨file name⟩ (no default, initially \jobname_sorted.csv)
Sets the ⟨file name⟩ of the CSV file which is the output of a preprocessor.
/csvsim/preprocessor=⟨macro⟩ (no default)
Defines a preprocessor for the given CSV file. The ⟨macro⟩ has to have two mandatory
arguments. The first argument is the original CSV file which is set by /csvsim/file. The
second argument is the preprocessed CSV file which is set by /csvsim/preprocessed file.
Typically, the ⟨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
\csvreader→ P. 9 or \csvloop→ P. 10 .
\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
}
See Subsection 3.10 on page 33 for a concrete sorting preprocessing implemented with an
external tool.
/csvsim/no preprocessing (style, no value, initially set)
Clears any preprocessing, i. e. preprocessing is switched of.
4
default is used because of the global nature of most settings.
32
3.10 Sorting
TEX/LATEX was not born under a sorting planet. csvsimple-l3 provides no sorting of data lines
by LATEX-methods since sorting can be done much faster and much better by external tools.
First, one should consider the appropriate place for sorting:
• CSV files may be sorted by a tool before the LATEX document is processed at all. If the
CSV data is not likely to change, this is the most efficient method.
• CSV files may be sorted by a tool every time before the LATEX document is compiled. This
could be automated by a shell script or some processing tool like arara.
• CSV files may be sorted on-the-fly by a tool during compilation of a LATEX document.
This is the most elegant but not the most efficient way.
The first two methods are decoupled from anything concerning csvsimple-l3. For the third
method, the /csvsim/preprocessor→ P. 32 option is made for. This allows to access an external
tool for sorting. Which tool is your choice.
CSV-Sorter was written as a companion tool for csvsimple. It is an open source Java command-
line tool for sorting CSV files, available at
https://T-F-S.github.io/csvsorter/ or https://github.com/T-F-S/csvsorter
It can be used for all three sorting approaches described above. There is special support for
on-the-fly sorting with CSV-Sorter using the following options.
1. To use the sorting options, you have to install CSV-Sorter before!
2. You have to give permission to call external tools during compilation, i. e. the
command-line options for latex have to include -shell-escape.
\csvset{csvsorter configpath=xmlfiles}
\csvset{csvsorter log=outdir/csvsorter.log}
33
/csvsim/csvsorter token=⟨file name⟩ (no default, initially \jobname.csvtoken)
Sets ⟨file name⟩ as token file. This is an auxiliary file which communicates the success of
CSV-Sorter to csvsimple.
\csvset{csvsorter log=outdir/\jobname.csvtoken}
% \usepackage{booktabs}
\csvreader[
head to column names,
sort by = namesort.xml,
tabular = >{\color{red}}lllll,
table head = \toprule Name & Given Name & Matriculation & Gender & Grade\\\midrule,
table foot = \bottomrule
]{grade.csv}{}{%
\csvlinetotablerow
}
34
Second example: To sort our example grade.csv file according to grade, we use the
following XML configuration file. Further, persons with the same grade are sorted by name
and givenname. Since CSV-Sorter uses double quotes as default brackets for column values,
we remove bracket recognition to avoid a clash with the escaped umlauts of the example
CSV file.
Configuration file «gradesort.xml»
<?xml version="1.0" encoding="UTF-8"?>
<csv>
<bracket empty="true" />
<sortlines>
<column name="grade" order="ascending" type="double"/>
<column name="name" order="ascending" type="string"/>
<column name="givenname" order="ascending" type="string"/>
</sortlines>
</csv>
% \usepackage{booktabs}
\csvreader[
head to column names,
sort by = gradesort.xml,
tabular = llll>{\color{red}}l,
table head = \toprule Name & Given Name & Matriculation & Gender & Grade\\\midrule,
table foot = \bottomrule
]{grade.csv}{}{%
\csvlinetotablerow
}
35
Third example: To generate a matriculation/grade list, we sort our example grade.csv
file using the following XML configuration file. Again, since CSV-Sorter uses double quotes
as default brackets for column values, we remove bracket recognition to avoid a clash with
the escaped umlauts of the example CSV file.
Configuration file «matriculationsort.xml»
<?xml version="1.0" encoding="UTF-8"?>
<csv>
<bracket empty="true" />
<sortlines>
<column name="matriculation" order="ascending" type="integer"/>
</sortlines>
</csv>
% \usepackage{booktabs}
\csvreader[
head to column names,
sort by = matriculationsort.xml,
tabular = >{\color{red}}ll,
table head = \toprule Matriculation & Grade\\\midrule,
table foot = \bottomrule
]{grade.csv}{}{%
\matriculation & \grade
}
Matriculation Grade
12345 1.0
19202 3.3
23456 2.3
34567 5.0
36
/csvsim/new sorting rule={⟨name⟩}{⟨file name⟩} (style, initially unset)
This is a convenience option to generate a new shortcut for often used /csvsim/sort
by→ P. 34 applications. It also adds a more semantic touch. The new shortcut option is
sort by ⟨name⟩ which expands to sort by={⟨file name⟩} .
Consider the following example:
\csvautotabular[sort by=namesort.xml]{grade.csv}
A good place for setting up a new sorting rule would be inside the preamble:
\csvautotabular[sort by name]{grade.csv}
\csvsortingrule{name}{namesort.xml}
\csvautotabular[sort by name]{grade.csv}
37
3.11 Data Collection
csvsimple-l3 reads and processes a CSV file line by line. Accordingly, the TEX input stream is
filled line by line. Although this is an efficient procedure, for some applications like tables with
the tabularray package, collecting the data from the CSV file into a macro is needed. This
macro can be given to the target application for further processing.
N 2021-07-06 /csvsim/collect data=true|false (default true, initially false)
csvsimple-l3 provides limited and experimental support to collect the input data
from the CSV file plus user additions into a macro named \csvdatacollection→ P. 39 .
Setting /csvsim/collect data adds the contents of the following keys to
\csvdatacollection→ P. 39 :
• /csvsim/after head→ P. 16
• /csvsim/after line→ P. 17
• /csvsim/before first line→ P. 16
• /csvsim/before line→ P. 16
• /csvsim/late after first line→ P. 16
• /csvsim/late after head→ P. 16
• /csvsim/late after last line→ P. 16
• /csvsim/late after line→ P. 16
Also, the expanded content of
• /csvsim/command→ P. 17
is added to \csvdatacollection. Note that for /csvsim/command→ P. 17 special care has
to be taken what should be protected from expansion and what not. Observe the following
hints for /csvsim/command→ P. 17 :
• For data macros like \csvcoli use \csvexpval\csvcoli to add the value of this macro
to \csvdatacollection→ P. 39 . This is optional, if \csvcoli contains numbers or text
without active characters, but essential, if it contains macros.
• \csvlinetotablerow→ P. 15 is to be used without \csvexpval.
• For macros like \textbf use \csvexpnot\textbf to prevent expansion.
• Using computations or not expandable conditionals may likely cause compilation er-
rors.
\csvreader[
collect data,
head to column names,
late after line=\\,
late after last line=,
]{grade.csv}{}{%
\thecsvrow. \csvexpval\givenname\ \csvexpnot\textbf{\csvexpval\name}
}
Collected data:\par
\csvdatacollection
Collected data:
1. Hans Maier
2. Anna Huber
3. Werner Weißbäck
4. Maria Bauer
Note that data collection is limited to some special cases and does not allow to save all
possible content. Table options like /csvsim/longtable→ P. 26 are generally not supported
with the important exception of /csvsim/tabularray→ P. 26 which uses /csvsim/collect
data automatically.
See Section 5.6 on page 54 for examples.
38
N 2021-07-06 /csvsim/data collection=⟨macro⟩ (no default, initially \csvdatacollection)
Sets the collection macro to an alternative for \csvdatacollection.
data collection = \myData, % instead of \csvdatacollection
N 2021-07-06 \csvdatacollection
Macro which contains the collected data of a CSV file processed with /csvsim/collect
data→ P. 38 . This macro name can be changed by setting /csvsim/data collection.
N 2021-07-06 \csvexpval⟨macro⟩
Recovers the content of the given ⟨macro⟩ and prevents further expansion. This is a wrapper
for \exp_not:V. Alternatively, \expandonce from etoolbox could be used.
N 2021-07-06 \csvexpnot⟨macro⟩
Prevents the expansion of the given ⟨macro⟩. This is a wrapper for \exp_not:N. Alterna-
tively, \noexpand could be used.
The following macros can only be used inside keys which are not collected to
\csvdatacollection, e.g. inside /csvsim/after filter→ P. 16 .
N 2021-07-06 \csvcollectn{⟨code⟩}
Appends the given ⟨code⟩ to \csvdatacollection.
This corresponds to \tl_gput_right:Nn.
N 2021-07-06 \csvcollectx{⟨code⟩}
Appends the expansion of the given ⟨code⟩ to \csvdatacollection.
This corresponds to \tl_gput_right:Nx.
N 2021-07-06 \csvcollectV⟨macro⟩
Appends the content of the given ⟨macro⟩ to \csvdatacollection.
This corresponds to \tl_gput_right:NV.
39
4 String and Number Tests
The following string tests are complementing the string tests from packages like etoolbox. They
all do the same, i.e., comparing expanded strings for equality. To some extent, they are provided
for backward compatibility.
• \ifcsvstrcmp may be the most efficient method, because it uses the native compiler string
comparison (if available).
• \ifcsvstrequal does not rely on a compiler. It also is the fallback implementation for
\ifcsvstrcmp, if there is no native comparison method.
• \ifcsvprostrequal is possibly more failsafe than the other two string tests. It may be
used, if strings contain dirty things like \textbf{A}.
N 2016-07-01 \ifcsvstrcmp{⟨stringA⟩}{⟨stringB⟩}{⟨true⟩}{⟨false⟩}
U 2021-06-28
Compares two strings and executes ⟨true⟩ if they are equal, and ⟨false⟩ otherwise. The
comparison is done using \str_compare:eNeTF. \ifcsvstrcmp is expandable.
N 2016-07-01 \ifcsvnotstrcmp{⟨stringA⟩}{⟨stringB⟩}{⟨true⟩}{⟨false⟩}
U 2021-06-28
Compares two strings and executes ⟨true⟩ if they are not equal, and ⟨false⟩ otherwise. The
implementation uses \ifcsvstrcmp. \ifcsvstrcmp is expandable.
N 2016-07-01 \ifcsvstrequal{⟨stringA⟩}{⟨stringB⟩}{⟨true⟩}{⟨false⟩}
U 2021-06-28
Compares two strings and executes ⟨true⟩ if they are equal, and ⟨false⟩ otherwise. The
strings are expanded and the comparison is done using \tl_if_eq:NNTF. \ifcsvstrequal
is not expandable.
N 2016-07-01 \ifcsvprostrequal{⟨stringA⟩}{⟨stringB⟩}{⟨true⟩}{⟨false⟩}
U 2021-06-28
Compares two strings and executes ⟨true⟩ if they are equal, and ⟨false⟩ otherwise.
The strings are expanded with \protected@edef in the test, i.e. parts of the strings
which are protected stay unexpanded. The comparison is done using \tl_if_eq:NNTF.
\ifcsvprostrequal is not expandable.
The following number tests are wrappers for corresponding LATEX3 conditionals.
N 2021-06-28 \ifcsvfpcmp{⟨floating point expression⟩}{⟨true⟩}{⟨false⟩}
Evaluates the given ⟨floating point expression⟩ and executes ⟨true⟩ or ⟨false⟩ appropriately.
The evaluation is done using \fp_compare:nTF. \ifcsvfpcmp is expandable.
N 2021-06-28 \ifcsvintcmp{⟨integer expression⟩}{⟨true⟩}{⟨false⟩}
Evaluates the given ⟨integer expression⟩ and executes ⟨true⟩ or ⟨false⟩ appropriately. The
evaluation is done using \int_compare:nTF. \ifcsvintcmp is expandable.
40
5 Examples
5.1 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.
CSV file «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ße 32, Hinterschlag},10020,Örtingstetten,30
Weißbä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
Firstly, we survey the file content quickly using \csvautotabular. As can be seen, unfeasible
lines are ignored automatically.
\tiny\csvautotabular{address.csv}
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.
41
% this example requires the tcolorbox package
\newcommand{\ifmale}[2]{\ifcsvstrcmp{\gender}{m}{#1}{#2}}
Letter to Schmitt
Dear Sir,
we are pleased to announce you a bonus value
of 60% which will be delivered to Œresung
soon.
...
42
5.2 A Graphical Presentation
For this example, we use some artificial statistical data given by a CSV file.
CSV file «data.csv»
land,group,amount
Bayern,A,1700
Baden-Württemberg,A,2300
Sachsen,B,1520
Thüringen,A,1900
Hessen,B,2100
The amount values are presented in the following diagram by bars where the group classification
is given using different colors.
Bayern 1700
Baden-Württemberg 2300
Sachsen 1520
Thüringen 1900
Hessen 2100
43
It would be nice to sort the bars by length, i. e. to sort the CSV file by the amount column. If
the CSV-Sorter program is properly installed, see Subsection 3.10 on page 33, this can be done
with the following configuration file for CSV-Sorter:
Configuration file «amountsort.xml»
<?xml version="1.0" encoding="UTF-8"?>
<csv>
<bracket empty="true" />
<sortlines>
<column name="amount" order="descending" type="double"/>
<column name="land" order="ascending" type="string"/>
</sortlines>
</csv>
Baden-Württemberg 2300
Hessen 2100
Thüringen 1900
Bayern 1700
Sachsen 1520
44
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.
% 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}%
}
Baden-Württemberg
Bayern
2300
1700
Sachsen 1520
2100
1900
Hessen
Thüringen
45
Finally, the filter option is demonstrated by separating the groups A and B. Every item is piled
upon the appropriate stack.
\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}
Thüringen (1900)
Baden-Württemberg (2300)
Hessen (2100)
Group A Group B
46
5.3 Macro code inside the data
If needed, the data file may contain macro code.
CSV file «macrodata.csv»
type,description,content
M,A nice \textbf{formula}, $\displaystyle \int\frac{1}{x} = \ln|x|+c$
G,A \textcolor{red}{colored} ball, {\tikz \shadedraw [shading=ball] (0,0) circle (.5cm);}
M,\textbf{Another} formula, $\displaystyle \lim\limits_{n\to\infty} \frac{1}{n}=0$
\csvautobooktabular{macrodata.csv}
G A colored ball
1
M Another formula lim =0
n→∞ n
\csvreader[my enumerate]{macrodata.csv}{}{%
\item \description:\par\content}
\bigskip
Now, formulas only:
\csvreader[my enumerate,filter strcmp={\type}{M}]{macrodata.csv}{}{%
\item \description:\qquad\content}
Z nice formula:
1. A
1
= ln x + c
x
2. A colored ball:
3. Another formula:
1
lim =0
n→∞ n
47
5.4 Tables with Number Formatting
We consider a file with numerical data which should be pretty-printed.
CSV file «data_numbers.csv»
month, dogs, cats
January, 12.50,12.3e5
February, 3.32, 8.7e3
March, 43, 3.1e6
April, 0.33, 21.2e4
May, 5.12, 3.45e6
June, 6.44, 6.66e6
July, 123.2,7.3e7
August, 12.3, 5.3e4
September,2.3, 4.4e4
October, 6.5, 6.5e6
November, 0.55, 5.5e5
December, 2.2, 3.3e3
The siunitx package provides a huge amount of formatting options for numbers. A good and
robust way to apply formatting by siunitx inside tables generated by csvsimple-l3 is the
\tablenum macro from siunitx.
% \usepackage{siunitx,array,booktabs}
\csvreader[
head to column names,
before reading = \begin{center}\sisetup{table-number-alignment=center},
tabular = cc,
table head = \toprule \textbf{Cats} & \textbf{Dogs} \\\midrule,
table foot = \bottomrule,
after reading = \end{center}
]{data_numbers.csv}{}{%
\tablenum[table-format=2.2e1]{\cats} & \tablenum{\dogs}
}
Cats Dogs
5
12.3 × 10 12.50
8.7 × 103 3.32
3.1 × 106 43
21.2 × 104 0.33
3.45 × 106 5.12
6.66 × 106 6.44
7.3 × 107 123.2
5.3 × 104 12.3
4.4 × 104 2.3
6.5 × 106 6.5
5.5 × 105 0.55
3.3 × 103 2.2
48
It is also possible to create on-the-fly tables using calcations of the given data. The following
example shows cat values bisected and dog values doubled.
% \usepackage{siunitx,array,booktabs,xfp}
\csvreader[
head to column names,
before reading = \begin{center}\sisetup{table-number-alignment=center},
tabular = cccc,
table head = \toprule \textbf{Cats} & \textbf{Dogs}
& \textbf{Halfcats} & \textbf{Doubledogs} \\\midrule,
table foot = \bottomrule,
after reading = \end{center}
]{data_numbers.csv}{}{%
\tablenum[table-format=2.2e1]{\cats} & \tablenum{\dogs}
& \tablenum[exponent-mode=scientific, round-precision=3,
round-mode=places, table-format=1.3e1]{\fpeval{\cats/2}}
& \tablenum{\fpeval{\dogs*2}}
}
49
The siunitx package also provides a new column type S which can align material using a
number of different strategies. Special care is needed, if the first or the last column is to be
formatted with the column type S. The number detection of siunitx is disturbed by the line
reading code of csvsimple-l3 which actually is present at the first and last column. To avoid
this problem, the utilization of \tablenum is appropriate, see above. Alternatively, a very nifty
workaround suggested by Enrico Gregorio is to add an invisible dummy column with c@{} as
first column and @{}c as last column:
% \usepackage{siunitx,array,booktabs}
\csvreader[
head to column names,
before reading = \begin{center}\sisetup{table-number-alignment=center},
tabular = {c@{}S[table-format=2.2e1]S@{}c},
table head = \toprule & \textbf{Cats} & \textbf{Dogs} & \\\midrule,
table foot = \bottomrule,
after reading = \end{center}
]{data_numbers.csv}{}{%
& \cats & \dogs &
}
Cats Dogs
5
12.3 × 10 12.50
8.7 × 103 3.32
3.1 × 106 43
21.2 × 104 0.33
3.45 × 106 5.12
6.66 × 106 6.44
7.3 × 107 123.2
5.3 × 104 12.3
4.4 × 104 2.3
6.5 × 106 6.5
5.5 × 105 0.55
3.3 × 103 2.2
50
Now, the preceding table shall be sorted by the cats values. If the CSV-Sorter program is prop-
erly installed, see Subsection 3.10 on page 33, this can be done with the following configuration
file for CSV-Sorter:
Configuration file «catsort.xml»
<?xml version="1.0" encoding="UTF-8"?>
<csv>
<bracket empty="true" />
<sortlines>
<column name="cats" order="ascending" type="double"/>
</sortlines>
</csv>
% \usepackage{siunitx,array,booktabs}
% Also, the CSV-Sorter tool has to be installed
\csvreader[
head to column names,
sort by = catsort.xml,
before reading = \begin{center}\sisetup{table-number-alignment=center},
tabular = lcc,
table head = \toprule \textbf{Month} & \textbf{Dogs} & \textbf{Cats} \\\midrule,
table foot = \bottomrule,
after reading = \end{center}
]{data_numbers.csv}{}{%
\month & \tablenum{\dogs} & \tablenum[table-format=2.2e1]{\cats}
}
51
5.5 CSV data without header line
CSV files with a header line are more semantic than files without header, but it’s no problem
to work with headless files.
For this example, we use again some artificial statistical data given by a CSV file but this time
without header.
CSV file «data_headless.csv»
Bayern,A,1700
Baden-Württemberg,A,2300
Sachsen,B,1520
Thüringen,A,1900
Hessen,B,2100
Note that you cannot use the /csvsim/no head→ P. 18 option for the auto tabular commands. If
no options are given, the first line is interpreted as header line which gives an unpleasant result:
\csvautobooktabular{data_headless.csv}
Bayern A 1700
Baden-Württemberg A 2300
Sachsen B 1520
Thüringen A 1900
Hessen B 2100
To get the expected result, the star versions of the auto tabular commands can be used.
\csvautobooktabular*{data_headless.csv}
Bayern A 1700
Baden-Württemberg A 2300
Sachsen B 1520
Thüringen A 1900
Hessen B 2100
This example can be extended to insert a table head for this headless data:
\csvautobooktabular*[
table head=\toprule\bfseries Land & \bfseries Group
& \bfseries Amount\\\midrule
]{data_headless.csv}
52
For the normal \csvreader→ P. 9 command, the /csvsim/no head→ P. 18 option should be ap-
plied. Of course, we cannot use /csvsim/head to column names→ P. 18 because there is no
head, but the columns can be addressed by their numbers:
\csvreader[
no head,
tabular = lr,
table head = \toprule\bfseries Land & \bfseries Amount\\\midrule,
table foot = \bottomrule]
{data_headless.csv}
{ 1=\land, 3=\amount }
{\land & \amount}
Land Amount
Bayern 1700
Baden-Württemberg 2300
Sachsen 1520
Thüringen 1900
Hessen 2100
53
5.6 Tables with tabularray
The tabularray package gives extended control for generating tables. /csvsim/tabularray→ P. 26
and /csvsim/centered tabularray→ P. 26 support such tables. A distinctiveness is that for
tabularray data from a CSV file has to be collected first (into a macro) and applied after-
wards. The process is hidden from the user view, but has to be taken into account when
/csvsim/command→ P. 17 is set up, see Section 3.11 on page 38.
The following examples uses data.csv from Section 5.2 on page 43.
% \usepackage{tabularray,siunitx,xfp}
\csvreader[
head to column names,
centered tabularray =
{
rowsep = 1mm,
colsep = 5mm,
rows = {blue7},
hlines = {2pt, white},
vlines = {2pt, white},
row{1} = {bg=azure3, fg=white, font=\bfseries\large, 8mm},
},
table head = {\SetCell[c=4]{c} Important Data Table \\},
]{data.csv}{}{
\ifcsvstrcmp{\group}{A}{\csvexpnot\SetRow{brown7}}{}
\csvexpnot\SetCell{bg=purple7}
\csvexpval\land
& \csvexpval\group
& \csvexpval\amount
& \tablenum[exponent-mode=scientific, round-precision=3,
round-mode=places, table-format=1.3e1]{\fpeval{pi*\amount}}
}
54
Filters and line ranges can be used for tabularray and all data collections without restriction:
% \usepackage{tabularray}
Display group `A` only:\par
\csvreader[
head to column names,
filter strcmp = {\group}{A},
centered tabularray =
{
rowsep = 1mm,
colsep = 5mm,
column{1} = {r, fg=yellow5, colsep=2pt},
column{2} = {r, yellow8!10, font=\bfseries},
column{3} = {l, yellow8},
hlines = {2pt, white},
},
]{data.csv}{}{
\thecsvrow
& \csvexpval\land
& \csvexpval\amount
}
2 Baden-Württemberg 2300
3 Thüringen 1900
% \usepackage{tabularray}
Display data from line 3 on:\par
\csvreader[
head to column names,
range = 3-,
centered tabularray =
{
rowsep = 1mm,
colsep = 5mm,
column{1} = {r, fg=violet5, colsep=2pt},
column{2} = {r, violet8!10, font=\bfseries},
column{3} = {l, violet8},
hlines = {2pt, white},
},
]{data.csv}{}{
\thecsvrow
& \csvexpval\land
& \csvexpval\amount
}
4 Thüringen 1900
5 Hessen 2100
55
The following example displays a tabularray variant of the \csvautotabular→ P. 10 command.
Note the /csvsim/no head→ P. 18 option to read the first line as data line. Also note that
\csvlinetotablerow→ P. 15 is used without preceding modifier.
% \usepackage{tabularray}
\csvreader[
no head,
centered tabularray =
{
row{odd} = {blue!85!gray!7},
row{1} = {blue!50!gray!25, font=\bfseries},
},
table head = {\hline[0.1em,blue!50!black]},
table foot = {\hline[0.1em,blue!50!black]},
late after first line = {\\\hline[blue!50!black]},
]{data.csv}{}{%
\csvlinetotablerow%
}
56
5.7 Imported CSV data
If data is imported from other applications, there is not always a choice to format in comma
separated values with curly brackets.
Consider the following example data file:
CSV file «imported.csv»
"name";"address";"email"
"Frank Smith";"Yellow Road 123, Brimblsby";"frank.smith@organization.org"
"Mary May";"Blue Alley 2a, London";"mmay@maybe.uk"
"Hans Meier";"Hauptstraße 32, Berlin";"hans.meier@corporation.de"
If the CSV-Sorter program is properly installed, see Subsection 3.10 on page 33, this can be
transformed on-the-fly with the following configuration file for CSV-Sorter:
Configuration file «transform.xml»
<?xml version="1.0" encoding="UTF-8"?>
<csv>
<bracket leftsymbol="doublequote" rightsymbol="doublequote" />
<delimiter signsymbol="semicolon" />
<outBracket leftsymbol="braceleft" rightsymbol="braceright" />
<outDelimiter signsymbol="comma" />
</csv>
Now, we just have to add an option sort by=transform.xml to transform the input data. Here,
we actually do not sort.
% \usepackage{booktabs,array}
% Also, the CSV-Sorter tool has to be installed
\newcommand{\Header}[1]{\normalfont\bfseries #1}
\csvreader[
sort by = transform.xml,
tabular = >{\itshape}ll>{\ttfamily}l,
table head = \toprule\Header{Name} & \Header{Address} & \Header{email}\\\midrule,
table foot = \bottomrule
]
{imported.csv}{}
{\csvlinetotablerow}
The file which is generated on-the-fly and which is actually read by csvsimple-l3 is the follow-
ing:
{name},{address},{email}
{Frank Smith},{Yellow Road 123, Brimblsby},{frank.smith@organization.org}
{Mary May},{Blue Alley 2a, London},{mmay@maybe.uk}
{Hans Meier},{Hauptstraße 32, Berlin},{hans.meier@corporation.de}
57
5.8 Encoding
If the CSV file has a different encoding than the LATEX source file, then special care is needed.
• The most obvious treatment is to change the encoding of the CSV file or the LATEX source
file to match the other one (every good editor supports such a conversion). This is the
easiest choice, if there a no good reasons against such a step. E.g., unfortunately, several
tools under Windows need the CSV file to be cp1252 encoded while the LATEX source file
may need to be utf8 encoded.
• The inputenc package allows to switch the encoding inside the document, say from utf8
to cp1252. Just be aware that you should only use pure ASCII for additional texts inside
the switched region.
% !TeX encoding=UTF-8
% ....
\usepackage[utf8]{inputenc}
% ....
\begin{document}
% ....
\inputencoding{latin1}% only use ASCII from here, e.g. "Uberschrift
\csvreader[%...
]{data_cp1252.csv}{%...
}{% ....
}
\inputencoding{utf8}
% ....
\end{document}
• As a variant to the last method, the encoding switch can be done using options from
csvsimple-l3:
% !TeX encoding=UTF-8
% ....
\usepackage[utf8]{inputenc}
% ....
\begin{document}
% ....
% only use ASCII from here, e.g. "Uberschrift
\csvreader[%...
before reading=\inputencoding{latin1},
after reading=\inputencoding{utf8},
]{data_cp1252.csv}{%...
}{% ....
}
% ....
\end{document}
58
• If the CSV-Sorter program is properly installed, see Subsection 3.10 on page 33, the CSV
file can be re-encoded on-the-fly with the following configuration file for CSV-Sorter:
Configuration file «encoding.xml»
<?xml version="1.0" encoding="UTF-8"?>
<csv>
<noHeader/>
<bracket empty="true"/>
<charset in="windows-1252" out="UTF-8"/>
</csv>
% !TeX encoding=UTF-8
% ....
\usepackage[utf8]{inputenc}
% ....
\begin{document}
% ....
\csvreader[%...
sort by=encoding.xml,
]{data_cp1252.csv}{%...
}{% ....
}
% ....
\end{document}
59
Index
after filter key, 16 \csvnames, 12
after first line key, 17 \csvreader, 9
after head key, 16 \csvset, 12
after line key, 17 csvsorter command key, 33
after reading key, 17 csvsorter configpath key, 33
after table key, 27 csvsorter log key, 33
autobooklongtable key, 29 csvsorter token key, 34
autobooklongtable* key, 29 \csvsortingrule, 37
autobooktabular key, 29 \csvstyle, 12
autobooktabular* key, 29
autolongtable key, 29 data collection key, 39
autolongtable* key, 29 default key, 32
autotabular key, 29
autotabular* key, 29 every csv key, 32
60
autobooklongtable*, 29 no table, 26
autobooktabular, 29 on column count error, 19
autobooktabular*, 29 preprocessed file, 32
autolongtable, 29 preprocessor, 32
autolongtable*, 29 range, 24
autotabular, 29 respect all, 30
autotabular*, 29 respect and, 30
before filter, 16 respect backslash, 30
before first line, 16 respect circumflex, 30
before line, 16 respect dollar, 30
before reading, 16 respect leftbrace, 30
before table, 27 respect none, 30
centered tabbing, 26 respect percent, 30
centered tabular, 26 respect rightbrace, 30
centered tabularray, 26 respect sharp, 30
check column count, 19 respect tab, 30
collect data, 38 respect tilde, 30
column count, 19 respect underscore, 30
column names, 18 separator, 31
column names detection, 18 sort by, 34
column names reset, 18 tabbing, 26
command, 17 table centered, 27
csvsorter command, 33 table foot, 27
csvsorter configpath, 33 table head, 27
csvsorter log, 33 tabular, 26
csvsorter token, 34 tabularray, 26
data collection, 39 warn on column count error, 19
default, 32
every csv, 32 late after first line key, 16
file, 32 late after head key, 16
filter accept all, 23 late after last line key, 16
filter bool, 21 late after line key, 16
filter equal, 20 long tabularray key, 26
filter expr, 22 longtable key, 26
filter fp, 20
filter ifthen, 23 new sorting rule key, 37
filter not equal, 20 no check column count key, 19
filter not strcmp, 20 no filter key, 23
filter reject all, 23 no head key, 18
filter strcmp, 20 no preprocessing key, 32
filter test, 22 no table key, 26
full filter, 23
on column count error key, 19
generic collected table, 28
generic table, 28 pipe value, 31
generic table options, 28 preprocessed file key, 32
head, 18 preprocessor key, 32
head to column names, 18
head to column names prefix, 18 range key, 24
late after first line, 16 respect all key, 30
late after head, 16 respect and key, 30
late after last line, 16 respect backslash key, 30
late after line, 16 respect circumflex key, 30
long tabularray, 26 respect dollar key, 30
longtable, 26 respect leftbrace key, 30
new sorting rule, 37 respect none key, 30
no check column count, 19 respect percent key, 30
no filter, 23 respect rightbrace key, 30
no head, 18 respect sharp key, 30
no preprocessing, 32 respect tab key, 30
61
respect tilde key, 30
respect underscore key, 30
semicolon value, 31
separator key, 31
sort by key, 34
tab value, 31
tabbing key, 26
table centered key, 27
table foot key, 27
table head key, 27
tabular key, 26
tabularray key, 26
\thecsvcolumncount, 15
\thecsvinputline, 15
\thecsvrow, 9, 15
Values
comma, 31
pipe, 31
semicolon, 31
tab, 31
62