1

I need to create a summary table for a talk. The idea is to have cells with different colours. However, I do not manage to make the colours fill for one cell. Here is a small example of my problem.

Code

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{xcolor} 
\usepackage{color, colortbl}

%% How to color the cells
\newcommand{\ResultCell}[1]{%
\cellcolor{red!10}\textcolor{red}{#1}%
}

\newcommand{\SetPackingCell}[1]{%
\cellcolor{blue!10}\textcolor{blue}{#1}%
}

\begin{document}

\begin{table}[]
        \begin{tabular}{c|c|c}
        & VTT & VTS \\\hline
          \begin{tabular}[c]{@{}c@{}}
            Approximation\\
            ratio
          \end{tabular} 
        & \begin{tabular}[c]{@{}c@{}}
            \SetPackingCell{$(4/3+\varepsilon)$}\\
            \ResultCell{APX-hard}
          \end{tabular} 
        & \begin{tabular}[c]{@{}c@{}}
            \SetPackingCell{$(4/3+\varepsilon)$}\\
            \ResultCell{APX-hard}\\
            \ResultCell{$(1+\frac{6}{s-1})$}
           \end{tabular} 
        \\\hline
        Kernel 
        & \begin{tabular}[c]{@{}c@{}}
            \SetPackingCell{$O(k^{1.5})$-V}\\
            \ResultCell{$O(f)$}\\
            \ResultCell{no $O(N^{2-\varepsilon})$-bits}
          \end{tabular} 
        & \ResultCell{$O(k)$-V}
        \end{tabular}
\end{table}
\end{document}

Result Result

As you can notice, there is a cell with a bad colour-filling. The goal would be to have something like this

Expected output Goal

How could I fix this? I tried with multirow, etc.. but nothing worked so far.

Thank you in advance!

Edit - Personal (and ugly) solution

I managed to fix the problem with a multirow based from an answer of user11232, and some ugly phantom text...

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{xcolor} 
\usepackage{color, colortbl}

\usepackage{multirow} % https://www.ctan.org/pkg/multirow

%% How to color the cells
\newcommand{\ResultCell}[1]{%
\cellcolor{red!10}\textcolor{red}{#1}%
}

\newcommand{\SetPackingCell}[1]{%
\cellcolor{blue!10}\textcolor{blue}{#1}%
}

\begin{document}

\begin{table}[]
    \begin{tabular}{c|c|c}
    & VTT & VTS \\\hline
      \begin{tabular}[c]{@{}c@{}}
        Approximation\\
        ratio
      \end{tabular} 
    & \begin{tabular}[c]{@{}c@{}}
        \SetPackingCell{$(4/3+\varepsilon)$}\\
        \ResultCell{\phantom{iiino $\O(N^{2-\varepsilon})$ bits}}\\%adding some i's to fix the tiny remaining spaces... Better solution ? 
        \multirow{-2}{*}{\ResultCell{APX-hard}}\\
      \end{tabular} 
    & \begin{tabular}[c]{@{}c@{}}
        \SetPackingCell{$(4/3+\varepsilon)$}\\
        \ResultCell{APX-hard}\\
        \ResultCell{$(1+\frac{6}{s-1})$}
       \end{tabular} 
    \\\hline
    Kernel 
    & \begin{tabular}[c]{@{}c@{}}
        \SetPackingCell{$O(k^{1.5})$-V}\\
        \ResultCell{$O(f)$}\\
        \ResultCell{no $O(N^{2-\varepsilon})$-bits}
      \end{tabular} 
    & \ResultCell{$O(k)$-V}
    \end{tabular}
\end{table}
\end{document}

This code gives the following output.

Solution Solution

I hope it will be useful, even if I am highly not convinced by my solution. I am pretty sure there is a nicer way to fix my problem, but at least it does the job...

4
  • Welcome to TeX.SX! Unrelated to the issue itself, but please do not use adjustbox on a tabular since this will make the font sizes in your document inconsistent.
    – leandriis
    Oct 27, 2019 at 11:36
  • Please also clarify the desired output. Is i.sstatic.net/hq02i.png what you want to achieve or do you want the blue part and the red part to each take up half of the height?
    – leandriis
    Oct 27, 2019 at 11:45
  • @leandriis Thank you for the tip. I put an ajustbox because the full table is too big for the textwidth. The result will be in a beamer talk, so the font size consistency is, I guess, less crucial. However, I will remove it from the example, it will make things clearer.
    – YplN
    Oct 27, 2019 at 12:48
  • If you answer your own question, it must be in an answer and not in the question itself, see: tex.stackexchange.com/help/search?q=answer+own+question
    – AndréC
    Oct 28, 2019 at 11:51

2 Answers 2

1

The problem comes from the fact that you are using nested tabulars, when you don't really need them.

Removing nesting allows you to get rid of your phantom text. In case you don't really want the expected output with this weird step in coloration between cells:

Setp in coloration

The following should do the trick (using makecell and multirow packages):

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{xcolor} 
\usepackage{color, colortbl}

\usepackage{multirow}
\usepackage{makecell}

%% How to color the cells
\newcommand{\ResultCell}[1]{%
\cellcolor{red!10}\textcolor{red}{#1}%
}

\newcommand{\SetPackingCell}[1]{%
\cellcolor{blue!10}\textcolor{blue}{#1}%
}

\begin{document}
\begin{table}
\begin{tabular}{c|c|c}
& VTT & VTS \\
\hline
\multirow{3}{*}{\makecell{Approximation \\ ratio}} & \SetPackingCell{$(4/3+\varepsilon)$} & \SetPackingCell{$(4/3+\varepsilon)$} \\
 & \ResultCell{} & \ResultCell{APX-hard} \\
 & \multirow{-2}{*}{\ResultCell{APX-hard}} & \ResultCell{$(1+\frac{6}{s-1})$} \\
\hline
\multirow{3}{*}{Kernel} & \SetPackingCell{$O(k^{1.5})$-V} & \ResultCell{} \\
 & \ResultCell{$O(f)$} & \ResultCell{} \\
 & \ResultCell{no $O(N^{2-\varepsilon})$-bits} & \multirow{-3}{*}{\ResultCell{$O(k)$-V}}\\
\hline
\end{tabular}
\end{table}

\end{document}

Outputs

Output

Notice that the makecell package is just use to fit a two lines text into a three rows "cell".

2

For information, here is a solution with {NiceTabular} of nicematrix. With that environment, it's always possible to draw colored panels with Tikz (by using the PGF/Tikz nodes created by {NiceTabular}).

\documentclass{article}
\usepackage{nicematrix,tikz}
\usetikzlibrary{calc}

\begin{document}

\newcommand{\blue}{\color{blue}}
\newcommand{\red}{\color{red}}

\renewcommand{\arraystretch}{1.4}
\begin{NiceTabular}{ccc}[hvlines-except-borders,respect-arraystretch]
\CodeBefore
    \begin{tikzpicture}
    \fill [blue!15] (2-|2) rectangle (2.5-|3) ; 
    \fill [red!15] (2.5-|2) rectangle (3-|3) ; 
    % There are nodes 1, 1.5, 2, 2.5, 3, etc. but no node 3.3...
    % That's why we have to use the syntax ($node!0.3!node$)
    \fill [blue!15] (2-|3) rectangle ($(2-|4)!0.3!(3-|4)$) ; 
    \fill [blue!15] (2-|3) rectangle ($(2-|4)!0.3!(3-|4)$) ; 
    \fill [red!15] ($(2-|3)!0.3!(3-|3)$) rectangle (3-|4) ; 
    \fill [blue!15](3-|2) rectangle ($(3-|3)!0.3!(4-|3)$) ; 
    \fill [red!15] ($(3-|2)!0.3!(4-|2)$) rectangle (4-|3); 
    \fill [red!15] (3-|3) rectangle (4-|4) ; 
    \end{tikzpicture}
\Body
      & VTT & VTS \\
    \Block{}{Approximation\\ ratio}
      & \Block{}{\blue $(4/3+\varepsilon)$\\ \red APX-hard}
      & \Block{}{\blue $(4/3+\varepsilon)$\\ \red APX-hard \\ \red $(1+\frac{6}{s-1})$} \\
      Kernel 
      & \Block{}{\blue $O(k^{1.5})$-V \\ \red $O(f)$ \\ \red no $O(N^{2-\varepsilon})$-bits}
      & \red $O(k)$-V
\end{NiceTabular}

\end{document}

You need several compilations (because of the PGF/Tikz nodes).

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .