Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 20 down vote accepted

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}
share|improve this answer
    
Thank you very much! Perfect... –  Stephan Nov 8 '11 at 10:39
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 Dec 8 at 17:48
1  
@hannes: I run into the same problems. This seems like a bug in \pgfplotstablesort. I've opened a ticket. –  Jake Dec 8 at 18:12

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.

share|improve this answer
    
That solves it partly, however, how to determine the cell to highlight automatically? –  Stephan Nov 7 '11 at 19:21
    
@Christian: Defining a new style like you suggested works fine. –  Jake Nov 8 '11 at 10:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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