25

In a table generated by pgfplotstable, how can I highlight for each column or row the cell with the minimum value (e. g. displaying the corresponding entry in bold face)?

Example:

Example

0

2 Answers 2

29

You could add an auxiliary column containing the row numbers to your table, then use \pgfplotstablesort to sort the table according to a specified column, and then extract the first element of the row numbers column. This can then be fed to the every row <row> column <col> style that Christian used in his answer.

To get the extremal values for rows, you can transpose the table first using \pgfplotstabletranspose{<new macro>}{<existing table>} and then proceed as before.

Here's a solution that automates this. If you stored your data in a table called \data using \pgfplotstableread{...}\data, then you can call

\pgfplotstabletypeset[
    highlight col max ={\data}{a}, highlight col min ={\data}{a},
    highlight col max ={\data}{b}, highlight col min ={\data}{b},
    highlight col max ={\data}{c}, highlight col min ={\data}{c}
]{\data}

to get the following output:

and

\pgfplotstabletypeset[
    highlight row max ={\data}{1}, highlight row min ={\data}{1},
    highlight row max ={\data}{2}, highlight row min ={\data}{2},
    highlight row max ={\data}{3}, highlight row min ={\data}{3}
]{\data}

will yield

enter image description here

Here's the code:

\documentclass{article}
\usepackage{pgfplotstable}

\newcommand{\findmax}[3]{
    \pgfplotstablevertcat{\datatable}{#1}
    \pgfplotstablecreatecol[
    create col/expr={%
    \pgfplotstablerow
    }]{rownumber}\datatable
    \pgfplotstablesort[sort key={#2},sort cmp={float >}]{\sorted}{\datatable}%
    \pgfplotstablegetelem{0}{rownumber}\of{\sorted}%
    \pgfmathtruncatemacro#3{\pgfplotsretval}
    \pgfplotstableclear{\datatable}
}

\newcommand{\findmin}[3]{
    \pgfplotstablevertcat{\datatable}{#1}
    \pgfplotstablecreatecol[
      create col/expr={%
    \pgfplotstablerow
    }]{rownumber}\datatable
    \pgfplotstablesort[sort key={#2},sort cmp={float <}]{\sorted}{\datatable}%
    \pgfplotstablegetelem{0}{rownumber}\of{\sorted}%
    \pgfmathtruncatemacro#3{\pgfplotsretval}
    \pgfplotstableclear{\datatable}
}


\pgfplotstableread{
a b c
0.32 0.22 0.99
1.22 3.21 0.05
4.10 0.32 2.49
}\data


\begin{document}

\pgfplotstableset{
    highlight col max/.code 2 args={
        \findmax{#1}{#2}{\maxval}
        \edef\setstyles{\noexpand\pgfplotstableset{
                every row \maxval\noexpand\space column #2/.style={
                    postproc cell content/.append style={
                        /pgfplots/table/@cell content/.add={$\noexpand\bf}{$}
                    },
                }
            }
        }\setstyles
    },
    highlight col min/.code 2 args={
        \findmin{#1}{#2}{\minval}
        \edef\setstyles{\noexpand\pgfplotstableset{
                every row \minval\noexpand\space column #2/.style={
                    postproc cell content/.append style={
                        /pgfplots/table/@cell content/.add={\noexpand\color{red}$\noexpand\bf}{$}
                    },
                }
            }
        }\setstyles
    },
    highlight row max/.code 2 args={
        \pgfmathtruncatemacro\rowindex{#2-1}
        \pgfplotstabletranspose{\transposed}{#1}
        \findmax{\transposed}{\rowindex}{\maxval}
        \edef\setstyles{\noexpand\pgfplotstableset{
                every row \rowindex\space column \maxval\noexpand/.style={
                    postproc cell content/.append style={
                        /pgfplots/table/@cell content/.add={$\noexpand\bf}{$}
                    },
                }
            }
        }\setstyles
    },
    highlight row min/.code 2 args={
        \pgfmathtruncatemacro\rowindex{#2-1}
        \pgfplotstabletranspose{\transposed}{#1}
        \findmin{\transposed}{\rowindex}{\maxval}
        \edef\setstyles{\noexpand\pgfplotstableset{
                every row \rowindex\space column \maxval\noexpand/.style={
                    postproc cell content/.append style={
                        /pgfplots/table/@cell content/.add={\noexpand\color{red}$\noexpand\bf}{$}
                    },
                }
            }
        }\setstyles
    },
}

\makeatletter
\long\def\pgfplotstabletypeset@opt@collectarg[#1]#2{%

    \pgfplotstable@isloadedtable{#2}%
        {\pgfplotstabletypeset@opt@[#1]{#2}}%
        {\pgfplotstabletypesetfile@opt@[#1]{#2}}%
}
\makeatother


\pgfplotstabletypeset[
    highlight row max ={\data}{1}, highlight row min ={\data}{1},
    highlight row max ={\data}{2}, highlight row min ={\data}{2},
    highlight row max ={\data}{3}, highlight row min ={\data}{3}
]{\data}

\end{document}
3
  • 1
    This solution almost works for me, but it fails when there is a missing value/NaN somewhere in the column. Any suggestion on how to solve this? thanks!
    – hannes
    Commented Dec 8, 2014 at 17:48
  • 1
    @hannes: I run into the same problems. This seems like a bug in \pgfplotstablesort. I've opened a ticket.
    – Jake
    Commented Dec 8, 2014 at 18:12
  • How can I loop this over all rows?
    – Hyperplane
    Commented Aug 23, 2018 at 12:35
13

The most recent version 1.5 comes with a every row <rowindex> column <colindex> style which appears to do what you want. The following example is taken from the manual:

\pgfplotstabletypeset[
    every row 1 column 2/.style={/pgf/number format/sci},
    every row 0 column 0/.style={postproc cell content/.style={@cell content=\textbf{##1}}},
    col sep=&,row sep=\\]{
  colA & colB & colC \\
  11   & 12   & 13   \\
  21   & 22   & 23   \\
}

enter image description here

Two cells receive special treatment here; one by means of simple options, the other by means of a formatting instruction. Perhaps one can hide the lengthy instruction behind some smaller styles like

\pgfplotstableset{
    bf content/.style={postproc cell content/.style={@cell content=\textbf{##1}}
}

combined with every row 0 column 0/.style={bf content} (I did not verify it, though).

Note that it also accepts column names instead of indices.

2
  • That solves it partly, however, how to determine the cell to highlight automatically?
    – Stephan
    Commented Nov 7, 2011 at 19:21
  • @Christian: Defining a new style like you suggested works fine.
    – Jake
    Commented Nov 8, 2011 at 10:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .