2

I need to insert large tables in the appendix of my MSc thesis and I want to read the data from .csv files without fill the .tex file where I write. I produce the tables with the help of TableGenerator. Let us consider the following cutted .csv file:

    1,    459872    ,      2459600.5,    0.245955429393969E+07,    0.150902180397680E+09,    0.700936137135535E-01,    0.444423490130398E+02,    0.509894719093894E+02,    0.449518800380460E+02,    0.972856801473429E+00
    2,    1991 VG   ,      2459600.5,    0.245965301734296E+07,    0.154448562453878E+09,    0.524440272062934E-01,    0.981969077945011E+02,    0.305901193619353E+03,    0.310657729272394E+03,    0.939542405344313E+00
    3,    2000 SG344,      2459600.5,    0.245967142235446E+07,    0.146215475380197E+09,    0.668906906224866E-01,    0.107259306095888E+03,    0.280186369328737E+03,    0.287658863754547E+03,    0.102000471918105E+01
    4,    2003 YN107,      2459600.5,    0.245973765703815E+07,    0.147913409318582E+09,    0.138963447919681E-01,    0.352070216744272E+03,    0.221439015133514E+03,    0.222501180674322E+03,    0.100249189670507E+01
    5,    2006 JY26 ,      2459600.5,    0.245950771201134E+07,    0.151137260433216E+09,    0.831296262363333E-01,    0.317110762865456E+03,    0.995403784006208E+02,    0.900588991605521E+02,    0.970587901094867E+00
    6,    2006 QQ56 ,      2459600.5,    0.245948919576395E+07,    0.147356242165350E+09,    0.455957054639189E-01,    0.132721216278857E+03,    0.116944610798197E+03,    0.112215042556733E+03,    0.100818303543359E+01

My code to produce the table with help of above website is:

\documentclass{article}
\usepackage{siunitx}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{caption}
\usepackage{csvsimple}
\usepackage{graphicx}

\sisetup{
    round-mode=places,
    uncertainty-mode = separate,
}
\begin{table}[]
\centering
\caption{}
\label{tab:my-table}
\resizebox{1.1\textwidth}{!}{%
\begin{tabular}{
c
l
c
S[table-format=-1.3e1, round-precision=3, scientific-notation = true]
S[table-format=-1.3e1, round-precision=3, scientific-notation = true]
S[table-format=-1.3e1, round-precision=3, scientific-notation = true]
S[table-format=-1.3e1, round-precision=3, scientific-notation = true]
S[table-format=-1.3e1, round-precision=3, scientific-notation = true]
S[table-format=-1.3e1, round-precision=3, scientific-notation = true]
S[table-format=-1.3e1, round-precision=3, scientific-notation = true]
}
\toprule
\textbf{index} & \multicolumn{1}{c}{\textbf{pdes}} & \textbf{\begin{tabular}[c]{@{}c@{}}epoch \\ (JDTDB)\end{tabular}} & \multicolumn{1}{c}{\textbf{\begin{tabular}[c]{@{}c@{}}tp\\ (JDTDB)\end{tabular}}} & \multicolumn{1}{c}{\textbf{a\_km}} & \multicolumn{1}{c}{\textbf{e}} & \multicolumn{1}{c}{\textbf{w\_deg}} & \multicolumn{1}{c}{\textbf{ta\_deg}} & \multicolumn{1}{c}{\textbf{ma\_deg}} & \multicolumn{1}{c}{\textbf{n\_deg/d}} \\ \midrule
1 & 459872 & 2459600.5 & 0.245955429393969E+07 & 0.150902180397680E+09 & 0.700936137135535E-01 & 0.444423490130398E+02 & 0.509894719093894E+02 & 0.449518800380460E+02 & 0.972856801473429E+00 \\
2 & 1991 VG & 2459600.5 & 0.245965301734296E+07 & 0.154448562453878E+09 & 0.524440272062934E-01 & 0.981969077945011E+02 & 0.305901193619353E+03 & 0.310657729272394E+03 & 0.939542405344313E+00 \\
3 & 2000 SG344 & 2459600.5 & 0.245967142235446E+07 & 0.146215475380197E+09 & 0.668906906224866E-01 & 0.107259306095888E+03 & 0.280186369328737E+03 & 0.287658863754547E+03 & 0.102000471918105E+01 \\
4 & 2003 YN107 & 2459600.5 & 0.245973765703815E+07 & 0.147913409318582E+09 & 0.138963447919681E-01 & 0.352070216744272E+03 & 0.221439015133514E+03 & 0.222501180674322E+03 & 0.100249189670507E+01 \\
5 & 2006 JY26 & 2459600.5 & 0.245950771201134E+07 & 0.151137260433216E+09 & 0.831296262363333E-01 & 0.317110762865456E+03 & 0.995403784006208E+02 & 0.900588991605521E+02 & 0.970587901094867E+00 \\
6 & 2006 QQ56 & 2459600.5 & 0.245948919576395E+07 & 0.147356242165350E+09 & 0.455957054639189E-01 & 0.132721216278857E+03 & 0.116944610798197E+03 & 0.112215042556733E+03 & 0.100818303543359E+01 \\ \bottomrule
\end{tabular}
}
\end{table}
\end{document}

Output: output_table

I read some questions talking about the csvsimple package, can you help me read the .csv file to create the professional table with the booktab style without entering the data in the .tex file?

2 Answers 2

4

In case you table is going to be also large vertically, and possibly span multiple pages, you probably want to apply one of the long tables: longtable, xltabular etc.

Below is an example of using csvsimple and xltabular. I applied landscape as the table is quite wide with \newgeometry to slightly increase margins on pages occupied by the table. I also added solutions form makecell to simplify code when dealing with multiple lines

\documentclass{article}
\usepackage[pass]{geometry}
\usepackage{pdflscape}
\usepackage{csvsimple}
\usepackage{xltabular}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{makecell}

\sisetup{
    round-mode=figures,
    round-precision=3,
}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadalign{c}

\begin{document}
Some text here

\newgeometry{margin=3cm,a4paper}   % Starts new page
\begin{landscape}
    % \setlength\tabcolsep{3pt}
    \begin{xltabular}{0.93\linewidth}{
            @{} c X
            S[table-format=7.1, round-mode=none]
            *7{S[table-format=1.3e1]}
            @{}
        }
        \caption{The table\label{tab:label}}\\[-6pt]
        \toprule
        {\thead{idx.}}
        & {\thead[l]{pdes}}
        & {\thead{epoch\\(JDTDB)}}
        & {\thead{tp\\(JDTDB)}}
        & {\thead{a\_km}}
        & {\thead{e}}
        & {\thead{w\_deg}}
        & {\thead{ta\_deg}}
        & {\thead{ma\_def}}
        & {\thead{n\_deg/d}} \\
        \midrule \endhead
        \bottomrule \endfoot
        \csvreader[
            no head,
            late after line = \\,
        ]{sample.csv}{}%
        {\csvcoli & \csvcolii & \csvcoliii
        & \csvcoliv & \csvcolv & \csvcolvi & \csvcolvii & \csvcolviii
        & \csvcolix & \csvcolx
    }
    \end{xltabular}
\end{landscape}
\restoregeometry   % Adds pagebreak

\end{document}

enter image description here


EDIT. ~Code generating vertical table~

Edit 2.

\documentclass{article}
\usepackage{array}
\usepackage{csvsimple}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{caption}
\usepackage{makecell}

\sisetup{
  round-mode=figures,
  round-precision = 3,
  exponent-product = {\hspace{-1pt}\times\hspace{-2pt}},
}
\captionsetup[table]{position=top, skip=3pt}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadalign{c}

\begin{document}
Some text

\begin{table}[tbh]
  \small
  \renewcommand*{\arraystretch}{1.25}
  \setlength\tabcolsep{4pt}
  \caption{The table}
  \label{tab:label}
  \centering
  \begin{tabular}{
        @{} c l
        S[table-format=7.1, round-mode=none]
        *3{S[table-format=1.3e1]}
        @{\hspace{12pt}}
        S[table-format=1.3e1]
        @{}
    }
    \toprule
    {\thead{idx.}}
    & {\thead[l]{pdes}}
    & {\thead{epoch\\(JDTDB)}}
    & {\thead{a\_km}}
    & {\thead{e}}
    & {\thead{w\_deg}}
    & {\thead{ta\_deg}} \\
    \midrule
    \csvreader[
      no head,
      late after line = \\,
    ]{sample.csv}{}{%
      \csvcoli  & \csvcolii & \csvcoliii & \csvcoliv
      & \csvcolv & \csvcolvi & \csvcolvii
    }
    \bottomrule
  \end{tabular}
\end{table}

\end{document}
7
  • Since I plan to delete some columns, can you show how to produce the table vertically, i.e. without the use of landscape?
    – g_don
    Commented Nov 4, 2022 at 19:29
  • I have made a few changes and post a new code. The easiest way to render a vertical table is to substitute table with sidewaystable. In case you have twoside document, have a look at page 2 of rotating documentation in case you need to change orientation.
    – Celdor
    Commented Nov 4, 2022 at 23:17
  • Thank you very much @Celdor. Yeas, I use twoside document. Howvere I decided to not display the columns 4,9 and 10. Can you kindly update the code? I guess I have to remove \csvcol ...
    – g_don
    Commented Nov 4, 2022 at 23:31
  • Yes, you need to change number of elements generated from csv, as so a number of columns in two other places: headings and a column definition. Because you removed three columns, the table can now fit the page using regular table. I hope this is sorted now.
    – Celdor
    Commented Nov 5, 2022 at 0:34
  • 1
    The code is already updated.
    – Celdor
    Commented Nov 5, 2022 at 0:50
4

I would suggest not to scale the table but instead use a smaller font and decrease the column sep length. Other than that, assuming your data sits in a file named data.csv, the following should work:

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage{csvsimple}

\sisetup{
    round-mode=places,
    uncertainty-mode = separate,
}

\newcolumntype{Z}{S[table-format=-1.3e1, round-precision=3, scientific-notation=true]}

\begin{document}
\begin{table}
\centering
\caption{}
\label{tab:my-table}{%
\scriptsize%
\setlength{\tabcolsep}{2pt}%
\begin{tabular}{clc*{7}{Z}}
\toprule
\textbf{index} & \multicolumn{1}{c}{\textbf{pdes}} & \textbf{\begin{tabular}{@{}c@{}}epoch \\ (JDTDB)\end{tabular}} & \multicolumn{1}{c}{\textbf{\begin{tabular}{@{}c@{}}tp\\ (JDTDB)\end{tabular}}} & \multicolumn{1}{c}{\textbf{a\_km}} & \multicolumn{1}{c}{\textbf{e}} & \multicolumn{1}{c}{\textbf{w\_deg}} & \multicolumn{1}{c}{\textbf{ta\_deg}} & \multicolumn{1}{c}{\textbf{ma\_deg}} & \multicolumn{1}{c}{\textbf{n\_deg/d}} \\ \midrule
\csvreader[
    no head,
    late after line = \\
]{data.csv}{}{%
    \csvcoli & \csvcolii & \csvcoliii & \csvcoliv & \csvcolv & \csvcolvi & \csvcolvii & \csvcolviii & \csvcolix & \csvcolx
}%
\bottomrule
\end{tabular}
}
\end{table}
\end{document}

enter image description here

You must log in to answer this question.

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