1

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*}
8
  • What do you mean "import"? How do you intend to use the data? I am thinking the readarray package can help. I could try to compose an answer, if I could understand how you want the data to be digested and recalled. Mar 13, 2017 at 12:26
  • p.s. If you choose readarray, make sure you employ the latest version (2.0), dated 2016/11/07 Mar 13, 2017 at 12:35
  • as Steven said, would do you want to do? pgfplotstable can also read this file without problems.
    – Timm
    Mar 13, 2017 at 12:37
  • For example, \documentclass{article} \usepackage{readarray}[2016-11-07] \usepackage{filecontents} \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*} \begin{document} \readarraysepchar{,} \readdef{forum_posts_table_1.csv}\mydata \readarray*\mydata\myarray[-,\ncols] Column 3 title is ``\myarray[1,3]''.\par Column 3 data is ``\myarray[2,3]'' \end{document} Mar 13, 2017 at 12:37
  • It all depends on how you intend to use the data. All the named packages may be helpful (including csvsimple). Spaces inside the header are no problem as far as you do not make automated macros with head to column names. Mar 13, 2017 at 12:47

2 Answers 2

3

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

7
  • Newbie question: what do expressions surrounded with ]% and {% signify?
    – m33lky
    Mar 16, 2017 at 13:11
  • The % deletes the linebreak which would insert a blank space otherwise. It may be not needed everywhere in the the above. Mar 16, 2017 at 13:13
  • If you say column count=5, why can't csvsimple just load the first 5 columns by default? I mean the columns of the header as well, the way you've written it.
    – m33lky
    Mar 19, 2017 at 13:47
  • @m33lky The option column count=5 is needed for \csvlinetotablerow. You could omit it and replace \csvlinetotablerow by \csvcoli & \csvcolii & \csvcoliii & \csvcoliv & \csvcolv. Mar 20, 2017 at 11:51
  • 1
    @m33lky It's all a matter of the tabular settings. tabular=p{5cm}rrrr for example would create a first column with 5cm width which wraps the content. Jul 3, 2017 at 14:10
4

The great datatool package seems to have no problem with spaces in headers.

\documentclass{article}
\usepackage{datatool}
\thispagestyle{empty}
\begin{document}
\DTLloadrawdb{posts}{forum_posts_table_1.csv}
\renewcommand{\dtlheaderformat}[1]{\parbox[t]{5em}{\bfseries
    #1}}
\DTLdisplaydb{posts}
\end{document}

enter image description here

2
  • To improve this answer, can you give an example of its use? Mar 13, 2017 at 13:11
  • Datatool has great documentation, but if you insist...
    – Boris
    Mar 13, 2017 at 15:35

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.