Highlight maximum value in each row in csvsimple table

csvcsvsimple

I am currently using csvsimple (in pdfLaTeX) to generate a table from a csv file:


\documentclass{article}
\usepackage{xcolor}

\usepackage{csvsimple}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{float}


\begin{document}

\def\myasdf{1.5cm}
\begin{table}[htbp]
  \sisetup{round-mode=places, round-precision=4}
  \centering
  \csvreader[tabular=l|p{\myasdf}p{\myasdf}p{\myasdf}p{\myasdf}p{\myasdf}p{\myasdf},
            table head=\toprule Test Sequence & A & B & C & D & E & F\\ \midrule,
            head to column names,
            late after last line=\\\bottomrule]
            {csvs/data.csv}%
            {liu2021_vimeo90k_weighted_ex5a-v1_325000=\liuexFivea,
            liu2021_vimeo90k_weighted_ex1a-v1_825000=\liuexOnea,
            drdbnet_vimeo90k_weighted_ex16a-v1_375000=\drdbSixteena,
            drdbnet_vimeo90k_weighted_ex12b-v3_682917=\drdbTwelveb}%
            {\csvcoli & \num{\liuexFivea} & \num{\liuexOnea} &%
             \num{\drdbSixteena} & \num{\drdbTwelveb} & \num{\yadif} & \num{\zhu}
            }\\
  \caption{Example Caption}
  \label{tab:table1}
\end{table}


\end{document}

How can I adapt this code to highlight in each row the maximum value(s)? I don't care if it's by making the text bold, changing the cell background color, or any other way.


So far I found

I would prefer not having to learn how to use a new package though, since my table already looks how I want it to and I know how to adapt the table I have for similar uses. Is there a way to do this highlighting with csvsimple?

Best Answer

A simple csv file 610711.csv:

ooo,aaa,bbb,ccc,ddd,eee,fff
ooa,0.32,0.22,0.99,0.35,1.22,0.19
oob,1.22,1.72,3.27,2.05,3.21,3.27
ooc,4.10,0.32,2.49,1.10,0.52,2.19

A solution with expl3 code:

\documentclass{article}

\usepackage[table]{xcolor}
\usepackage{csvsimple}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{float}

\begin{document}

\ExplSyntaxOn
\NewDocumentCommand { \getmax } { }
  {
    \clist_gset:Nx \g_tmpa_clist {\aaa, \bbb, \ccc, \ddd, \eee, \fff}
    \clist_sort:Nn \g_tmpa_clist
      {
        \fp_compare:nNnTF {##1} < {##2}
          { \sort_return_swapped: }
          { \sort_return_same: }
      }
    \tl_gset:Nx \g_tmpa_tl { \clist_item:Nn \g_tmpa_clist {1} }
  }
\NewDocumentCommand { \mynum } { m }
  {
    \fp_compare:nNnTF { #1 } = { \g_tmpa_tl }
      { \cellcolor{blue!20} \num{#1} }
      { \num{#1} }
  }
\ExplSyntaxOff

\def\myasdf{1.5cm}
\begin{table}[htbp]
  \sisetup{round-mode=places, round-precision=4}
  \centering
  \csvreader[tabular=lp{\myasdf}p{\myasdf}p{\myasdf}p{\myasdf}p{\myasdf}p{\myasdf},
            table head=\toprule Test Sequence & A & B & C & D & E & F\\ \midrule,
            head to column names,
            before line=\getmax,
            late after last line=\\\bottomrule]
            {610711.csv}%
            {}%
            {\csvcoli & \mynum{\aaa} & \mynum{\bbb} &%
             \mynum{\ccc} & \mynum{\ddd} & \mynum{\eee} & \mynum{\fff}
            }
  \caption{Example Caption}
  \label{tab:table1}
\end{table}

\end{document}

enter image description here

Related Question