1

I have a simple two-lines csv file, which I've imported to latex with csvsimple like this:

\begin{filecontents*}{data.csv}
   1,2,3,4,5
   101,102,104,105,106
   -1,-2,-3,-4,-5
\end{filecontents*}
...
\begin{table}
   \begin{tabular}{*{5}{c}}
      \csvreader{data.csv}{}{\\ \csvlinetotablerow}
   \end{tabular}
\end{table}

But I want to add a header on the left side of the table and I've been seeing no way to do it.

In other words I want something like this:

Horizontal table

How does that can be accomplish?

  • You cannot add it to your csv file? – Benjamin Feb 23 '16 at 18:27
  • Is that acceptable practice? Latex compiler marks any math insertion in csv file as parse error. – Rasmussen Feb 23 '16 at 18:37
  • 1
    I just tried it with $I_c$ and $U_\mathrm{same}$ and had no problem. – Benjamin Feb 23 '16 at 18:40
1

For a two-line csv file, you can differentiate between the lines using \csviffirstrow. Simply put the two headers into the settings for the first and the following line(s):

\documentclass[12pt]{article}

\usepackage{csvsimple}

\begin{filecontents*}{data.csv}
   1,2,3,4,5
   101,102,104,105,106
   -1,-2,-3,-4,-5
\end{filecontents*}

\begin{document}

\begin{table}
  \csvloop{
    file=data.csv,
    tabular=|*{6}{c|},
    table head=\hline,
    command=\csviffirstrow{$I_c$}{$U_\mathrm{same}$} & \csvlinetotablerow,
    late after line=\\\hline,
  }
\end{table}

\end{document}

enter image description here

  • This is exactly what I've been looking for, thanks a lot! – Rasmussen Feb 24 '16 at 13:51
  • Suppose all data on csv has the format 0.000000. Is it possible to format the table entry with only 2 decimal places? I mean, like 0.00, automatically. – Sigur Feb 13 '18 at 18:22
  • 1
    @Sigur Yes, this is possible. For example, formatting with help of the siunitx package can be done. You find an application example in Section 5.4 (page 36) of the csvsimple documentation. – Thomas F. Sturm Feb 14 '18 at 11:38
  • @ThomasF.Sturm, thanks, but on that example, it only aligns the decimal separator. In my case I want to print only 2 decimal, that is, I want to truncate numbers. I'll post another question. – Sigur Feb 14 '18 at 13:13

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.