How to add font commands to a pgfplotstable header cell?

1,178

Apparently I still don't remember the relevant code in How to rotate head row cell entries of pgfplotstable :)

I believe this can be a feature request.

\documentclass[border=5mm]{standalone}
\usepackage{colortbl}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.10}
\definecolor{cellshade}{rgb}{0.42, 0.55, 0.84}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\begin{document}
\pgfplotstabletypeset[
    col sep=comma,
    column type={C{5em}},
    every first column/.style={reset styles,string type,
                        column type={>{\color{white}\cellcolor{cellshade}\arraybackslash}C{5em}}
                        },
    every head row/.append style={
        before row={\rowcolor{cellshade}},
        typeset cell/.code={
        \ifnum\pgfplotstablecol=\pgfplotstablecols
        \pgfkeyssetvalue{/pgfplots/table/@cell content}{\textcolor{white}{##1}\\}%
        \else
        \pgfkeyssetvalue{/pgfplots/table/@cell content}{\textcolor{white}{##1}&}%
        \fi
        }
    },
    /pgf/number format/fixed,
    /pgf/number format/skip 0.
]{
Component,1,2,3
1,1.000,.186,.074
2,.186,1.000,.003
3,.074,.003,1.000
 }
\end{document}

enter image description here

Share:
1,178

Related videos on Youtube

Christian
Author by

Christian

Updated on April 02, 2020

Comments

  • Christian
    Christian over 3 years

    I'm trying to change the cell color and text color of the header of a pgfplotstable. Changing the cell color is not that much of a problem because I can just prefix the row with \rowcolor:

    \documentclass{standalone}
    \usepackage{colortbl}
    \usepackage{pgfplotstable}
    \definecolor{cellshade}{rgb}{0.42, 0.55, 0.84}
    \newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
    \begin{document}
    \pgfplotstabletypeset[
        col sep=comma,
        column type={C{5em}},
    every first column/.style={reset styles,string type,column type={>{\color{white}\cellcolor{cellshade}\arraybackslash}C{5em}}},
    %    /pgfplots/colormap={orangewhiteorange}{color=(myorange) color=(white) color=(myorange)},
    %    color cells={min=-1,max=1,textcolor=black},
        every head row/.style={before row={\rowcolor{cellshade}}},
        /pgf/number format/fixed,
        /pgf/number format/skip 0.
    ]{
    Component,1,2,3
    1,1.000,.186,.074
    2,.186,1.000,.003
    3,.074,.003,1.000
    }
    \end{document}
    

    table with black header text

    But clearly what the table should look like is this:

    table with white header text

    I'm stuck finding a good way to change the header text color though. I could change the name of each individual column but that's pretty inelegant. I know that I could in principle adapt this answer but it's not only also very inelegant IMHO (it changes the font style of the whole table first and then reverses this in postprocessing), it's also a solution that doesn't scale in general and in my case in particular it's not really applicable because a detail that I've mostly left out of the above MWE is that I postprocess the table cells anyway. I use this code to color in the table body, so this is what the table actually looks like (or should look like at least):

    table with right header and colored cells

    Sure, I could adapt this code but I'm using it in other tables, too, and going this route would lead to disaster sooner or later. It's also not The Right Thing To Do™.

    So, long story short, how do I add font commands (or, I imagine, any other commands) to header cells in particular, without affecting the rest of the table.

  • Christian
    Christian over 9 years
    Works great, even with the added code to color in the table cells! But yeah, it would be nice if there was an easier way to do this. I can imagine that setting a certain font style specifically for the header is quite a common use case in one way or another.