TransWikia.com

issue with hhline and cellcolor in table

TeX - LaTeX Asked on January 5, 2022

I am trying to setup a table with hhline and cellcolor. After I got it so far, that I don’t see hhlines in the colored cells I am now missing the black hhlines which should separate the cells. This is how it should look like:

enter image description here

Here’s what I have:
enter image description here

documentclass{article}
% General document formatting
usepackage[margin=0.7in]{geometry}
usepackage[parfill]{parskip}
usepackage[utf8]{inputenc}
usepackage{multirow, hhline}

% Related to math
usepackage{amsmath,amssymb,amsfonts,amsthm}
usepackage{multirow}
usepackage[table,xcdraw]{xcolor}



begin{document}    
    begin{table}[!ht]  
        centering
        begin{tabular}{|c|c|c|}
            hhline{*{1}{-}}
            {cellcolor[HTML]{C0E5C6}}Modell-Überkategorien   &multicolumn{2}{c}{}    \ 
            cline{1-1}
            statisch                &multicolumn{2}{c}{}   \ 
            cline{1-1}
            zufällig (Monte-Carlo)  &multicolumn{2}{c}{}   \ 
            hhline{*{3}{-}}            
            cellcolor[HTML]{E2EFD9} & cellcolor[HTML]{E2EFD9} Kontinuirlich & cellcolor[HTML]{E2EFD9} \
            hhline{>{arrayrulecolor[HTML]{E2EFD9}}->{arrayrulecolor{black}}->{arrayrulecolor[HTML]{E2EFD9}}-}
            cellcolor[HTML]{E2EFD9} & cellcolor[HTML]{E2EFD9} Hybrid & cellcolor[HTML]{E2EFD9}\
            hhline{>{arrayrulecolor[HTML]{E2EFD9}}->{arrayrulecolor{black}}->{arrayrulecolor[HTML]{E2EFD9}}-} multirow{-3}{*}{{cellcolor[HTML]{E2EFD9}}dynamisch} 
            & cellcolor[HTML]{E2EFD9} Diskret & cellcolor[HTML]{E2EFD9} multirow{-3}{*}{Raum/Zeit}  \ 
            hline
            cellcolor[HTML]{E2EFD9} (Multi)-Agenten basiert &multicolumn{2}{c}{} \ 
            hhline{*{1}{-}}
        end{tabular}
        bigskip
    end{table}
end{document}

I have two missing hhlines and theres some small spaces inbetween the borders of the multirow cells. Is there a way to fix this issue?

3 Answers

Here is a solution with {NiceTabular} of nicematrix. This environment is similar to the classical {tabular} (of array) but with additional features.

  • With the key corners, you tell nicematrix to take into account the empty corners (see below).

  • You merge cells both vertically and horizontally with the command Block.

  • The key hvlines draws all the rules, taking into account the blocks and the corners.

  • There is commands to color cells, columsn and rows (in the CodeBefore). The cells in the corners are not filled.

With nicematrix, you won't have artefacts in some PDF viewers at some level of zoom (as you can have with PDF created with colortbl).

documentclass{article}
usepackage{nicematrix}

renewcommand{arraystretch}{1.2}
setlength{extrarowheight}{1pt}

begin{document}
begin{table}[!ht]
centering
begin{NiceTabular}{ccc}[corners,hvlines]
CodeBefore
  cellcolor[HTML]{C0E5C6}{1-1}
  rowcolor[HTML]{E2EFD9}{4-*}
Body
  Modell-Überkategorien \
  statisch \
  zufällig (Monte-Carlo)  \
  Block{3-1}{dynamisch} & Kontinuirlich & Block{3-1}{Raum/Zeit} \
                         & Hybrid        &  \
                         & Diskret \
  (Multi)-Agenten basiert
end{NiceTabular}
end{table}
end{document}

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

Ouput of the above code

Answered by F. Pantigny on January 5, 2022

Small modification of first example (+1) regarding use of colors in table in the nice Sveinung answer (for exercise)):

documentclass{article}
% General document formatting
usepackage[margin=0.7in]{geometry}
usepackage[parfill]{parskip}
usepackage[utf8]{inputenc}
usepackage{multirow, hhline}

% Related to math
usepackage{amsmath,amssymb,amsfonts,amsthm}
usepackage[table,xcdraw]{xcolor}
definecolor{col1}{HTML}{C0E5C6}    % <---
definecolor{col2}{HTML}{E2EFD9}    % <---

renewcommand{arraystretch}{1.2}
setlength{extrarowheight}{1pt}

begin{document}
    begin{table}[!ht]
    centering
begin{tabular}{|c|c|c|}
    hhline{-~~}
cellcolor{col1}{Modell-Überkategorien} % <---
                        &   multicolumn{2}{c}{}    \
    hhline{-~~}
statisch                &   multicolumn{2}{c}{}    \
    hhline{-~~}
zufällig (Monte-Carlo)  &   multicolumn{2}{c}{}    \
    hhline{*{3}{-}}
rowcolor{col2} 
                        & Kontinuirlich &           \  % <---
    hhline{>{arrayrulecolor{col2}}-%
            >{arrayrulecolor{black}}-%
            >{arrayrulecolor{col2}}-}
    arrayrulecolor{black}
rowcolor{col2} 
                        & Hybrid        &           \
    hhline{>{arrayrulecolor{col2}}-%
            >{arrayrulecolor{black}}-%
            >{arrayrulecolor{col2}}-}
    arrayrulecolor{black}
rowcolor{col2}
multirow{-3}{*}{dynamisch}
                        & Diskret       & multirow{-3}{*}{Raum/Zeit}   \
    hhline{|---}
cellcolor{col2} (Multi)-Agenten basiert
                        &   multicolumn{2}{c}{}    \
    hhline{|-~~}
end{tabular}
    end{table}
end{document}

Result is the same as in his answer.

Answered by Zarko on January 5, 2022

I have two MWEs: the last one cure both problems, the first does not cure the small green spaces.

Using hhline

You have forgotten to turn on arrayrowcolor{black} two places. In addition, the hhline- syntax was wrong one place. I have not found a solution for the small spaces in the horizontal borders (yet). It is the green coloured arrayrule under the multirow that overwrite the vertical line.

Not related: You load multirow twice.

documentclass{article}
% General document formatting
usepackage[margin=0.7in]{geometry}
usepackage[parfill]{parskip}
usepackage[utf8]{inputenc}
usepackage{multirow, hhline}

% Related to math
usepackage{amsmath,amssymb,amsfonts,amsthm}
usepackage[table,xcdraw]{xcolor}

renewcommand{arraystretch}{1.2}
setlength{extrarowheight}{1pt}

begin{document}    
    begin{table}[!ht]  
        centering
        begin{tabular}{|c|c|c|}
            hhline{-~~}
            {cellcolor[HTML]{C0E5C6}}Modell-Überkategorien   &multicolumn{2}{c}{}    \ 
            hhline{-~~}
            statisch                &multicolumn{2}{c}{}   \ 
            hhline{-~~}
            zufällig (Monte-Carlo)  &multicolumn{2}{c}{}   \ 
            hhline{*{3}{-}}            
            cellcolor[HTML]{E2EFD9} & cellcolor[HTML]{E2EFD9} Kontinuirlich & cellcolor[HTML]{E2EFD9} \
            hhline{>{arrayrulecolor[HTML]{E2EFD9}}->{arrayrulecolor{black}}->{arrayrulecolor[HTML]{E2EFD9}}-}arrayrulecolor{black}
            cellcolor[HTML]{E2EFD9} & cellcolor[HTML]{E2EFD9} Hybrid & cellcolor[HTML]{E2EFD9}\
            hhline{>{arrayrulecolor[HTML]{E2EFD9}}->{arrayrulecolor{black}}->{arrayrulecolor[HTML]
{E2EFD9}}-}arrayrulecolor{black}
multirow{-3}{*}{{cellcolor[HTML]{E2EFD9}}dynamisch} 
            & cellcolor[HTML]{E2EFD9} Diskret & cellcolor[HTML]{E2EFD9} multirow{-3}{*}{Raum/Zeit}  \ 
            hhline{|---}
            cellcolor[HTML]{E2EFD9} (Multi)-Agenten basiert &multicolumn{2}{c}{} \ 
            hhline{|-~~}
        end{tabular}
        bigskip
    end{table}
end{document}

enter image description here

Using cals

Your tabular is much easier to build using cals.

documentclass{article}
% General document formatting
usepackage[margin=0.7in]{geometry}
usepackage[parfill]{parskip}
usepackage[utf8]{inputenc}
usepackage{cals}

% Related to math
usepackage{amsmath,amssymb,amsfonts,amsthm}
usepackage[table,xcdraw]{xcolor}

letnc=nullcell                                                  % Shortcuts
letsc=spancontent

definecolor{darkGreen}{HTML}{C0E5C6}
definecolor{lightGreen}{HTML}{E2EFD9}


begin{document}

begin{calstable}

% Defining 3 column relativ to each other and relativ to the margins
colwidths{{dimexpr(columnwidth/16*4)relax}
            {dimexpr(columnwidth/16*3)relax}
            {dimexpr(columnwidth/16*3)relax}
            }
% The tabular fills 5/8 of the text area

% Set up the tabular
makeatletter
defcals@framers@width{0.4pt}   % Outside frame horizontal rules
defcals@framecs@width{0.4pt}   % Outside frame horizontal rules, reduce
                                 % thickness if you find them too heavy
cals@setpadding{Ag}
cals@setcellprevdepth{Al}
defcals@cs@width{0.4pt}        % Inside rules, reduce if the rule is too heavy
defcals@rs@width{0.4pt}
defcals@bgcolor{}              % Define cals@bgcolor to empty

defgreen{ifxcals@bgcolorempty      % Colour switch (on-off)
    defcals@bgcolor{darkGreen}
else defcals@bgcolor{} fi}

deflgreen{ifxcals@bgcolorempty     % Colour switch (on-off)
    defcals@bgcolor{lightGreen}
else defcals@bgcolor{} fi}

deftb{ifxcals@borderTrelax     % Top border switch (off-on)
    defcals@borderT{0pt}
else letcals@borderTrelaxfi}

defbb{ifxcals@borderBrelax     % Botton border switch (off-on)
    defcals@borderB{0pt}
else letcals@borderBrelaxfi}

defrb{ifxcals@borderRrelax     % Right border switch (off-on)
    defcals@borderR{0pt}
else letcals@borderRrelaxfi}

% R1 Body
brow
    greenalignCcell{Modell-Überkategorien}green
    tbrbnc{lt}
    nc{rt}tbrb
erow
% R2 Body
brow
    cell{statisch}
    rbnc{l}
    nc{r}rb
erow
% R3 Body
brow
    cell{zufällig (Monte-Carlo)}
    rbnc{lb}
    nc{rb}sc{}rb
erow
% R4 Body
brow
    lgreennc{lrt}
    cell{Kontinuirlich}
    nc{lrt}
erow
% R5 Body
brow
    nc{lr}
    cell{Hybrid}
    nc{lr}
erow
% R6 Body
brow
    nc{lrb}sc{vfil dynamisch}
    cell{Diskret}
    nc{lrb}sc{vfil Raum/Zeit}
erow
% R7 Body
brow
    cell{(Multi)-Agenten basiert}lgreen
    rbbbnc{ltb}
    nc{rtb}sc{}bbrb
erow
makeatletter
end{calstable}par % par needed to align the tabular

end{document}

enter image description here

Answered by Sveinung on January 5, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP