从 csv 文件导入表格

从 csv 文件导入表格

我在 avg_value.csv 文件中有下表:

,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

我想将其导入我的 LaTeX 文件中。目前,我使用的代码来自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}

事实上,我没有导入任何东西。

我收到来自 kile 的输出消息:

!包 pgfplots 错误:无法读取表格文件“pgfplotstable.avg_value.csv”。


修改代码如下:

\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}

没有解决问题。什么也没有出现。

答案1

您必须向 描述 csv 文件的结构pgfplotstable。并且您需要注意列/行名称中的下标。我刚刚删除了它们。

\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}

在此处输入图片描述

如果您需要了解常见选项,请查看手册以了解快速入门。

相关内容