3

I have an horribly complex LaTeX table which I try to make a bit more readable.

I would like to use three different row colours, starting at the batch size column.

It is very difficult to do so, because every row consists of two rows (please ignore those zero values, they will soon change to meaningful values) and in the beginning, they are merged in packs of three.

The code is the following:

\begin{tabular}{|c|c|l|l|l|l|}
\hline
\multirow{2}{*}{Modell} & \multicolumn{1}{l|}{\multirow{2}{*}{Batch size}} & \multicolumn{4}{c|}{Learning rate} \\ \cline{3-6}
                        & \multicolumn{1}{l|}{}                            & 0.05    & 0.10   & 0.15   & 0.20   \\ \hline
\hline
\cellcolor{white}\multirow{6}{*}{Multilingual Flair}    & \multirow{2}{*}{16}        & 0.7319    & 0.7705    & 0.7901    & \textit{0.8316}   \\             
                        &                            & ±0.0      & ±0.0      & ±0.0      & ±0.0      \\ \cline{2-6} 
                        & \multirow{2}{*}{32}        & 0.631     & 0.681     & 0.7658    & 0.7747    \\             
                        &                            & ±0.0      & ±0.0      & ±0.0      & ±0.0      \\ \cline{2-6} 
                        & \multirow{2}{*}{64}        & 0.5919    & 0.6628    & 0.6676    & 0.7105    \\             
                        &                            & ±0.0      & ±0.0      & ±0.0      & ±0.0      \\ \hline      
 
\multirow{6}{*}{BERT}   & \multirow{2}{*}{16}        & 0.8113    & 0.8215    & 0.8279    & 0.8307    \\             
                        &                            & ±0.0      & ±0.0      & ±0.0      & ±0.0      \\ \cline{2-6} 
                        & \multirow{2}{*}{32}        & 0.7918    & 0.8236    & \textbf{0.8338}   & 0.8286    \\             
                        &                            & ±0.0      & ±0.0      & ±0.0      & ±0.0      \\ \cline{2-6} 
                        & \multirow{2}{*}{64}        & 0.7495    & 0.8008    & 0.8027    & 0.8128    \\             
                        &                            & ±0.0      & ±0.0      & ±0.0      & ±0.0      \\ \hline      
 
\multirow{6}{*}{XLM-RoBERTa}    & \multirow{2}{*}{16}        & 0.5972    & 0.5939    & 0.6351    & 0.6162    \\             
                        &                            & ±0.0      & ±0.0      & ±0.0      & ±0.0      \\ \cline{2-6} 
                        & \multirow{2}{*}{32}        & 0.5855    & 0.5929    & \textit{0.6364}   & 0.6057    \\             
                        &                            & ±0.0      & ±0.0      & ±0.0      & ±0.0      \\ \cline{2-6} 
                        & \multirow{2}{*}{64}        & 0.5641    & 0.5943    & 0.5989    & 0.6177    \\             
                        &                            & ±0.0      & ±0.0      & ±0.0      & ±0.0      \\ \hline      
 
\end{tabular}

At the moment, it looks like that: enter image description here

However, I would love to have it looking that way (with other colours for sure): enter image description here Are there any nice ideas out there how to do so? The \rowcolors{1}{grey!25}{white} command seems to work only with two colours

3

With \rowcolor for each row and a different placement of the \multirow commands, you can achieve the following output. Personally, I'd prefer a different colorless approach that also removes almost all lines in the table. I have also included the code for this approach as well:

enter image description here

\documentclass{article}
\usepackage{geometry}
\usepackage{multirow}
\usepackage[table,svgnames]{xcolor}

% Just used in the second example table:
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{makecell}
\renewcommand{\theadfont}{\normalsize}

\usepackage{etoolbox}
\robustify\bfseries
\robustify\itshape


\begin{document}

\begin{tabular}{|c|c|l|l|l|l|}
\hline
\multirow{2}{*}{Modell} & \multicolumn{1}{l|}{\multirow{2}{*}{Batch size}} & \multicolumn{4}{c|}{Learning rate} \\ \cline{3-6}
                        & \multicolumn{1}{l|}{}                            & 0.05    & 0.10   & 0.15   & 0.20   \\ \hline
\hline
\rowcolor{LightYellow}\cellcolor{white}                                    &                      & 0.7319  & 0.7705  & 0.7901          & \textit{0.8316}   \\             
\rowcolor{LightYellow}\cellcolor{white}                                    & \multirow{-2}{*}{16} & ±0.0    & ±0.0    & ±0.0            & ±0.0      \\ \cline{2-6} 
\rowcolor{LightPink}\cellcolor{white}                                      &                      & 0.631   & 0.681   & 0.7658          & 0.7747    \\             
\rowcolor{LightPink}\cellcolor{white}                                      & \multirow{-2}{*}{32} & ±0.0    & ±0.0    & ±0.0            & ±0.0      \\ \cline{2-6} 
\rowcolor{LightCyan}\cellcolor{white}                                      &                      & 0.5919  & 0.6628  & 0.6676          & 0.7105    \\             
\rowcolor{LightCyan}\cellcolor{white}\multirow{-6}{*}{Multilingual Flair}  & \multirow{-2}{*}{64} & ±0.0    & ±0.0    & ±0.0            & ±0.0      \\ 
\hline      
 
\rowcolor{LightYellow}\cellcolor{white}                                    &                      & 0.8113  & 0.8215  & 0.8279          & 0.8307    \\             
\rowcolor{LightYellow}\cellcolor{white}                                    & \multirow{-2}{*}{16} & ±0.0    & ±0.0    & ±0.0            & ±0.0      \\ \cline{2-6} 
\rowcolor{LightPink}\cellcolor{white}                                      &                      & 0.7918  & 0.8236  & \textbf{0.8338}  & 0.8286   \\             
\rowcolor{LightPink}\cellcolor{white}                                      & \multirow{-2}{*}{32} & ±0.0    & ±0.0    & ±0.0            & ±0.0      \\ \cline{2-6} 
\rowcolor{LightCyan}\cellcolor{white}                                      &                      & 0.7495  & 0.8008  & 0.8027          & 0.8128    \\             
\rowcolor{LightCyan}\cellcolor{white}\multirow{-6}{*}{BERT}                & \multirow{-2}{*}{64} & ±0.0    & ±0.0    & ±0.0            & ±0.0      \\ 
\hline      
 
\rowcolor{LightYellow}\cellcolor{white}                                    &                      & 0.5972  & 0.5939  & 0.6351          & 0.6162    \\             
\rowcolor{LightYellow}\cellcolor{white}                                    & \multirow{-2}{*}{16} & ±0.0    & ±0.0    & ±0.0            & ±0.0      \\ \cline{2-6} 
\rowcolor{LightPink}\cellcolor{white}                                      &                      & 0.5855  & 0.5929  & \textit{0.6364} & 0.6057    \\             
\rowcolor{LightPink}\cellcolor{white}                                      & \multirow{-2}{*}{32} & ±0.0    & ±0.0    & ±0.0            & ±0.0      \\ \cline{2-6} 
\rowcolor{LightCyan}\cellcolor{white}                                      &                      & 0.5641  & 0.5943  & 0.5989          & 0.6177    \\             
\rowcolor{LightCyan}\cellcolor{white} \multirow{-6}{*}{XLM-RoBERTa}        & \multirow{-2}{*}{64} & ±0.0    & ±0.0    & ±0.0            & ±0.0      \\ 
\hline      
\end{tabular}
\bigskip

\sisetup{separate-uncertainty, table-format=1.4(5), detect-all}
\setlength{\tabcolsep}{0pt}
\noindent
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lcSSSS}
\toprule
\thead{Modell} & \thead{Batch\\ size} & \multicolumn{4}{c}{\thead{Learning rate}}              \\ 
\cmidrule{3-6}
                    &     & {0.05}    & {0.10}     & {0.15}           & {0.20}                 \\ 
\midrule
Multilingual Flair  & 16  & 0.7319(1) & 0.7705(22) & 0.7901(333)      & \itshape 0.8316(4444)  \\             
                    & 32  & 0.631     & 0.681      & 0.7658           & 0.7747                 \\             
                    & 64  & 0.5919    & 0.6628     & 0.6676           & 0.7105                 \\             
\addlinespace    
BERT                & 16  & 0.8113    & 0.8215     & 0.8279           & 0.8307                 \\             
                    & 32  & 0.7918    & 0.8236     & \bfseries 0.8338 & 0.8286                 \\             
                    & 64  & 0.7495    & 0.8008     & 0.8027           & 0.8128                 \\             
\addlinespace      
XLM-RoBERTa         & 16   & 0.5972    & 0.5939    & 0.6351           & 0.6162                 \\             
                    & 32   & 0.5855    & 0.5929    & \itshape 0.6364  & 0.6057                 \\             
                    & 64   & 0.5641    & 0.5943    & 0.5989           & 0.6177                 \\             
\bottomrule      
\end{tabular*}

\end{document}
5
  • This is wonderful, thank you so much for your effort! I do have two questions about it: 1) Is there anything I need to reset after the table is finished? (like that one with tablecolsep), I want to use other tables in the document as well – DORpapst Nov 20 '20 at 19:36
  • 2) Can I adjust the column width? It seems like it isn't enough width and the numbers overlap when I insert it. I might need to have a look why this is the case – DORpapst Nov 20 '20 at 19:40
  • 1
    @DORpapst: There is not really the need to reset anything. Just place a { right before the \sisetup... and a } right after \end{tabular*} and all setting are kept local to this specific table. If you place your table in a table environment (to get it to float), there settings are local to the corresponding table environment, automatically. – leandriis Nov 20 '20 at 19:44
  • 1
    The table width automatically adapts to the textwidth. Since you didn't provide information about the documentclass and package you use, I assumed some settings. To make sure the numbers don't overlap, you could either decrease the margin size or adapt the font size within the table accordingly. For more specifi advice, please provide the necessary settings. – leandriis Nov 20 '20 at 19:44
  • Thanks for the hint, it works now. The latter question was due to me copying it wrong ;) – DORpapst Nov 20 '20 at 20:49
2

A solution without multirow, based on collcell and stackengine:

\documentclass{article}
\usepackage{amsmath}
\usepackage[table, svgnames]{xcolor}
\usepackage{multirow, hhline}
\usepackage{collcell}
\newcolumntype{L}{>{$\collectcell\Vectorstack}l<{\endcollectcell$}}
\usepackage[usestackEOL]{stackengine}
\setstackEOL{\nl}
\setlength{\extrarowheight}{2pt}

\begin{document}

\begin{tabular}{|>{\cellcolor{white}}c|>{\cellcolor{white}}c|*{4}{L|}}
\hline
&& \multicolumn{4}{c|}{Learning rate} \\
\hhline{|~|~|----|}
\multirow{-2}{*}{Modell} & \multirow{-2}{*}{Batch size} & 0.05 & 0.10 & 0.15 & 0.20 \\
\hline\hline
\rowcolor{Wheat} & 16 & 0.7319\nl \pm 0.0 & 0.7705\nl \pm 0.0 & 0.7901\nl \pm 0.0 & \textit{0.8316}\nl \pm 0.0 \\
\hhline{|~|-----|}
\rowcolor{LavenderBlush}Multilingual Flair & 32 & 0.631\nl \pm 0.0 & 0.681\nl \pm 0.0 & 0.7658\nl \pm 0.0 & 0.7747\nl \pm 0.0 \\
\hhline{|~|-----|}
\rowcolor{PowderBlue!50} & 64 & 0.5919\nl \pm 0.0 & 0.6628\nl \pm 0.0 & 0.6676\nl \pm 0.0 & 0.7105\nl \pm 0.0 \\
 \hline
\rowcolor{Wheat} & 16 & 0.8113 \nl \pm 0.0 & 0.8215\nl \pm 0.0 & 0.8279 \nl \pm 0.0 & 0.8307 \nl \pm 0.0 \\% \cline{2-6}
\hhline{|~|-----|}
\rowcolor{LavenderBlush}BERT & 32 & 0.7918\nl \pm 0.0 & 0.8236\nl \pm 0.0 & \textbf{0.8338}\nl \pm 0.0 & 0.8286\nl \pm 0.0 \\
\hhline{|~|-----|}
\rowcolor{PowderBlue!50} & 64 & 0.7495\nl \pm 0.0 & 0.8008\nl \pm 0.0 & 0.8027\nl \p 0.0 & 0.8128\nl \pm 0.0 \\
\hline
\rowcolor{Wheat} & 16 & 0.5972\nl \pm 0.0 & 0.5939\nl \pm 0.0 & 0.6351\nl \pm 0.0 & 0.6162\nl \pm 0.0 \\
\hhline{|~|-----|}
\rowcolor{LavenderBlush}XLM-RoBERTa & 32 & 0.5855 \nl \pm 0.0 & 0.5929\nl \pm 0.0 & \textit{0.6364}\nl \pm 0.0 & 0.6057 \nl \pm 0.0 \\
\hhline{|~|-----|}
\rowcolor{PowderBlue!50} & 64 & 0.5641\nl \pm 0.0 & 0.5943\nl \pm 0.0 & 0.5989\nl \pm 0.0 & 0.6177\nl \pm 0.0 \\
 \hline
\end{tabular}

\end{document} 

enter image description here

1
  • Thanks for your answer! It seems like you also solved my problem with those two merged rows per batch. One more nice solution! – DORpapst Nov 20 '20 at 20:48
2

You can easily do that table with nicematrix.

The output will be perfect in all the PDF viewers, at all levels of zoom (you won't have rules which seem to vanish).

However, you need several compilations (because nicematrix uses PGF/Tikz nodes).

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{ccllll}%
 [ 
   vlines ,
   code-before = 
    \rectanglecolor{yellow!15}{3-2}{4-6}
    \rectanglecolor{red!15}{5-2}{6-6}
    \rectanglecolor{blue!15}{7-2}{8-6}
    \rectanglecolor{yellow!15}{9-2}{10-6}
    \rectanglecolor{red!15}{11-2}{12-6}
    \rectanglecolor{blue!15}{13-2}{14-6}
    \rectanglecolor{yellow!15}{15-2}{16-6}
    \rectanglecolor{red!15}{17-2}{18-6}
    \rectanglecolor{blue!15}{19-2}{20-6}
 ]
\Hline
\Block{2-1}{Modell} & \Block{2-1}{Batch size} & \Block[c]{1-4}{Learning rate} \\ \Hline
                        &                            & 0.05    & 0.10   & 0.15   & 0.20   \\ 
\Hline
\Block{6-1}{Multilingual Flair}    & \Block{2-1}{16}        & 0.7319    & 0.7705    & 0.7901    & \textit{0.8316}   \\             
                        &                            & ±0.0      & ±0.0      & ±0.0      & ±0.0      \\ \Hline
                        & \Block{2-1}{32}        & 0.631     & 0.681     & 0.7658    & 0.7747    \\             
                        &                            & ±0.0      & ±0.0      & ±0.0      & ±0.0      \\ \Hline
                        & \Block{2-1}{64}        & 0.5919    & 0.6628    & 0.6676    & 0.7105    \\             
                        &                            & ±0.0      & ±0.0      & ±0.0      & ±0.0      \\ \Hline      
\Block{6-1}{BERT}   & \Block{2-1}{16}        & 0.8113    & 0.8215    & 0.8279    & 0.8307    \\             
                        &                            & ±0.0      & ±0.0      & ±0.0      & ±0.0      \\ \Hline
                        & \Block{2-1}{32}        & 0.7918    & 0.8236    & \textbf{0.8338}   & 0.8286    \\             
                        &                            & ±0.0      & ±0.0      & ±0.0      & ±0.0      \\ \Hline
                        & \Block{2-1}{64}        & 0.7495    & 0.8008    & 0.8027    & 0.8128    \\             
                        &                            & ±0.0      & ±0.0      & ±0.0      & ±0.0      \\ \Hline
 
\Block{6-1}{XLM-RoBERTa}    & \Block{2-1}{16}        & 0.5972    & 0.5939    & 0.6351    & 0.6162    \\             
                        &                            & ±0.0      & ±0.0      & ±0.0      & ±0.0      \\ \Hline
                        & \Block{2-1}{32}        & 0.5855    & 0.5929    & \textit{0.6364}   & 0.6057    \\             
                        &                            & ±0.0      & ±0.0      & ±0.0      & ±0.0      \\ \Hline
                        & \Block{2-1}{64}        & 0.5641    & 0.5943    & 0.5989    & 0.6177    \\             
                        &                            & ±0.0      & ±0.0      & ±0.0      & ±0.0      \\ \Hline
\end{NiceTabular}

\end{document}

Output of the above code

5
  • Thanks for your answer as well. Is it possible to put that in a floating environment and label/caption it? I have never worked with tabular. But it looks good! – DORpapst Nov 20 '20 at 20:48
  • You can place an environment {NiceTabular} in a {table} exactly as you would do with an standard environment {tabular}. – F. Pantigny Nov 20 '20 at 20:51
  • Really? That’s amazing. Thank you so much for your answer, I wish a wonderful evening – DORpapst Nov 20 '20 at 20:53
  • Is there are reason why you write \Hline with capital h? Does that make a difference? – DORpapst Nov 20 '20 at 22:46
  • 1
    With \Hline (of nicematrix), the horizontal rule is not drawn in the blocks (created by \Block). – F. Pantigny Nov 20 '20 at 22:48

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.