2

I am loading a rather large .csv file (>100 rows) that I need to print in longtable format and highlight the maximum value in each row. I found this thread Highlighting Extremal Values in Table which discussed the issue but the solution requires to add a line for each row.

I tried to use

\pgfplotstablegetrowsof{\data}
\pgfplotstabletypeset[
    \foreach \n in {1,...,\pgfplotsretval}{
         highlight row max ={\data}{\n},
    } 
]{\data}

but that won't compile. I have now spend several hours trying to work through the manuals of pgfplotstable, csvsimple and datatools looking for a solution (I'm not very well versed in LaTeX programming).

See also: Highlight min/max of value pairs on each row with pgfplotstable

  • You have a working example below but, we have no idea what the data were, what the output is that you wanted from it, and what the output is that you finally got. Please therefore post a snapshot of the data file (with maxima appropriately located) and an example of the output that you want to generate from it. – Jeffrey J Weimer Sep 22 '18 at 15:14
2

I was finally able to do it with datatools, all that was needed to do is read a 200p manual /s.

\documentclass{article}
\usepackage{booktabs}
\usepackage{array}
\usepackage{datatool}
\usepackage{longtable}

\begin{document}
    \DTLloadrawdb[]{DATA}{DATA.csv}
    \DTLforeach{DATA}{}{                                % iterate over rows
        \def\theMax{0}                                  % set max to zero
        \DTLforeachkeyinrow{\thisValue}{                % iterate over cols
            \ifthenelse{\dtlcol>3}{                     % ignore first 3 cols (unrelated)
                \DTLmax{\theMax}{\theMax}{\thisValue}   % compare max with current value
            }{}
        }       
        % Now \theMax should be maximal

        \DTLforeachkeyinrow{\thisValue}{                % iterate again over cols
            \ifthenelse{\dtlcol>3}{                     % ignore first 3 cols (unrelated)
                % If current Value is maximal, make it bold
                \ifthenelse{\DTLisieq{\thisValue}{\theMax}}{
                    \DTLreplaceentryforrow{\dtlkey}{\textbf{\theMax}}
                }{}
            }{}
        }           
    }
    \DTLdisplaylongdb[]{DATA}
\end{document}

Also note that this hightlights ALL maximal values, if multiple entries per row attain the maximum.

  • Agreeably, the manual for datatool can be ... cumbersome. In any case, I wonder whether your first foreach block is essentially what DTLmaxforcolumn provides. It thereby becomes as simple as defining this \DTLmaxforcolumn{DATA}{Column4}{\theMax4} (since you skipped setting keys for columns). – Jeffrey J Weimer Sep 22 '18 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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