我在文件中有一些数据.csv
,我想用pgfplotstable
和的组合来排版siunitx
。考虑以下(不是那么极简的)MWE,它会抛出错误
Undefined control sequence. \micro ->\ERROR l.49 ]{mytable.csv}\par
为什么会出错呢?
\documentclass[norsk,10pt,a4paper]{article}
\usepackage{babel}
\usepackage[utf8]{inputenc}
\usepackage{graphicx,parskip,url,xcolor,tabularx,array,adjustbox,booktabs}
\usepackage[showframe]{geometry}
\usepackage[hang,bf,small]{caption}
\usepackage{capt-of,pgfplotstable}
\usepackage{siunitx}
\sisetup{exponent-product = {\cdot},output-decimal-marker={,}, per-mode=symbol}
\begin{filecontents*}{mytable.csv}
Chem.; Avg. Conc.; Avg. Conc. Norm.; Conc. Unit; Mass sum; Mass unit
ammonium; 159083,33; 114450,21; \micro\gram\per\liter; 2839,463; \kilo\gram
\end{filecontents*}
\begin{document}
Some text here
\captionof{table}{Some caption text}
{\centering
\pgfplotstabletypeset[%
col sep=semicolon,
read comma as period=true,
header=has colnames,
every head row/.style={
before row={\toprule},
after row={\midrule},
every last row/.style={
after row=\bottomrule},
},
display columns/0/.style={%
string type, column type=l,%
},%
display column/1/.style={
},
display column/2/.style={
},
display column/3/.style={%
string type, column type={s},%
},%
display column/4/.style={
},
display column/5/.style={%
string type, column type={s},
},%
]{mytable.csv}\par
}
\end{document}
答案1
您的 MWE 有很多错误,我成功消除了其中的大部分,因此 MWE 现在至少可以编译......
编辑(1):
同时,我翻遍了pgfplotstable
手册和 SE,寻找答案中未解决的问题。这很有成效;我找到了我在纠正 MWE 中的错误时引入的错误……生成的新代码和所获表格的图像如下所示。
\documentclass[margin=5mm,preview]{standalone}
\usepackage{siunitx} % Formats the units and values
\sisetup{ % setup siunitx ...
round-mode = places, % rounds numbers
round-precision = 2, % to 3 places
per-mode = symbol, % kg/dm^3 instead kgm^{-3}
group-four-digits = true, % for 1 234,567
}
\usepackage{booktabs} % for table rules
\usepackage{pgfplotstable} % Generates table from .csv
\usepackage{filecontents} % <--- important: enable table
% refreshing at each compilation
\usepackage[hang,bf,small]{caption}
%---------------------------------------------------------------%
\begin{filecontents*}{mytable.csv}
Chem.; Avg. Conc.; Avg. Conc. Norm.; Conc. unit; Mass sum; Mass unit
Ammonium ; 159083.33; 114450.21; \si{\micro\gram\per\liter}; 2839.463; \si{\kilo\gram}
Ammonium* ; 1234.123; 4567.890; \si{\micro\gram\per\liter}; 2839.46; \si{\kilo\gram}
\end{filecontents*}
%---------------------------------------------------------------%
\begin{document}
Test of use \verb+siunitx+ units syntax in text \si{\micro\gram\per\liter} and \si{\kilo\gram},
\captionof{table}{Some caption text}
\pgfplotstabletypeset[
multicolumn names,
col sep=semicolon, % the separator in our .csv file
string type, % added in hopes of enabling alphabetic input.
header=has colnames,
every head row/.style={before row=\toprule,after row=\midrule},
every last row/.style={after row=\bottomrule},
display columns/0/.style={string type},
display columns/1/.style={column type={S[table-format=7.3]}},% use
display columns/2/.style={column type={S[table-format=7.3]}},% siunitx
display columns/3/.style={string type}, % units as string
display columns/4/.style={column type={S[table-format=5.3]}},% for formating
display columns/5/.style={string type}, % units as string
]{mytable.csv}
\end{document}
如您所见,在表中我介绍了S
列的类型并使用siunitx
引擎对列进行数字格式化。
警告:由于列标题过长,该表比文档类\textwidth
中的通常表更宽。article
编辑(2): 同时,Stefan Pinnow 在他的评论中描述了替代解决方案(由于未知原因,该解决方案之前不起作用,现在我记不清是什么问题了 :-( )。但是,下面给出了更正后的代码。
\documentclass[margin=5mm,preview]{standalone}
\usepackage{siunitx} % Formats the units and values
\sisetup{ % setup siunitx ...
round-mode = places, % rounds numbers
round-precision = 2, % to 3 places
per-mode = symbol, % kg/dm^3 instead kgm^{-3}
group-four-digits = true, % for 1 234,567
}
\usepackage{booktabs} % for table rules
\usepackage{pgfplotstable} % Generates table from .csv
\usepackage{filecontents} % <--- important: enable table
% refreshing at each compilation
\usepackage[hang,bf,small]{caption}
%---------------------------------------------------------------%
\begin{filecontents*}{mytable.csv}
Chem.; Avg. Conc.; Avg. Conc. Norm.; Conc. unit; Mass sum; Mass unit
Ammonium ; 159083.33; 114450.21; \micro\gram\per\liter; 2839.463; \kilo\gram
Ammonium* ; 1234.123; 4567.890; \micro\gram\per\liter; 2839.46; \kilo\gram
\end{filecontents*}
%---------------------------------------------------------------%
\begin{document}
Test of use \verb+siunitx+ units syntax in text \si{\micro\gram\per\liter} and \si{\kilo\gram},
\captionof{table}{Some caption text}
\pgfplotstabletypeset[
multicolumn names,
col sep=semicolon, % the separator in our .csv file
string type, % added in hopes of enabling alphabetic input.
header=has colnames,
every head row/.style={before row=\toprule,after row=\midrule},
every last row/.style={after row=\bottomrule},
display columns/0/.style={string type},
display columns/1/.style={column type={S[table-format=7.3]}},% use
display columns/2/.style={column type={S[table-format=7.3]}},% siunitx
display columns/3/.style={column type={s}}, % for units
display columns/4/.style={column type={S[table-format=5.3]}},% for formating
display columns/5/.style={column type={s}}, % for units
]{mytable.csv}
\end{document}
结果和以前一样!