# Create table from .csv file, Header line too long

I have a big table that I want to put into my latex Document. The problem that I have is that the header line (the top line) for the column names is too long. This is my state.csv file

game id,start,dimensions,number of players,error type,survived rounds,avg iter dur,max iter dur
1,,"(46, 58)",6,____,3,0.0189638150252525,0.070585
2,,"(73, 54)",3,____,59,0.0081690392435849,1.142459
3,,"(62, 65)",3,____,107,0.0101505997288269,2.223196
4,,"(44, 75)",5,____,25,0.0157099498952178,2.771
5,,"(41, 41)",3,____,63,0.0062615696206158,1.012879
6,,"(42, 71)",5,____,63,0.006719767371205704,1.936


For example: Latex will write number of players in one line but I want that latex inserts a linebreak after number or of in order to make the table not so wide. I also looked into the csvmanual but I did not find any suggestion on how to deal with this. https://osl.ugr.es/CTAN/macros/latex/contrib/csvsimple/csvsimple.pdf

With \csvautotabular[]{state.csv} the table looks like this:

As you can see the table is longer than the paper width and the values inside the table are missing. How can I fit the table nicely into my Latex document?

One way is to play with the tabular environment rather than csv as you can determine easily the size of your cells:

\documentclass{article}

\usepackage{caption}

\begin{document}

\begin{center}
\begin{table}[h!]
\begin{tabular}{||p{1cm}|p{1cm}|p{1.7cm}|p{1.5cm}|p{1.5cm}|p{1.5cm}|p{1.3cm}|p{1.3cm}|}
\hline
game id&start&dimensions&number of players&error type&survived rounds&avg iter dur&max iter dur
\\
\hline\hline

\hline
\end{tabular}
\caption[]{}
\label{}
\end{table}
\end{center}
\end{document}


will give you something like

Then you can play with the size that it fits exactly what you want. You can customize it easily. Then you can enter every line of your data. For long data, you can also use \usepackage{array}

A tutorial for the tabular environment:https://www.overleaf.com/learn/latex/tables

• Hey, thank you for your reply. It is correct to resize the column width with the p{1.5cm} option but how can i then automatically read the .csv file in with these appropriate width options that i configured? Jan 12, 2021 at 11:47

Your .csv file is badly formatted. First of all the _ symbol cannot be used in text mode. I changed it to -. Secondly, according to the csvsimple documentation:

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 mwe example below shows two of the many solutions made possible by the csvsimple package.

\documentclass{article}
\usepackage[a4paper,margin=1cm]{geometry}
\usepackage{csvsimple}
\usepackage{array,booktabs}

\begin{filecontents*}[overwrite]{test.csv}
game id,start,dimensions,number of players,error type,survived rounds,avg iter dur,max iter dur
1,,{(46, 58)},6,--,3,0.0189638150252525,0.070585
1,,{(46, 58)},6,--,3,0.0189638150252525,0.070585
2,,{(73, 54)},3,--,59,0.0081690392435849,1.142459
3,,{(62, 65)},3,--,107,0.0101505997288269,2.223196
4,,{(44, 75)},5,--,25,0.0157099498952178,2.771
5,,{(41, 41)},3,--,63,0.0062615696206158,1.012879
6,,{(42, 71)},5,--,63,0.006719767371205704,1.936
\end{filecontents*}

\begin{document}

\section{A first simple solution}

\csvautotabular{test.csv}

\section{Another (not so simple) solution}

tabular=>{\centering}p{7mm}cc>{\centering}m{1.7cm}>{\centering}m{.8cm}>{\centering}m{1.4cm}ll,
table head=\toprule \bfseries game id & \bfseries start & \bfseries dimensions & \bfseries number of players
& \bfseries error type & \bfseries survived rounds & \bfseries  avg iter dur & \bfseries max iter dur\\\midrule,
table foot=\bottomrule]
{test.csv}%
{}%
{\csvcoli&\csvcolii&\csvcoliii&\csvcoliv&\csvcolv&\csvcolvi&\csvcolvii&\csvcolviii}

\end{document}

• Thank you Ivan, that helped me. Jan 13, 2021 at 19:46

I have found a workaround for your header using the makecell package. To insert a manual line break you can use the \thead command from this package. An MWE based on your csv file is provided here:

\documentclass{article}

\usepackage{csvsimple}
\usepackage{makecell}

\begin{document}
\begin{tabular}{|c|c|c|c|c|c|c|c|}
\hline
\hline
\csvreader[late after line=\\\hline] {state.csv}{game id = \gameId, start = \start, dimensions = \dimensions, number of players = \numberOfPlayers, error type = \errorType, survived rounds = \survivedRounds, avg iter dur = \avgIterDur, max iter dur = \maxIterDur}{\gameId & \start & \dimensions & \numberOfPlayers & \errorType & \survivedRounds & \avgIterDur & \maxIterDur}
\end{tabular}

\end{document}


The output looks as follows with your csv file:

But I couldn't find why the whole csv file is not imported. Also, the borders (vertical lines) in the 2nd row haven't appeared. I will edit the answer if I can find a solution to this.

Edit: As per the mentioned edits in the answer of Ivan, I edited your csv file and got the desired result as follows:

• The .csv file is not imported because it is badly formatted, as said in my answer
– Ivan
Jan 12, 2021 at 13:26