1

I have a confusion matrix where the first column (or in this case the second column) has the same labels as the first row (or in this case the second row). The problem I have is that these labels are math and I only managed to give custom column labels, but not row labels.

Please see the MWE where I would like to modify the row entries of the column Name to have the same labels as the column headings. What could make things easier is that I know that there will be only 5 rows and the labels $A_1$ to $A_5$ could be fixed. However, how can I modify the row entries?

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},
            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}

Edit:

I found a possible solution which results in the desired output. However, I'm still curious if that is the right way or if it can be done more elegant.

MWE Solution:

\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

Here's a possibility using collcell; the assumption is that the header is of the form <letter><digits>, with a single letter.

\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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.