Tell me more ×
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.

I have a table generated by pgfplotstable, and would like to highlight the maximum value in each row for a particular number of columns.

Here is a simple, completely made up, example that should illustrate my problem:

                                 Car 1    Car 2    Car 3    Car 4
 Engine       Power(bhp)          300      200      350      250
              Torque (Nm)         200      250      120      400
 Dimensions   Wheel base (cm)     250      200      240      220
              Ground clearance    ...
                    ...

I'd like to make the maximum value in each row, excluding the first two columns, bold. This is a slight extension of Highlighting Extremal Values in Table, which has an excellent solution that I am trying to extend. (Unfortunately I can't add a comment to that thread because I have just joined stackexchange).

The code I need from the previous thread is

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

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

The table is read in with \pgfplotstableread{...}\data and then the above code called using

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

If this code is run for my example then it fails because the first two columns are not a float for the comparison. I understand the above code and have tried to extend it but am not an expert at tex.

I have managed to get a non general solution by limiting the columns that are generated by the transpose command:

pgfplotstabletranspose[columns={Car 1,Car 2,Car 3,Car 4}]{\transposed}{#1}

then the index of the column returned from \findmax just has to be increased to account for the offset.

I would like a more general approach that will work for any table. My current idea is to change the contents of any cell in the transposed table that is not a float to 0. I'm currently unable to find a way to do this.

share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.