我有一张表格,其中的列很窄,除了列标题之外。我正在使用 pgfplots 排版表格,我需要一种方法来指定应合并某些标题列。
平均能量损失
\documentclass[twocolumn]{article}
\usepackage{pgfplotstable}
\usepackage{booktabs}
% set column separator for our csv loading package
\pgfplotstableset{col sep=comma}
\begin{document}
\begin{table*}
\centering
\scriptsize
\caption{caption}
\label{tab:hierarchical_vs_soa}
\pgfplotstabletypeset[
begin table=\begin{tabular},
header=true,
every head row/.style={before row=\toprule, after row=\midrule},
every last row/.style={after row=\bottomrule},
every first row/.style={after row=\midrule},
string type,
columns/1/.style={column name=},
columns/3/.style={column name=},
columns/4/.style={column name=},
columns/6/.style={column name=},
columns/7/.style={column name=},
columns/9/.style={column name=},
end table=\end{tabular}
]{data.csv}
\end{table*}
\end{document}
使用示例 CSV 文件
header,,WIDETEXT,,,WIDETEXT MORE,,,WIDESTTEXTHERE,
,A,B,C,A,B,C,A,B,C
A,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
B,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
C,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
我怎样才能简单地实现这一目标?
答案1
我不知道如何自动执行此操作,但可以在选项\multicolumn
中声明一个标题。before row
如您所见,第一行(宽文本标题)已被注释,第二行用作标题。
\documentclass{article}
\usepackage{booktabs}
\usepackage{pgfplotstable}
\begin{document}
\begin{table}
\centering
\pgfplotstabletypeset[
every head row/.style={
before row={%
\toprule
& \multicolumn{3}{c}{Widetext} & \multicolumn{3}{c}{Widetext more} & \multicolumn{3}{c}{Widetext here}\\
},
after row=\midrule,
},
every last row/.style={
after row=\bottomrule},
columns/0/.style ={column name={}},
col sep=comma, row sep=\\,
string type,
]{
%header,,WIDETEXT,,,WIDETEXT MORE,,,WIDESTTEXTHERE,\\
,A,B,C,D,E,F,G,H,I\\
A,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1\\
B,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1\\
C,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1\\
}
\caption{A very nice table}
\end{table}
\end{document}
答案2
这个解决方案存在一个问题(由@Ignasi 提供):
\documentclass[twocolumn]{article}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\begin{document}
\begin{table}
\centering
\pgfplotstabletypeset[
every head row/.style={
before row={%
\toprule
& \multicolumn{3}{c}{Widetext} & \multicolumn{3}{c}{Widetext more} & \multicolumn{3}{c}{Widetext here}\\
},
after row=\midrule,
},
every last row/.style={
after row=\bottomrule},
columns/0/.style ={column name={}},
col sep=comma, row sep=\\,
string type,
]{
%header,,WIDETEXT,,,WIDETEXT MORE,,,WIDESTTEXTHERE,\\
,A,B,C,A,B,C,A,B,C\\
A,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1\\
B,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1\\
C,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1\\
}
\caption{A very nice table}
\end{table}
\end{document}
“header”行被附加了“-index*”以保证唯一性。我是这样解决的:
\documentclass[twocolumn]{article}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\begin{document}
\begin{table}
\centering
\caption{A very nice table}
\pgfplotstabletypeset[
header=false,
every head row/.style={after row={\toprule header & \multicolumn{3}{c}{Widetext} & \multicolumn{3}{c}{Widetext more} & \multicolumn{3}{c}{Widetext here}\\ \midrule}},
every first row/.style={after row=\midrule},
every last row/.style={after row=\bottomrule},
string type,
columns/0/.style={column name=},
columns/1/.style={column name=},
columns/2/.style={column name=},
columns/3/.style={column name=},
columns/4/.style={column name=},
columns/5/.style={column name=},
columns/6/.style={column name=},
columns/7/.style={column name=},
columns/8/.style={column name=},
columns/9/.style={column name=},
col sep=comma, row sep=\\,
string type,
]{
%header,,WIDETEXT,,,WIDETEXT MORE,,,WIDESTTEXTHERE,\\
,A,B,C,A,B,C,A,B,C\\
A,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1\\
B,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1\\
C,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1\\
}
\end{table}
\end{document}
但我不喜欢这个解决方案,因为由于不可见的空白标题以及必须指定每个列名,表格顶部会留下额外的空间。