我正在尝试从 .csv 文件生成表格。首先,我创建一个如下所示的 Excel 电子表格:
然后我将其导出为 .csv 文件,如下所示:
然后我使用以下代码来生成表格:
\usepackage{booktabs} % for \toprule, \midrule, \bottomrule.
\usepackage{siunitx} % for formatting units and values.
\usepackage{pgfplotstable} % for generating tables form .csv files.
\sisetup{ % setting up the siunitx package
round-mode=places, % round numbers
round-precision=2 % up to the second digit
}
\begin{document}
\begin{table}[h!]
\begin{center}
\caption{Auto-generated table from .csv file.}
\label{tab:students}
\pgfplotstabletypeset[
multicolumn names, % allows to have multicolumn names
col sep=comma, % the separator in our .csv file
display columns/0/.style={
column name=$Students$, % name of 1st column
column type={S},string type},
display columns/1/.style={
column name=$Heights [cm]$, % name of 2nd column
column type={S},string type},
display columns/2/.style={
column name=$Weights [kg]$, % name of 3rd column
column type={S},string type},
every head row/.style={
before row={\toprule},
after row={\si{Name} & \si{cm} & \si{kg}\\
\midrule}
},
every last row/.style={after row=\bottomrule},
]{HeightsAndWeights.csv}
\end{center}
\end{table}
\end{document}
我得到的结果如下:
我做错了什么?我应该纠正、添加或删除什么?
答案1
这是一个使用的工作示例真的csv 文件和一些修改
\documentclass{article}
\usepackage{tabularx}
\usepackage{booktabs} % for \toprule, \midrule, \bottomrule.
\usepackage{siunitx} % for formatting units and values.
\usepackage{pgfplotstable} % for generating tables form .csv files.
\sisetup{ % setting up the siunitx package
round-mode=places, % round numbers
round-precision=2 % up to the second digit
}
\begin{filecontents*}{file.csv}
Peter, 150, 45
Jason, 134, 39
Stefan, 139, 41
\end{filecontents*}
\begin{document}
\begin{table}[h!]
\begin{center}
\caption{Auto-generated table from .csv file.}
\label{tab:students}
\pgfplotstabletypeset[
multicolumn names, % allows to have multicolumn names
col sep=comma, % the separator in our .csv file
display columns/0/.style={column name=Students, column type={c},string type},
display columns/1/.style={column name=Heights, column type={S},string type},
display columns/2/.style={column name=Weights,column type={S},string type},
every head row/.style={before row={\toprule},after row={Name & {\si{\centi\meter}} & {\si{\kilo\gram}}\\\midrule}},
every last row/.style={after row=\bottomrule},
]{file.csv}
\end{center}
\end{table}
\end{document}