1

I've been using LaTeX to write lab reports for a year now, which includes manually entering data into tables. However, now I am performing experiments that have way more datapoints that manually entering data is a huge time waster now.

I've read about the csv importer command that works like:

\documentclass{article}
\usepackage{csvsimple}
\begin{document}
\csvautotabular{data.csv}
\end{document}

However, the table that LaTeX produces does not fit the style that I particularly like. The table style format and style I have come to use looks like this:

\begin{center}
    \captionof{table}[Short Caption for LoT]{Title of Table}        % title of table
    \begin{tabular}{c c}
        \toprule
        Heading 1 & Heading 2   \\ %heading for table
        \midrule
        Data1 & Data2 \\
        Data3 & Data4 \\
        \bottomrule
        \label{table:tablename} \\
    \end{tabular}
\end{center}

Is there a way to import CSV files into this format? Thanks

  • You can also look at pgfplotstable (It is part of pgfplots) – Ignasi Feb 4 '15 at 16:36
  • Could you, please, give an example for the data.csv file? I guess that every data line should produce a table (?) – Thomas F. Sturm Feb 4 '15 at 16:47
  • data.csv would have its first row be a heading ie: (Measurement #, Voltage, Uncertainty) and then the following rows would be data corresponding to these column headings. So I just need one table created from the dataset. – Smitty Shultz Feb 4 '15 at 20:14
  • You can use \csvreader inside a tabular environment. Check the csvsimple manual for details. – Zoxume Feb 4 '15 at 21:20
6

csvtabular has \csvautobooktabular for this purpose.

\documentclass{article}
\usepackage{csvsimple,booktabs}
\usepackage{filecontents}
\begin{filecontents*}{mycsvdata.csv}
A, B
0.0, 1.0
0.0, -0.5
\end{filecontents*}
\begin{document}
\begin{table}[htb]
    \centering
    \caption[Short Caption for LoT]{Title of table}\label{table:tablename}
\csvautobooktabular{mycsvdata.csv}
\end{table}

\end{document}

enter image description here

You can use even the S column types from siunitx. (an example from the manual)

\documentclass{article}
\usepackage{csvsimple,booktabs,array,siunitx}
\usepackage{filecontents}
\begin{filecontents*}{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
\end{filecontents*}
\begin{document}
\begin{table}[htb]
    \centering
    \caption[Short Caption for LoT]{Title of table}\label{table:tablename}
        \csvloop{
            file=data_numbers.csv,
            head to column names,
            before reading=\centering\sisetup{table-number-alignment=center},
            tabular={lSS[table-format=2.2e1]},
            table head=\toprule\textbf{Month} & \textbf{Dogs} & \textbf{Cats}\\\midrule,
            command=\month & \dogs & \cats,
            table foot=\bottomrule}
\end{table}

\end{document}

enter image description here

With \csvreader another example:

\documentclass{article}
\usepackage{csvsimple,booktabs,siunitx}
\usepackage{filecontents}
\begin{filecontents*}{data_headless.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
\end{filecontents*}
\begin{document}
\begin{table}[htb]
    \centering
    \caption[Short Caption for LoT]{Title of table}\label{table:tablename}
        \csvreader[
            tabular={lS[table-format=2.2e1]},
            table head=\toprule\bfseries Month & \bfseries Cats\\\midrule,
            table foot=\bottomrule]
            {data_headless.csv}
            {1=\month,3=\cats}
            {\month & \cats}
\end{table}

\end{document}

enter image description here

2

A short example with pgfplotstable

\documentclass{article}   

\usepackage{booktabs}
\usepackage{pgfplotstable}


\pgfplotstableread{
A  B
0.0    1.0
0.0   -0.5
}\datatable

\begin{document}

\begin{table}
    \centering
    \caption[Short Caption for LoT]{Title of table}
    \pgfplotstabletypeset[%
        every head row/.style={
            before row=\toprule, after row=\midrule},
        every last row/.style={
            after row=\bottomrule},
        ]{\datatable}
    \label{table:tablename}
\end{table}

\end{document}

enter image description here

  • So I would need to copy and paste my data CSV (or some other delimited) file into LaTeX code using the \pgfplotstableread command? Can I instead use \pgfplotstableread{data.csv}? – Smitty Shultz Feb 4 '15 at 20:16
  • @SmittyShultz Yes, you can use a \macro (like in my example), a file name (what you want) and inline data. – Ignasi Feb 4 '15 at 20:24
1

Just:

\documentclass{article}
\usepackage{csvsimple}
\begin{document}
\csvautotabular{YOUR_CSV_FILE.csv}
\end{document}

here for more info

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.