pgfplotstable:如何用跨越其列的单个标题替换多行标题

pgfplotstable:如何用跨越其列的单个标题替换多行标题

在这个 MWE 中,我使用multirow包使列标题跨越两行,但是,我想将col1([index]1) 和col2([index]2) 多层标题组合起来,例如,One Column不损害其他标题样式。

% arara: indent: { overwrite : yes}

\RequirePackage{filecontents}
\begin{filecontents*}{sample.csv}
radius,parameter,cubic,revised,quartic
h=1 ,13 ,34 ,5  ,9
    ,2  ,6  ,10 ,234
h=2 ,3  ,7  ,11 ,87
    ,4  ,8  ,12 ,16%
\end{filecontents*}

\documentclass{article}

\usepackage{pgfplotstable,booktabs,multirow,array,siunitx}


\begin{document}

\pgfplotstabletypeset[
    every col no 0/.style={column name=\multirow{2}{*}{radius},
        assign cell content/.code={% use \multirow for Z column:
            \ifnum\pgfplotstablerow=0
                \pgfkeyssetvalue{/pgfplots/table/@cell content}
                {\multirow{2}{*}{##1}}%
            \else
                \ifnum\pgfplotstablerow=2
                    \pgfkeyssetvalue{/pgfplots/table/@cell content}
                    {\multirow{2}{*}{##1}}%
                \else
                    \pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
                \fi
            \fi
        }
    },
    every col no 1/.style={column name=\multirow{2}{*}{col1},string type,column type=c},
    every col no 2/.style={column name=\multirow{2}{*}{col2}},
    every col no 3/.style={column name=Revised},
    every col no 4/.style={column name=\multirow{2}{*}{Quartic}},
    % use \booktabs as well (compare examples above):
    every head row/.style={
        before row=\toprule,
        after row/.add={%
            \arraybackslash
            & & & Super  & \\
            \midrule
            }{}
    },
    %
    every last row/.style={after row=\bottomrule},
    every row no 1/.style={after row=\midrule},
    col sep=comma%
]{sample.csv}

\end{document}

在此处输入图片描述

答案1

这里有一个解决方法multirow,首先清除column name所有标题列,然后像这样添加它们

 every head row/.style={
        before row={\toprule
      radius  &\multicolumn{2}{c}{one column}& \parbox[c]{2cm}{\centering Revised\\Super} & Quartic \\[-\normalbaselineskip]},
        after row=\midrule
                        }

代码

\RequirePackage{filecontents}
\begin{filecontents*}{sample.csv}
radius,parameter,cubic,revised,quartic
h=1 ,13 ,34 ,5  ,9
    ,2  ,6  ,10 ,234
h=2 ,3  ,7  ,11 ,87
    ,4  ,8  ,12 ,16%
\end{filecontents*}

\documentclass{article}

\usepackage{pgfplotstable,booktabs,multirow,array,siunitx}


\begin{document}

\pgfplotstabletypeset[
    every col no 0/.style={column name=,
        assign cell content/.code={% use \multirow for Z column:
            \ifnum\pgfplotstablerow=0
                \pgfkeyssetvalue{/pgfplots/table/@cell content}
                {\multirow{2}{*}{##1}}%
            \else
                \ifnum\pgfplotstablerow=2
                    \pgfkeyssetvalue{/pgfplots/table/@cell content}
                    {\multirow{2}{*}{##1}}%
                \else
                    \pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
                \fi
            \fi
        }
    },
    every col no 1/.style={column name=\hbox to 6mm{},string type,column type=c},
    every col no 2/.style={column name=},
    every col no 3/.style={column name=},
    every col no 4/.style={column name=},
    % use \booktabs as well (compare examples above):
    every head row/.style={
        before row={\toprule
      radius  &\multicolumn{2}{c}{one column}& {\parbox[c]{2cm}{\centering Revised\\Super}} & Quartic \\[-\normalbaselineskip]},
        after row=\midrule
             },
    %
    every last row/.style={after row=\bottomrule},
    every row no 1/.style={after row=\midrule},
    col sep=comma%
]{sample.csv}

\end{document}

更新

要使标题文本与其他列内容居中,您可以创建存储标题文本的框,然后将其宽度的一小部分添加到column name

\RequirePackage{filecontents}
\begin{filecontents*}{sample.csv}
radius,parameter,cubic,revised,quartic
h=1 ,13 ,34 ,5  ,9
    ,2  ,6  ,10 ,234
h=2 ,3  ,7  ,11 ,87
    ,4  ,8  ,12 ,16%
\end{filecontents*}

\documentclass{article}

\usepackage{pgfplotstable,booktabs,multirow,array,siunitx}


\newbox{\tbox}
\def\instext#1{\global\setbox\tbox=\hbox{#1}\copy\tbox}
\def\inswidth{\hspace*{0.4\wd\tbox}}

\begin{document}

\pgfplotstabletypeset[
    every col no 0/.style={column name=,
        assign cell content/.code={% use \multirow for Z column:
            \ifnum\pgfplotstablerow=0
                \pgfkeyssetvalue{/pgfplots/table/@cell content}
                {\multirow{2}{*}{##1}}%
            \else
                \ifnum\pgfplotstablerow=2
                    \pgfkeyssetvalue{/pgfplots/table/@cell content}
                    {\multirow{2}{*}{##1}}%
                \else
                    \pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
                \fi
            \fi
        }
    },
    % use \booktabs as well (compare examples above):
    every head row/.style={
        before row={ \toprule  
      radius  &  \multicolumn{2}{c}{\instext{column with very long head}}& {\parbox[c]{2cm}{\centering Revised\\Super}} & Quartic \\[-\normalbaselineskip]},
        after row=\midrule
             },
    every col no 1/.style={column name={\inswidth},string type,column type=c},
    every col no 2/.style={column name=},
    every col no 3/.style={column name=},
    every col no 4/.style={column name=},         
    %
    every last row/.style={after row=\bottomrule},
    every row no 1/.style={after row=\midrule},
    col sep=comma%
]{sample.csv}

\end{document}

输出

在此处输入图片描述

相关内容