我正在尝试使用 pgfplotstalble 格式化一系列包含文本和数字的表格,用于针对欧洲受众的技术文档,因此我需要在输出中使用逗号作为小数分隔符。尽管尝试了各种方法,但还是无法在表格的第一行获得相同的格式。(小数分隔符不是所需的逗号)也许这是一个线索:在注释行“\renewcommand{\familydefault}{\sfdefault}%selecting default font (clone of helvetica)”时,第一行仍为无衬线字体,表明第一行具有“特殊”行为。任何建议(以及任何改善表格整体外观的技巧)都值得赞赏。
\documentclass[a5paper]{article}
\usepackage[T1]{fontenc}
\usepackage{tgheros,textcomp}
\usepackage[helvet]{sfmath}
\usepackage{colortbl,hhline}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\renewcommand{\familydefault}{\sfdefault}%selecting default font (clone of helvetica)
\begin{document}
\begin{filecontents}{data.csv}
AWG 4, 6.1, 9, 3.9, 4.3
AWG 1/0, 6.2, 9, 5.2, 5.7
AWG 2/0, 6, 9, 6.0, 6.6
AWG 3/0, 5.1 ,7.5, 7.0 ,7.7
AWG 4/0, 4, 6 ,7.6, 8.4
\end{filecontents}
\pgfplotstableread[col sep=comma]{data.csv}{\datatable}
\begin{table}[H]
\centering
\pgfplotstabletypeset[
use comma,
every col no 0/.style={string type, column type={l}},
every col no 1/.style={fixed, column type={l}},
every col no 2/.style={fixed, column type={l|}},
every col no 3/.style={fixed, column type={l}},
every col no 4/.style={fixed, column type={l}},
every head row/.style={
before row={\toprule%
{Wire size}
&\multicolumn{4}{c}{Maximum voltage drop (\si{mV})}\\
&\multicolumn{2}{c|}{US military spec.}&\multicolumn{2}{c}{European spec.}\\
&\multicolumn{1}{c}{initial}
&\multicolumn{1}{c|}{final}
&\multicolumn{1}{c}{initial}
&\multicolumn{1}{c}{final}\\\midrule}
},
every even row/.style={%
before row={\rowcolor[gray]{0.9}}},
every last row/.style={after row=\bottomrule},
]{\datatable}
\end{table}
\end{document}
答案1
\pgfplotstableread
默认情况下,如果至少有一个非数字条目(例如“AWG 4”),则命令会将数据文件的第一行视为标题。添加header=false
到其选项中,得到以下行:
\pgfplotstableread[col sep=comma,header=false]{data.csv}{\datatable}
但是,这会创建一个新的默认标题来对列进行编号,这必须在输出中被抑制。这可以在样式中every head row
使用选项来完成output empty row
,结果如下:
every head row/.style={output empty row,
请参阅pgfplotstable 手册. 如下文摘录自 MWE 文章:
\pgfplotstableread[col sep=comma,header=false]{data.csv}{\datatable}
\pgfplotstabletypeset[
use comma,
every col no 0/.style={string type, column type={l}},
every col no 1/.style={fixed, column type={l}},
every col no 2/.style={fixed, column type={l|}},
every col no 3/.style={fixed, column type={l}},
every col no 4/.style={fixed, column type={l}},
every head row/.style={output empty row,
before row={\toprule%
{Wire size}
&\multicolumn{4}{c}{Maximum voltage drop (\si{mV})}\\
&\multicolumn{2}{c|}{US military spec.}&\multicolumn{2}{c}{European spec.}\\
&\multicolumn{1}{c}{initial}
&\multicolumn{1}{c|}{final}
&\multicolumn{1}{c}{initial}
&\multicolumn{1}{c}{final}\\\midrule}
},
every even row/.style={%
before row={\rowcolor[gray]{0.9}}},
every last row/.style={after row=\bottomrule},
]{\datatable}