3

My question builds on code from this question: original question/answer

What I am trying to achieve:

  1. I want to use pgfplots to build a table, where the header row is automatically added as first column to the table (sort of a header column).
  2. Less important: I want to add rotated header rows/columns below the table and after the last row, that give an explanation of what is seen in these axes.

To clarify, I have prepared a little graphic:

Top: MWE/What I have. Bottom: What I want

My main problem:

My main problem is how to get access to the columns header names by index, as can be seen in the code.

MWE:

\documentclass[10pt,twocolumn,letterpaper]{article}

\usepackage[table]{xcolor}
\usepackage{pgfplotstable}

%for confusion table

\pgfplotstableset{
precision=1,
fixed,
fixed zerofill,
/color cells/min/.initial=0,
/color cells/max/.initial=1000,
/color cells/textcolor/.initial=,
%
% Usage: 'color cells={min=<value which is mapped to lowest color>, 
%   max = <value which is mapped to largest>}
color cells/.code={%
    \pgfqkeys{/color cells}{#1}% 
    \pgfkeysalso{%
        postproc cell content/.code={%
            %
            \begingroup
            %
            % acquire the value before any number printer changed
            % it:
            \pgfkeysgetvalue{/pgfplots/table/@preprocessed cell content}\value
            \ifx\value\empty
                \endgroup
            \else
            \pgfmathfloatparsenumber{\value}%
            \pgfmathfloattofixed{\pgfmathresult}%
            \let\value=\pgfmathresult
            %
            % map that value:
            \pgfplotscolormapaccess
                [\pgfkeysvalueof{/color cells/min}:\pgfkeysvalueof{/color cells/max}]
                {\value}
                {\pgfkeysvalueof{/pgfplots/colormap name}}%
            % now, \pgfmathresult contains {<R>,<G>,<B>}
            % 
            % acquire the value AFTER any preprocessor or
            % typesetter (like number printer) worked on it:
            \pgfkeysgetvalue{/pgfplots/table/@cell content}\typesetvalue
            \pgfkeysgetvalue{/color cells/textcolor}\textcolorvalue
            %
            % tex-expansion control
            % see https://tex.stackexchange.com/questions/12668/where-do-i-start-latex-programming/27589#27589
            \toks0=\expandafter{\typesetvalue}%
            %%% my try to replicate the header row starts here
            \xdef\III{0}
             \ifnum\III=\pgfplotstablecol\relax
                 \def\addedContent{ \pgfplotstablecolname  &}%
             \else
                 \def\addedContent{}%
             \fi
            %%% my try ends here: problem, I dont have access to the column header names
            \xdef\temp{%
                \noexpand\pgfkeysalso{%
                    @cell content={%
                        \addedContent
                        \noexpand\cellcolor[rgb]{\pgfmathresult}%
                        \noexpand\definecolor{mapped color}{rgb}{\pgfmathresult}%
                        \ifx\textcolorvalue\empty
                        \else
                            \noexpand\color{\textcolorvalue}%
                        \fi
                        \the\toks0 %
                    }%
                }%
            }%
            \endgroup
            \temp
            \fi
        }%
    }%
},
  every head row/.style={
 before row=&,
  },
  every first column/.style={
      column type/.add={c}{},
  },
}



\begin{document}
\begin{table}
\caption{What I have}
\centering
\setlength{\tabcolsep}{1pt}
\pgfplotstabletypeset[
    /pgfplots/colormap={CM}{color=(white) rgb255=(255,170,0)},
    color cells={min=0,max=100,textcolor=black},
    col sep=space,row sep=\\
]{A E F I \\
98.07692 9.25926 2.33516 0.00000\\
0.00000 74.07407 3.02198 0.00000\\
0.00000 0.00000 58.79121 0.00000\\
0.00000 3.70370 1.64835 100.00000\\
}
\end{table}
\end{document}

1 Answers1

1

You can replace

    %%% my try to replicate the header row starts here
     \xdef\III{0}
     \ifnum\III=\pgfplotstablecol\relax
         \def\addedContent{ \pgfplotstablecolname  &}%
     \else
         \def\addedContent{}%
     \fi
    %%% my try ends here: problem, I dont have access to the column header names

by

 \ifnum0=\pgfplotstablecol\relax
     \pgfplotstablegetcolumnnamebyindex\pgfplotstablerow\of\pgfplotstabletypesetfile@opt@@\to\tempcolname
     \def\addedContent{ \tempcolname  &}%
 \else
     \def\addedContent{}%
 \fi

Here, \pgfplotstablegetcolumnnamebyindex is defined as follows:

% Defines #3 to be the column name of the column with index '#1'.
%
% #1: a column index (starting with 0)
% #2: a loaded table structure
% #3: an output macro name.
\def\pgfplotstablegetcolumnnamebyindex#1\of#2\to#3{%
  \pgfplotslistselect#1\of#2\to#3\relax
}%

And \pgfplotstabletypesetfile@opt@@ is a temporary name of the inline table. If you happen to want to typeset a loaded table, replace this token by the table you are typesetting.

The rest of the question is sort of \tikzmark exercise. So I will leave to you.

Symbol 1
  • 36,855