2

I tried to use the answer to the question asked here: How To Color A Single Multicolumn Cell

But if I use more rows with different multicolumn spacing, some of the colored multicolum-cells still have white padding on the right hand side. What am I doing wrong?

multicolumns in tabularx-tables with white paddings

\documentclass{report}
\usepackage[margin=1in,headheight=.5in,headsep=0.25in]{geometry}
\usepackage[table]{xcolor}
\usepackage{ltablex}
\keepXColumns
\renewcommand\tabularxcolumn[1]{m{#1}}% for vertical centering text in X column
\newcolumntype{C}{>{\centering\arraybackslash}X}

\newcommand{\cell}[2]{\multicolumn{#1}{|>{\hsize=%
    \dimexpr#1\linewidth+#1\tabcolsep+#1\tabcolsep-#1\arrayrulewidth}C|}{#2}}
\newcommand{\colorcell}[2]{\multicolumn{#1}{|>{\columncolor{red}\hsize=%
    \dimexpr#1\linewidth+#1\tabcolsep+#1\tabcolsep-#1\arrayrulewidth}C|}{#2}}

\begin{document}

Standard Column Color

\begin{tabularx}{\textwidth}{|*{8}{C|}}
  \hline
  7 & 6 & 5 & 4 & 3 & 2 & 1 & 0     \\
  \hline
  \colorcell{1}{ a a a a a a a a a a a a}
    &  CLEAR
        &   \colorcell{2}{Colored Cell}
            &   CLEAR
                &   \colorcell{3}{Colored Cell}     \\
  \hline
  \colorcell{2}{ a a a a a a a a a a a a}
    &  CLEAR
        &   \colorcell{2}{Colored Cell}
            &   CLEAR
                &   \colorcell{2}{Colored Cell}     \\
  \hline
  a a a a a a a a a a a a
    &  CLEAR
        &   CLEAR
            &   CLEAR
                &   \colorcell{4}{Colored Cell}     \\
  \hline


\end{tabularx}

\end{document}
9
  • you have removed the | from the left of cells in the first column you need a | at the left of the \multicolumn if in the first column (or better don't use vertical rules in coloured tables) Commented Feb 29, 2020 at 15:28
  • also \dimexpr#1\linewidth+#1\tabcolsep+#1\tabcolsep-#1\arrayrulewidth} looks wrong, the #1 need to be \numexpr#1-1\relax : if you span two columns then you only need to account for the middle column boundary, so that's 2\tabcolsep and 1\arrayrulewidth you need to add to the width of the text in the cell. Commented Feb 29, 2020 at 15:39
  • @DavidCarlisle I used the exact code from the proposed answer, just adding two rows. For your first comment: isn't the | mentioned at the beginning of the newcommand-definition {|>{\columncolor{red}\hsize=%?
    – NewBee
    Commented Feb 29, 2020 at 16:05
  • oh sorry it is at the start (which means it's wrong if you use the command in a later column) in tabular a | is always part of the cell to its left except the first column which has rules to left and right. A s for the second comment yes the arithmetic looks wrong in the answer you copied. Commented Feb 29, 2020 at 16:11
  • If you need to have vertical and horizontal lines together with colour, the you will face the problem that the colour cells overlay the rules unless you zoom in to a 2x-3x magnification in your PDF-viewer. You will find lot of questions and answers regarding this topic at SE. The only package that handle rules and colour cells without lot of hacking, is cals. Rules together with colours are normally unnecessary, but take a look at cals if someone force your to combine rules and colours.
    – Sveinung
    Commented Feb 29, 2020 at 20:02

3 Answers 3

1

In the evaluation of the multicolumn width, you also subtracted the first and the last rule thickness, which you shouldn't have done. Here is a simplified code that gives the expected result. I also had to defined two variants(colorcell, and \lcolorcell for the case when the \multicolumn is the first in its row) to avoid double vertical rules.

\documentclass{report}
\usepackage[margin=1in,headheight=.5in,headsep=0.25in]{geometry}
\usepackage[table]{xcolor}
\usepackage{ltablex}
\keepXColumns
\renewcommand\tabularxcolumn[1]{m{#1}}% for vertical centering text in X column
\newcolumntype{C}{>{\centering\arraybackslash}X}

\newcommand{\cell}[2]{\multicolumn{#1}{|>{\hsize=%
    \dimexpr#1\linewidth+#1\tabcolsep+#1\tabcolsep-#1\arrayrulewidth}C|}{#2}}
\newcommand{\colorcell}[2]{\multicolumn{#1}{>{\columncolor{red}[\tabcolsep]\hsize=%
 \dimexpr (\linewidth+2\tabcolsep + \arrayrulewidth)*#1-2\arrayrulewidth}C|}{#2}}

\newcommand{\lcolorcell}[2]{\multicolumn{#1}{|>{\columncolor{red}[\tabcolsep]\hsize=%
 \dimexpr (\linewidth+2\tabcolsep + \arrayrulewidth)*#1-2\arrayrulewidth}C|}{#2}}

\begin{document}

Standard Column Color

\begin{tabularx}{\textwidth}{|*{8}{C|}}
  \hline
  7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 \\
  \hline
  \cellcolor{red} a a a a a a a a a a a a
    & CLEAR
        & \colorcell{2}{Colored Cell}
            & CLEAR
                & \colorcell{3}{Colored Cell} \\
  \hline
  \lcolorcell{2}{a a a a a a a a a a a a}
    & CLEAR
        & \colorcell{2}{Colored Cell}
            & CLEAR
                & \colorcell{2}{Colored Cell} \\
  \hline
  a a a a a a a a a a a a
    & CLEAR
        & CLEAR
            & CLEAR
                & \colorcell{4}{Colored Cell} \\
  \hline

\end{tabularx}

\end{document} 

enter image description here

9
  • Trying your code, my output looks different for the upper and left rules (where they nearly disappear). But it solves the problem with the white paddings and as your output looks fine, I guess it's just a viewer problem. Thanks for your quick help.
    – NewBee
    Commented Feb 29, 2020 at 17:40
  • Yes, it may be a pdf viewer problem. Mine is a screenshot from Sumatrapdf. You can check what happens on printing, to be sure
    – Bernard
    Commented Feb 29, 2020 at 17:44
  • It's been a viewer problem. Using sumatra gives the same result as your screenshot. Thank you
    – NewBee
    Commented Mar 1, 2020 at 0:32
  • adding more rows with more combinations of multicolumns and colored (multicolumn) cells leads to more cells with white paddings on the right hand side. I guess, LateX doesn't want colored multicolumns ...
    – NewBee
    Commented Mar 2, 2020 at 20:09
  • Personally, each time I had a problem with colors in a table, it came from bad calculation of the multicolumn width. Could you post a code of this kind?
    – Bernard
    Commented Mar 2, 2020 at 20:22
3

With {NiceTabularX} of nicematrix (≥ 6.0 2021-08-10), you have directly the expected result.

The output PDF will be good in all the PDF viewers. The vertical rules won't seem to vanish at some levels of zoom.

\documentclass{report}
\usepackage[margin=1in,headheight=.5in,headsep=0.25in]{geometry}
\usepackage{xcolor}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabularX}{\textwidth}{*{8}{X[m,c]}}[hvlines,colortbl-like]
  7 & 6 & 5 & 4 & 3 & 2 & 1 & 0     \\
  \cellcolor{red} a a a a a a a a a a a a
    &  CLEAR
        &   \Block[fill=red]{1-2}{Colored Cell}
            &&   CLEAR
                &   \Block[fill=red]{1-3}{Colored Cell}  \\
  \Block[fill=red]{1-2}{ a a a a a a a a a a a a}
    &&  CLEAR
        &   \Block[fill=red]{1-2}{Colored Cell}
            &&   CLEAR
                &   \Block[fill=red]{1-2}{Colored Cell}  \\
  a a a a a a a a a a a a
    &  CLEAR
        &   CLEAR
            &   CLEAR
                &   \Block[fill=red]{1-4}{Colored Cell}  \\
\end{NiceTabularX}

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

1

Here is a calstable variant of the tabular. As you will see, rules are visible. The spanning of columns and rows are maybe important to learn.

Also, you may give exact value to the columns, instead of calculate them relative to the margin.

enter image description here

\documentclass[11 pt, a4paper, oneside, openany]{book}
\usepackage{cals, caption}
\usepackage[svgnames]{xcolor}
\usepackage[T1]{fontenc}

\let\nc=\nullcell                        % Shortcuts
\let\sc=\spancontent

\begin{document}

\begin{table}[tbh!]
\caption{calstable can float\label{tab:calstable}}

\begin{calstable}[c]               % Centred between margins

% Defining columns relative to each other and relative to the margins
\colwidths{{\dimexpr(\columnwidth/8)\relax}
           {\dimexpr(\columnwidth/8)\relax}
           {\dimexpr(\columnwidth/8)\relax}
           {\dimexpr(\columnwidth/8)\relax}
           {\dimexpr(\columnwidth/8)\relax}
           {\dimexpr(\columnwidth/8)\relax}
           {\dimexpr(\columnwidth/8)\relax}
           {\dimexpr(\columnwidth/8)\relax}
      }
% The tabular fills the text area if sum of all columns is 8

% Set up the tabular
\makeatletter
\def\cals@framers@width{0.4pt}   % Outside frame rules, reduce if the rule is too heavy
\def\cals@framecs@width{0.4pt}
\def\cals@bodyrs@width{0.4pt}          % Rule between header and body of the table
\def\cals@bgcolor{}
\def\cals@AtBeginCell{\vfil}           % Vertical centring all cell content
\setlength{\cals@paddingL}{3pt}        % cals’ tabcolsep,  
\setlength{\cals@paddingR}{3pt}


\def\lblue{\ifx\cals@bgcolor\empty     % "Switch" to turn on and off colour
    \def\cals@bgcolor{LightSteelBlue}
\else \def\cals@bgcolor{} \fi}


% R1H1
\thead{\footnotesize\bfseries
\brow
    \alignC\cell{7}
    \cell{6}
    \cell{5}
    \cell{4}
    \cell{3}
    \cell{2}
    \cell{1}
    \cell{0}
\erow
\mdseries}
\tfoot{\lastrule\strut}
%R2B1
\brow
    \lblue\cell{aaaaa\par aaaaa\par aa}\lblue
    \cell{CLEAR}
    \lblue\nc{ltb}
    \nc{rtb}\sc{Coloured cell}\lblue
    \cell{CLEAR}
    \lblue\nc{ltb}
    \nc{tb}
    \nc{rtb}\sc{Coloured cell}\lblue
\erow
%R3B2
\brow
    \lblue\nc{ltb}
    \nc{rtb}\sc{aaaaaaaaaaaa}\lblue
    \cell{CLEAR}
    \lblue\nc{ltb}
    \nc{rtb}\sc{Coloured cell}\lblue
    \cell{CLEAR}
    \lblue\nc{ltb}
    \nc{rtb}\sc{Coloured cell}\lblue
\erow
%R4B3
\brow
    \lblue\cell{aaaaa\par aaaaa\par aa}\lblue
    \cell{CLEAR}
    \cell{CLEAR}
    \cell{CLEAR}
    \lblue\nc{ltb}
    \nc{tb}
    \nc{tb}
    \nc{rtb}\sc{Coloured cell}\lblue
\erow
\makeatletter
\end{calstable}
\end{table}

\end{document}
5
  • Thank you for this solution. It's a little bit more complicated than the tabularx table, but I'll check, if I can adapt and use it for my problems. But as I see, the viewer is always important. With adobe there's no problem, but with sumatra, there are small white vertical lines.
    – NewBee
    Commented Mar 2, 2020 at 23:37
  • @NewBee Do you see white, vertical lines in Sumatra when you view the calstable?
    – Sveinung
    Commented Mar 3, 2020 at 7:15
  • @NewBee And when I look at the complicated calculation of coloured cells in your question and in the answer, I am surprised that you find cals complicated. :)
    – Sveinung
    Commented Mar 3, 2020 at 7:29
  • Yes, but they're very thin and hard to see.
    – NewBee
    Commented Mar 5, 2020 at 18:30
  • @NewBee this is obviously a viewer or screen issue.
    – Sveinung
    Commented Mar 5, 2020 at 21:21

You must log in to answer this question.

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