1

I want a way to automatically (post-)process the minimum of every n rows in a column as bold.

So if my table had L rows, I want to group a column by each group of n (assumes L % n == 0) and bold the minimum value of each group.

In pseudocode:

for i in 0 to (L//n)-1:
    idx = idxmin(row[i*n], row[i*n+1], row[i*n+2], ..., row[i*n+n-1])
    bold(row[idx])

So far the closest answer I've found is this answer that bolds the maximum value of all rows in a column, it was easy to modify this to bold the minimum value but I couldn't get beyond that.

This is a minimal working example (L==9, n=3). I want the third, sixth, and eigth value in column b bolded, not just the global minium (sixth value):

\documentclass[12pt]{article}

\usepackage{pgfplotstable} \usepackage{booktabs}

\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} }

\pgfplotstableset{ 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\bf}{$} }, } } }\setstyles } }

\pgfplotstableread{ a b 1 3.3 2 2.2 3 1.1 4 -101.2 5 -100.3 6 -992.3 7 0.9 8 0.3 9 0.4 }\data

\begin{document}

\begin{table} \pgfplotstabletypeset[ columns={a,b}, highlight col min ={\data}{b}, every head row/.style={before row=\toprule,after row=\midrule}, every last row/.style={after row=\bottomrule}, every nth row={3}{before row=\midrule} ]{\data} \end{table} % I want 1.1, -992.3, and 0.3 bolded - not just -992.3

\end{document}

Current output

I found the syntax hard to understand and I want to ask the experienced people here. Reading the pgfplotstable docs didn't help me get closer to a solution.

1 Answers1

1

I ended up modifying the csv files and added $(col)rank columns for each column $col I wanted to style based on its relative order compared to adjacent row values. I used Julia (groupby, transform from DataFrames.jl and competerank from StatsBase.jl) but you could use whatever you want.

Now all that was left was to bold each row value where its rank column equals 1. This is for the minimum value highlighting, check equality to n (the length of the group) for the maximum value highlighting.

I ended up with the following column style addition (for column col and rank column colrank). I would prefer a function / code segment defined in the "global" \pgfplotstabletypeset where I could then call it as needed for each (col, colrank) pair, table, etc. I couldn't get that to work though. Barring a reused function, this code snippet works and is pretty minimal and readable:

\pgfplotstabletypeset[
    ...
    columns/col/.style={
        ...
        postproc cell content/.code={ % snippet start
            \pgfplotstablegetelem{\pgfplotstablerow}{colrank}\of\table
            \ifnum\pgfplotsretval=1\relax
                \pgfkeysalso{@cell content/.add={$\bf}{$}}
            \fi
        }, % snippet end
        ...
    },
    ...
]{\table}

These answers were helpful: