2

I would like to read in data from a CSV file into a plot. Unfortunately, the data contains commas itself:

Name, Description, Data
hello, "hello, world", 1

Note that the data is quoted using quotation marks, which is quite common when it comes to CSV files. I read in the file using the following:

\pgfplotstableread[col sep=comma]{data.csv}{\datatable}

Problem is: the pgfplotstable package does not seem to be able to handle quotes, specifically, I get an error message because there are apparently too many entries in the second row. I edited the file in the following way:

Name, Description, Data
hello, hello\, world, 1

This seems to work OK. I then tried to use the Description column as symbolic coordinates in a plot:

  \begin{axis}[yticklabels from table={\datatable}{Description}]
  # ....
  \end{axis}

Problem in this case: the commas disappear, the label only reads "hello world".

Is there a way to read the quoted entries (I am stuck using pgfplotstable), or to quote the file in such a way that I can read the column into the labels?

  • Providing an MWE would help. LaTeX understands groups, rather than quotes. So maybe hello, {hello, world}, 1 would work. Note that \, is a LaTeX macro for a small horizontal space, not a quoted comma, which would explain why your test did not produce a comma. – Steven B. Segletes Nov 2 '17 at 11:51
  • I tested my theory on another question's pgfplotstable. at tex.stackexchange.com/questions/375737/…, changing "Grapes", 22 to "{Grapes, red}", 22 and it works fine with the grouped comma. Without the grouping braces, it errs just as in your case. – Steven B. Segletes Nov 2 '17 at 11:59
  • If you can manually place braces then that's the easiest solution. – percusse Nov 2 '17 at 12:01
  • Another way is to define, let's say, \let\comma, and then in your data set use hello, hello\comma{} world, 1, but this seems more difficult than the group approach. Note though, that with the group approach, even hello, hello{,} world, 1 would be sufficient. – Steven B. Segletes Nov 2 '17 at 12:10
1

Per the comments: since I am able to modify the CSV directly, an acceptable way to escape commas is be the fifth option, i.e. surrounding the cell by curly braces:

\documentclass{minimal}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{filecontents*}{data.csv}
Name,Description,Data
hello,hello,1
second,second \, option,2
third,third \, option,3
fourth,"{fourth, option}",4
fifth,{fifth, option},5
\end{filecontents*}

\pgfplotstableread[col sep=comma]{data.csv}\datatable

\begin{document}

\begin{tikzpicture}
  \begin{axis}[ymin=-5,ymax=5,yticklabels from table={\datatable}{Description}]
  \end{axis}
\end{tikzpicture}

\end{document}

Just as a note: I used the \, not to produce extra spacing but to escape the comma. After all, a line of the form first, "first, option", 1 will cause latex to fail with the error message

! Package pgfplots Error: Table 'data.csv' appears to have too many columns

So the parsing options for CSV files using pgfplotstable seem to be rather limited...

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.