自定义 pgfplotstable 中每行的第一列

自定义 pgfplotstable 中每行的第一列

我有一个混淆矩阵,其中第一列(或本例中为第二列)具有与第一行(或本例中为第二行)相同的标签。我的问题是这些标签是数学标签,我只能提供自定义列标签,而不能提供行标签。

请参阅 MWE,我想在其中修改列的行条目,Name使其具有与列标题相同的标签。我知道只有 5 行,标签可以固定,这样事情就简单多了$A_1$$A_5$但是,我该如何修改行条目呢?

平均能量损失

\documentclass{memoir}
\usepackage{pgfplotstable}
\usepackage{siunitx}
\usepackage{longtable}
\usepackage{multirow}

\newcommand*\rot{\rotatebox{90}}

\pgfplotsset{
    compat=1.13
}

\pgfplotstableset{%
        every head row/.style={before row=\toprule, after row=\midrule},
        every last row/.style={after row=\bottomrule}
}

\begin{document}
     \pgfplotstabletypeset[
            every head row/.style={
                before row={%
                    \toprule
                    &           &                                                                   \multicolumn{5}{c}{\textbf{Head 2}} \\
                    \cmidrule(lr){3-7}
                },
                after row/.add={}{%
                    \midrule
                    \multirow{5}{*}{\rot{\parbox{2cm}{\centering \textbf{Head 1}}}}
        }
            },
      col sep=semicolon,
            string replace={---}{\textemdash},
            use comma,
            header=true,
            multicolumn names={c},
            columns/Empty/.style={column name=,column type=l,string type},
            columns/Name/.style={column name=,column type=l,string type},
            columns/A1/.style={column name={$A_1$},string type,column type={S}},
            columns/A2/.style={column name={$A_2$},string type,column type={S}},
            columns/A3/.style={column name={$A_3$},string type,column type={S}},
            columns/A4/.style={column name={$A_4$},string type,column type={S}},
            columns/A5/.style={column name={$A_5$},string type,column type={S}}
    ]{
Empty;Name;A1;A2;A3;A4;A5
;A1;1;1;1;1;1
;A2;1;1;1;1;1
;A3;1;1;1;1;1
;A4;1;1;1;1;1
;A5;1;1;1;1;1
}%
\end{document}

编辑:

我找到了一个可能的解决方案,可以得到所需的输出。但是,我仍然好奇这是否是正确的方法,或者是否可以做得更优雅。

MWE解决方案:

\documentclass{memoir}
\usepackage{pgfplotstable}
\usepackage{siunitx}
\usepackage{longtable}
\usepackage{multirow}

\newcommand*\rot{\rotatebox{90}}

\pgfplotsset{
    compat=1.13
}

\pgfplotstableset{%
        every head row/.style={before row=\toprule, after row=\midrule},
        every last row/.style={after row=\bottomrule}
}

\begin{document}
     \pgfplotstabletypeset[
            every head row/.style={
                before row={%
                    \toprule
                    &           &                                                                   \multicolumn{5}{c}{\textbf{Head 2}} \\
                    \cmidrule(lr){3-7}
                },
                after row/.add={}{%
                    \midrule
                    \multirow{5}{*}{\rot{\parbox{2cm}{\centering \textbf{Head 1}}}}
        }
            },
      col sep=semicolon,
            string replace={---}{\textemdash},
            use comma,
            header=true,
            multicolumn names={c},
            columns/Empty/.style={column name=,column type=l,string type},
            columns/Name/.style={column name=,column type=l,string type,
                            assign cell content/.code={%
                                \ifnum\pgfplotstablerow=0
                                    \pgfkeyssetvalue{/pgfplots/table/@cell content}{$A_1$}%
                                \fi
                                \ifnum\pgfplotstablerow=1
                                    \pgfkeyssetvalue{/pgfplots/table/@cell content}{$A_2$}%
                                \fi
                                \ifnum\pgfplotstablerow=2
                                    \pgfkeyssetvalue{/pgfplots/table/@cell content}{$A_3$}%
                                \fi
                                \ifnum\pgfplotstablerow=3
                                    \pgfkeyssetvalue{/pgfplots/table/@cell content}{$A_4$}%
                                \fi
                                \ifnum\pgfplotstablerow=4
                                    \pgfkeyssetvalue{/pgfplots/table/@cell content}{$A_5$}%
                                \fi                             
                            },
                        },
            columns/A1/.style={column name={$A_1$},string type,column type={S}},
            columns/A2/.style={column name={$A_2$},string type,column type={S}},
            columns/A3/.style={column name={$A_3$},string type,column type={S}},
            columns/A4/.style={column name={$A_4$},string type,column type={S}},
            columns/A5/.style={column name={$A_5$},string type,column type={S}}
    ]{
Empty;Name;A1;A2;A3;A4;A5
;A1;1;1;1;1;1
;A2;1;1;1;1;1
;A3;1;1;1;1;1
;A4;1;1;1;1;1
;A5;1;1;1;1;1
}%
\end{document}

答案1

这是使用 的一种可能性collcell;假设标题的形式为<letter><digits>,包含一个字母。

\documentclass{memoir}
\usepackage{pgfplotstable}
\usepackage{siunitx}
\usepackage{longtable}
\usepackage{multirow}
\usepackage{collcell}

\pgfplotsset{compat=1.13}

\pgfplotstableset{%
  every head row/.style={before row=\toprule, after row=\midrule},
  every last row/.style={after row=\bottomrule}
}

\newcommand*\rot{\rotatebox{90}}
\newcommand{\rowhead}[1]{\dorowhead#1\relax}
\def\dorowhead#1#2\relax{$#1_{#2}$}

\begin{document}

\pgfplotstabletypeset[
  every head row/.style={
    before row={%
      \toprule
      & & \multicolumn{5}{c}{\textbf{Head 2}} \\
      \cmidrule(lr){3-7}
    },
    after row/.add={}{%
      \midrule
      \multirow{5}{*}{\rot{\parbox{2cm}{\centering \textbf{Head 1}}}}
    },
  },
  col sep=semicolon,
  string replace={---}{\textemdash},
  use comma,
  header=true,
  multicolumn names={c},
  columns/Empty/.style={column name=,column type=l,string type},
  columns/Name/.style={column name=,column type=>{\collectcell\rowhead}l<{\endcollectcell},string type},
  columns/A1/.style={column name={$A_1$},string type,column type={S}},
  columns/A2/.style={column name={$A_2$},string type,column type={S}},
  columns/A3/.style={column name={$A_3$},string type,column type={S}},
  columns/A4/.style={column name={$A_4$},string type,column type={S}},
  columns/A5/.style={column name={$A_5$},string type,column type={S}}
]{
  Empty;Name;A1;A2;A3;A4;A5
  ;A1;1;1;1;1;1
  ;A2;1;1;1;1;1
  ;A3;1;1;1;1;1
  ;A4;1;1;1;1;1
  ;A5;1;1;1;1;1
}

\end{document}

在此处输入图片描述

相关内容