1

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?

1 Answer 1

2

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

6
  • This looks perfect! I even think I understand what is happening there, thank you! I figure if I will grasp the details the best way is to look at the expl3 manual? If I want to use this in multiple tables, do I have to redefine the commands since they make use of global variables or is it ok to reuse, perhaps by resetting g_tmpa_tl somehow? (I will probably only get around to using it tomorrow. I'll give the checkmark out then :) )
    – lucidbrot
    Aug 15, 2021 at 12:53
  • 1
    The manual of expl3 is interface3.pdf. It is OK to reuse g_tmpa_tl since it is a scratch global variable.
    – L.J.R.
    Aug 15, 2021 at 12:56
  • So, this works exactly as promised when I use the example you're providing. When I add an alias aaa=\asdf, and use \asdf instead of \aaa it still works, so aliases seem to be supported. However when I use it with my alias drdbnet_vimeo90k_weighted_ex12b-v3_682917=\asdf, and change the example csv header from aaa to this long name, compilation fails with Undefined control sequence. <argument> \aaa although there is nowhere an explicit \aaa in use. Would you know what's going on? Thank you very much for the help!
    – lucidbrot
    Aug 16, 2021 at 11:25
  • Okay nevermind, somehow the problem is not my long name with underscores... even ab instead of aaa as header fails. I did not realize they were hardcoded in the command. My bad.
    – lucidbrot
    Aug 16, 2021 at 11:30
  • 1
    You can also try to use \csvcolii, \csvcoliii, \csvcoliv, \csvcolv instead of \aaa, \bbb, \ccc, \ddd.
    – L.J.R.
    Aug 16, 2021 at 11:36

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.