5

I have the following table in a avg_value.csv file:

,M23,F23_1,M36,F44,F63,M70
M23,1.00,0.81,0.88,0.83,0.41,0.82
F23_1,,1.00,0.52,0.56,0.25,0.94
M36,,,1.00,0.62,0.94,0.96
F44,,,,1.00,0.43,0.23
F63,,,,,1.00,0.22
M70,,,,,,1.00

and I want to import it in my LaTeX file. Currently, I am using this code from http://pgfplots.sourceforge.net/gallery.html :

\usepackage{pgfplots}
\usepackage{booktabs}
\usepackage{pgfplotstable}
\usepackage{array}
.
.
.
\pgfplotstabletypeset[
every head row/.style={
before row=\toprule,after row=\midrule},
every last row/.style={
after row=\bottomrule},
]
{pgfplotstable.avg_value.csv}

In fact, I do not import anything.

I receive as output message from kile:

! Package pgfplots Error: Could not read table file 'pgfplotstable.avg_value.csv'.


Changing the code as follows:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\pagestyle{empty}

\usepackage{pgfplotstable}
\usepackage{array}
\usepackage{colortbl}
\usepackage{booktabs}
\usepackage{amsmath}
\usepackage{pgfplotstable}

\begin{document}

\begin{table}
\pgfplotstableread[col sep=comma]{pgfplotstable.avg_value.csv}\mytable
\pgfplotstabletypeset[
every head row/.style={before row=\toprule,after row=\midrule},
every last row/.style={after row=\bottomrule},
display columns/0/.style={string type,column name={}}
]
{\mytable}
\caption[title.]{title.}
\label{tab:foo} 
\end{table}

\end{document}

did not solve the problem. Nothing really appears.

werty
  • 311

1 Answers1

10

You have to describe the structure of the csv file to pgfplotstable. And you have subscripts in your column/row names that you need to take care of. I've just removed them.

\documentclass{scrreprt}
\usepackage{pgfplotstable,booktabs}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% You don't need this part
% I did it to create your file 
\usepackage{filecontents} % <-- To create files on the fly

\begin{filecontents*}{avg_value.csv}
,M23,F231,M36,F44,F63,M70
M23,1.00,0.81,0.88,0.83,0.41,0.82
F231,,1.00,0.52,0.56,0.25,0.94
M36,,,1.00,0.62,0.94,0.96
F44,,,,1.00,0.43,0.23
F63,,,,,1.00,0.22
M70,,,,,,1.00
\end{filecontents*}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\pgfplotstabletypeset[
col sep = comma,
string replace*={_}{\textsubscript},
every head row/.style={before row=\toprule,after row=\midrule},
every last row/.style={after row=\bottomrule},
display columns/0/.style={string type,column name={}}
]
{avg_value.csv}

\end{document}

enter image description here

Please have a look at the manual for a quickstart if you need to get a feeling for common options.

percusse
  • 157,807
  • This way does not import any .csv file. – werty Aug 23 '15 at 08:23
  • @werty Read my second paragraph – percusse Aug 23 '15 at 08:23
  • 1
    Do you mean writing \pgfplotstableread[col sep=comma]{avg_value.csv}\mytable ? – werty Aug 23 '15 at 08:27
  • 1
    @werty Yes exactly. Notice that underscore is a math active character in TeX in some cases it might cause problems so if you can, avoid it in file names and variables. – percusse Aug 23 '15 at 08:29
  • i did it, but nothing appears. Look at the addendum at the end of my question. – werty Aug 23 '15 at 08:35
  • @werty See the edit wuth the explicit file – percusse Aug 23 '15 at 08:41
  • Everything works fine if I replace the underscore. So, the problem is almost solved. However, I need to specify the second column as "F23_1", so I have to maintain the underscore. In this case, the code does not work. Moreover, I was wondering if it is possible to draw a vertical line after the first column (i.e., the column with identifiers) – werty Aug 23 '15 at 10:24
  • @werty Is it a text subscript or not? In math mode, it is easy. – percusse Aug 23 '15 at 11:03
  • In the .csv file is F23_1. It is an underscore, not a subscript. – werty Aug 23 '15 at 11:13
  • @werty TeX thinks all underscores are subscripts and gives an error if you are not in math mode. That's why I'm trying to explain it – percusse Aug 23 '15 at 11:29
  • I see. I tried with F23\_1 in the .csv, but it did not solve the problem. – werty Aug 23 '15 at 12:51
  • @percusse I am sorry for asking this basic question, but could you tell me how to read a certain entry in .csv knowing its row and column numbers using pgfplotstable the same way done by go to in excel? – Diaa Dec 16 '16 at 21:54
  • 1
    @DiaaAbidou \pgfplotstablegetelem can be used – percusse Dec 16 '16 at 22:08