[Tex/LaTex] how to import CSV table with header containing spaces

csvcsvsimpletables

The header column names in my CSV contain spaces. Is there a way to still import it using csvsimple or maybe some other package? That way I would be able to specify data in CSV, import it and render it automatically as a table via a package. Additionally, since the headers are long, I'd like the table to expand up to page width and wrap long headers.

\begin{filecontents*}{forum_posts_table_1.csv}
    posts per author,sentences per post,quoted sentences per post,EREs per post,mentions per ERE
    3.1,23.83454,2313.27,13453453.8,2464642.43
\end{filecontents*}

Best Answer

As I have written in my comment, all depends on how to use the data. csvsimple has no problem with spaces inside the header as far as you do not create automated macros with head to column names.

An example usage is the following:

\documentclass{article}
\usepackage{csvsimple,array,filecontents,booktabs}

\begin{filecontents*}{forum_posts_table_1.csv}
    posts per author,sentences per post,quoted sentences per post,EREs per post,mentions per ERE
    3.1,23.83454,2313.27,13453453.8,2464642.43
    4.1,23.83454,2313.27,13453453.8,2464642.43
    5.1,23.83454,2313.27,13453453.8,2464642.43
\end{filecontents*}

\begin{document}

\newcommand\myhead[1]{\parbox[t]{5em}{\centering\bfseries#1\par\kern1mm}}

\csvreader[no head,column count=5,tabular=rrrrr,
  table head=\toprule,
  late after first line=\\\midrule,
  table foot=\bottomrule
  ]%
{forum_posts_table_1.csv}{}{%
  \csviffirstrow{\myhead{\csvcoli} & \myhead{\csvcolii} & \myhead{\csvcoliii}
    & \myhead{\csvcoliv} & \myhead{\csvcolv}
    }{\csvlinetotablerow}
}

\end{document}

enter image description here

Related Question