1

I was trying to import CSV data into a table. The CSV file has no header and I want to include a header in the table without editing the CSV file. How can I do that?

Here is an MWE based on my approach till now:

\documentclass{article}

\usepackage{csvsimple}
\usepackage{booktabs}

\begin{filecontents*}[overwrite]{test.csv}
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
\end{filecontents*}

\begin{document}
    \section*{Table with 12 columns}
    \csvreader[no head,
                tabular = *{12}{c}
                table head = \toprule\bfseries  Col a & \bfseries  Col b & \bfseries  Col c & \bfseries  Col d & \bfseries  Col e & \bfseries  Col f & \bfseries  Col g & \bfseries  Col h & \bfseries  Col i & \bfseries  Col j & \bfseries  Col k & \bfseries  Col l\\\midrule,
                table foot = \bottomrule]
                {test.csv}
                {1=\cola,2=\colb,3=\colc,4=\cold,5=\cole,6=\colf,7=\colg,8=\colh,9=\coli,10=\colj,11=\colk,12=\coll}
                {\cola & \colb & \colc & \cold & \cole & \colf & \colg & \colh & \coli & \colj & \colk & \coll}

    \section*{Table with 9 columns}
    \csvreader[no head,
                tabular = *{9}{c}
                table head = \toprule \bfseries  Col 1& \bfseries  Col 2& \bfseries  Col 3& \bfseries  Col 4& \bfseries  Col 5& \bfseries  Col 6& \bfseries  Col 7& \bfseries  Col 8& \bfseries  Col 9\\\midrule,
                table foot = \bottomrule]
                {test.csv}
                {1=\cola,2=\colb,3=\colc,4=\cold,5=\cole,6=\colf,7=\colg,8=\colh,9=\coli}
                {\cola & \colb & \colc & \cold & \cole & \colf & \colg & \colh & \coli}
\end{document}

Here is the erroneous output:

erroneous output

Also, you may notice that if I import 9 columns, all of those are imported. But if I import all the 12 columns only 10 are imported. How can this be fixed also?

0

2 Answers 2

1

Just add the missing commas (tabular = *{12}{c},)and use the latest version: cvsimple-l3

Added geometry to change the margins. Otherwise the table will overflow the page.

b

\documentclass{article}

\usepackage{csvsimple-l3} % new version

\usepackage{booktabs}

\usepackage[left=3.00cm, right=3.00cm, top=4.00cm, bottom=3.00cm]{geometry}% to change the margins

\begin{filecontents*}[overwrite]{test.csv}
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
\end{filecontents*}

\begin{document}
    \section*{Table with 12 columns}
    \csvreader[no head,
    tabular = *{12}{c}, %<<<<<<<<<<<<<<<<<<
    table head = \toprule\bfseries  Col a & \bfseries  Col b & \bfseries  Col c & \bfseries  Col d & \bfseries  Col e & \bfseries  Col f & \bfseries  Col g & \bfseries  Col h & \bfseries  Col i & \bfseries  Col j & \bfseries  Col k & \bfseries  Col l\\\midrule,
    table foot = \bottomrule]
    {test.csv}
    {1=\cola,2=\colb,3=\colc,4=\cold,5=\cole,6=\colf,7=\colg,8=\colh,9=\coli,10=\colj,11=\colk,12=\coll}
    {\cola & \colb & \colc & \cold & \cole & \colf & \colg & \colh & \coli & \colj & \colk & \coll}
    
    \section*{Table with 9 columns}
    \csvreader[no head,
    tabular = *{9}{c}, %<<<<<<<<<<<<<<<<<<
    table head = \toprule \bfseries  Col 1& \bfseries  Col 2& \bfseries  Col 3& \bfseries  Col 4& \bfseries  Col 5& \bfseries  Col 6& \bfseries  Col 7& \bfseries  Col 8& \bfseries  Col 9\\\midrule,
    table foot = \bottomrule]
    {test.csv}
    {1=\cola,2=\colb,3=\colc,4=\cold,5=\cole,6=\colf,7=\colg,8=\colh,9=\coli}
    {\cola & \colb & \colc & \cold & \cole & \colf & \colg & \colh & \coli}
        
    
\end{document}
1
  • Thanks. Wasn't this feature available in the previous version?
    – Imran
    Nov 9, 2021 at 21:49
0

Not sure you really have a problem here. Maybe I'm missing something.

(Your MWE was broken in that the generation of the CSV isn't actually part of the MWE; test.csv was just a prerequisite file)

Anyway, some review of working examples:

Reference (examples and so on) https://github.com/T-F-S/csvsimple/blob/master/doc/latex/csvsimple/csvsimple-l3.tex

About your question, I came up with this:

I've reduced your MWE to a very minimal working example:

First, the prerequisite:

$ cat > test.csv
1, 2
3, 4
5, 6
^D

Then the LaTeX:

\documentclass{article}

\usepackage{csvsimple}
\usepackage{booktabs}

\begin{document}

    \section*{Table with 2 columns}
    \csvreader[no head,
                tabular = {cc},
                table head = \toprule\bfseries  Col a & \bfseries  Col b \\ \midrule,
                table foot = \bottomrule]
                {test.csv}{%
                1=\cola, 2=\colb
                } { \cola & \colb }
\end{document}

Make it:

$ latex mwe.tex

The result contains the column headings Col a and Col b as you desired.

I personally wouldn't separate the column heading from the data, but that's a style issue regarding coherency with the data and the header describing it. Be that as it may, not germane to your question.

I think you're OK. Just reexamine your result again. I chopped it down to just two columns for brevity. The real defect appeared to be the use of:

tabular = *{12}{c}

I tried: tabular = *{2}{c} for just two and got an error of an illegal character in the array arg. Changed it to {cc}. Edit yours to suit.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .