尝试了之前的所有回复问题,但似乎都无法解决问题。我尝试了各种 csvreader,但似乎无法解决问题。
我正在尝试导入以下 csv 文件。下面是 CSV 示例。
Contract,Today,One Week Ago,One Year Ago,1 Week Change,1 Year Change
Petroleum (NYM, WTI Crude Cash, $ per barrel),,,,,
October 19,55.68,55.25,62.61,1%,-11%
March 20,53.89,53.55,61.04,1%,-12%
Natural Gas (NYM, $ per million BTU's),,,,,
October 19,2.177,2.154,2.732,1%,-20%
March 20,2.261,2.262,2.535,0%,-11%
Gasoline (NYMEX, RBOB, $ per gallon),,,,,
October 19,1.5639,1.5402,1.8366,2%,-15%
March 20,1.6884,1.6657,2.0045,1%,-16%
Heating Oil (NYM, $ per gallon),,,,,
October 19,1.864,1.851,,1%,
March 20,1.835,1.827,,0%,
答案1
您可能会因为数据文件而遇到问题。首先,您使用逗号作为列分隔符,但第一列有多个包含逗号的条目。pgfplotstable
例如,这会造成混淆。一种解决方法是使用不同的列分隔符,例如分号。另一种方法是将有问题的单元格包装在 中{}
。
另一个问题可能是美元符号和百分号。这些是 TeX 中的特殊字符,因此需要以某种方式处理。可能有一些软件包可以自动执行此操作,但在下面的示例中,我只是在文件中使用\$
而不是来转义$
美元符号。我本可以对百分号做同样的事情,但最终还是删除了它们并在表格设置中重新添加它们。
这很可能不是最好的方法,但我认为它确实有效。它可能有点宽,但根据文档的布局,这可能是也可能不是问题。
% the filecontents environment writes its content to the file
% specified, you don't need it for your own code
% just to make the example self contained, while showing the
% changes to the file
\begin{filecontents*}{datafile.csv}
Contract,Today,One Week Ago,One Year Ago,1 Week Change,1 Year Change
{Petroleum (NYM, WTI Crude Cash, \$ per barrel)},,,,,
October 19,55.68,55.25,62.61,1,-11
March 20,53.89,53.55,61.04,1,-12
{Natural Gas (NYM, \$ per million BTU's)},,,,,
October 19,2.177,2.154,2.732,1,-20
March 20,2.261,2.262,2.535,0,-11
{Gasoline (NYMEX, RBOB, \$ per gallon)},,,,,
October 19,1.5639,1.5402,1.8366,2,-15
March 20,1.6884,1.6657,2.0045,1,-16
{Heating Oil (NYM, \$ per gallon)},,,,,
October 19,1.864,1.851,,1,
March 20,1.835,1.827,,0,
\end{filecontents*}
\documentclass[border=5mm]{standalone}
\usepackage{pgfplotstable}
\usepackage{booktabs} % for improved table rules
\usepackage{etoolbox} % for ifstrequal
% read file to table
\pgfplotstableread[col sep=comma]{datafile.csv}\MyTable
\begin{document}
\pgfplotstabletypeset[
assign column name/.style={
/pgfplots/table/column name={\textbf{#1}} % bold header row
},
every head row/.style={
% add rules above and below header
before row=\toprule,
after row=\midrule,
},
% add rule after last row
every last row/.style={after row=\bottomrule},
%modify first column
columns/Contract/.style={
string type,
% left aligned
column type={l},
assign cell content/.code={
% kind of a hack to change the "sub headers"
\pgfmathtruncatemacro\TmpRow{mod(\pgfplotstablerow,3)}%
\ifnum\TmpRow=0% for every third row
% typeset the content in an \rlap, which makes a zero size box, with the content sticking out on the right
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\rlap{\textit{##1}}}%
\fi
}
},
% make a style for the last two columns
percents/.style={
postproc cell content/.append code={%
\pgfmathtruncatemacro\TmpRow{mod(\pgfplotstablerow,3)}%
\ifnum\TmpRow=0
\else
% if not empty, add a percent character
\ifstrequal{##1}{}{}{\pgfkeyssetvalue{/pgfplots/table/@cell content}{$##1\%$}}%
\fi
}
},
% use style for last two columns
columns/1 Week Change/.style={percents},
columns/1 Year Change/.style={percents},
% add some space before each subheading
every nth row={3[2]}{after row={\addlinespace}}
]\MyTable
\end{document}