Highlighting Extremal Values in Table
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
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}
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 \\
}
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.