7

I am currently struggling when I try to import a CSV file which contains math code using the following code:

\documentclass{article}
\usepackage{csvsimple}
\usepackage{amssymb}  % Math symbols
\usepackage{amsmath}  % Math formulas

% Make csv in question
\begin{filecontents*}{example.csv}
ex1,\rho = 4e9
0,19.85
\end{filecontents*}

\begin{document}
\csvautotabular{example.csv}
\end{document} 

The problem is that \rho is not interpreted. Moreover, it does not even compile. I also tried $\rho$ or within quotes. Unfortunately, csvsimple does not seem to work along with math mode contents of the CSV. Does anybody has a solution for this?

Thank you in advance! RMK

  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – karlkoeller Dec 13 '13 at 12:28
  • This seems to be caused by the presence of macros in the first line of the file, which, according to the package documentation, is usually used to indicate the column identifiers. If you swap the lines (and insert $ around \rho) the problem goes away. – Ian Thompson Dec 13 '13 at 12:38
  • Thank you for the fast anwer. You're right. Unfortunately, this is not what i want. I'd like to have $\rho$ in the identifier line, as this is the parameter I am varying. Is there any solution? – RMK Dec 13 '13 at 12:58
7

The \csvautotabular macro is just intended for raw viewing of text CSV files. For everything else, you should use \csvreader which allows full control of the content. If your file has no header, the assignment to the columns is simpy achieved by arabic numbers.

For following example sets the two columns in math mode and adds a line numbering for fun.

\documentclass{article}
\usepackage{csvsimple}
\usepackage{amssymb}  % Math symbols
\usepackage{amsmath}  % Math formulas

% Make csv in question
\begin{filecontents*}{example.csv}
ex1,\rho = 4e9
0,19.85
\end{filecontents*}

\begin{document}
\csvreader[no head,tabular=|l|c|c|,
  table head=\hline,late after line=\\\hline]{example.csv}
  {1=\one,2=\two}
  {\thecsvrow & $\one$ & $\two$}%
\end{document}

enter image description here

  • This is still cumbersome especially when I have various tables with various number of columns. However, I can work quite well with this snippet. Thank you very much! – RMK Dec 13 '13 at 16:56
  • You are welcome. Please, don't forget to press the accept button :-) – Thomas F. Sturm Dec 18 '13 at 8:05

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.