30

So I need to make a colored table with multirows, together with the use of partial horizontal lines. I used \cline which does not work: the \cellcolor-command overwrites the line. This question told me to change it to \hhline which will not be overwritten by \cellcolor. Instead, \hhline overwrites the color.

To make it more clear:

Without color:

\documentclass{article}

\usepackage{colortbl}
\usepackage{multirow}

\begin{document}

  \begin{tabular}{|c|c|}
    \hline
    \bfseries ColumnOne & \bfseries ColumnTwo\\ \hline
    First data & 932\\ \hline
     & 239\\ \cline{2-2}  % or \hhline{|~|-|}
     & 137\\ \cline{2-2}  % or \hhline{|~|-|}
    \multirow{-3}{*}{More data} & 319\\ \hline
    Last data & 132\\ \hline
  \end{tabular}

\end{document}

Using <code>\cline</code> without color

With color:
Using \cline

\documentclass{article}

\usepackage{colortbl}
\usepackage{multirow}

\begin{document}

  \begin{tabular}{|>{\columncolor{red}}c|>{\columncolor{blue}}c|}
    \hline
    \bfseries ColumnOne & \bfseries ColumnTwo\\ \hline
    First data & 932\\ \hline
     & 239\\ \cline{2-2}
     & 137\\ \cline{2-2}
    \multirow{-3}{*}{More data} & 319\\ \hline
    Last data & 132\\ \hline
  \end{tabular}

\end{document}

enter image description here Makes the lines in the right column disappear.

Using \hhline

\documentclass{article}

\usepackage{colortbl}
\usepackage{multirow}
\usepackage{hhline}

\begin{document}

  \begin{tabular}{|>{\columncolor{red}}c|>{\columncolor{blue}}c|}
    \hline
    \bfseries ColumnOne & \bfseries ColumnTwo\\ \hline
    First data & 932\\ \hline
     & 239\\ \hhline{|~|-|}
     & 137\\ \hhline{|~|-|}
    \multirow{-3}{*}{More data} & 319\\ \hline
    Last data & 132\\ \hline
  \end{tabular}

\end{document}

enter image description here Creates white lines on the left column.

Is there any possible way on how to resolve this?

6
  • I'd recommend reading this answer on avoiding horizontal rules in tables. Jul 30, 2012 at 13:46
  • Please do not edit my post without knowing the context, i.e. why I want to do this. I know what I'm doing and why I want to do it.
    – Didii
    Jul 30, 2012 at 13:56
  • @Didii: I agree; you can roll back an edit if you wish.
    – Werner
    Jul 30, 2012 at 14:08
  • I agree with Werner; please feel absolutely free to roll back the edit. It's worth mentioning, though, that one of the things that attract many of us here to TeX is the beauty of it's output, hence the warning, which was, please believe me, intended entirely in the spirit of friendship. Jul 30, 2012 at 14:13
  • On reflection, and considering the opinions of friends, I've rolled it back. Please accept my apologies. Jul 30, 2012 at 14:16

4 Answers 4

26

I'm sorry the interface is not brilliant here: But you can do this:

\documentclass{article}

\usepackage{colortbl}
\usepackage{multirow}
\usepackage{hhline}

\begin{document}

  \begin{tabular}{|>{\columncolor{red}}c|>{\columncolor{blue}}c|}
    \hline
    \bfseries ColumnOne & \bfseries ColumnTwo\\ \hline
    First data & 932\\ \hline
     & 239\\ \hhline{|>{\arrayrulecolor{red}}->{\arrayrulecolor{black}}|-|}
     & 137\\ \hhline{|>{\arrayrulecolor{red}}->{\arrayrulecolor{black}}|-|}
    \multirow{-3}{*}{More data} & 319\\ \hline
    Last data & 132\\ \hline
  \end{tabular}

\end{document}

Result:

enter image description here

5
  • Never thought of that solution. It's indeed not the best solution but it works nonetheless :) So thanks!
    – Didii
    Jul 30, 2012 at 13:58
  • 3
    I've noticed that in there are very small white-ish lines in between the cells of the multirows - has anyone found a way to get rid of those?
    – zje
    Dec 5, 2013 at 5:02
  • @zje , the lines are shown in my preview (writelatex.com), but not in the generated pdf. The pdf is neat and clean.
    – Alisa
    Apr 28, 2017 at 21:16
  • @zje I think it depends on the viewer, I don't get them in the texworks editor, but I do get the lines in evince Document Viewer
    – Jonno_FTW
    Feb 13, 2020 at 5:10
  • @Jonno_FTW I was using evince, so that is consistent
    – zje
    Feb 15, 2020 at 17:57
5

It’s easy peasy with tblr environment of the new LaTeX3 package tabularray: multirow, cline and cellcolor work well together as expected. You can even replace \cline{2}'s with \hlines in the following code.

\documentclass{article}

\usepackage{xcolor}
\usepackage{tabularray}

\begin{document}

\begin{tblr}{
  colspec = {|Q[c,red7]|Q[c,blue7]|},
  cell{3}{1} = {r=3}{m}, % multirow
  row{1} = {font=\bfseries},
}
  \hline
    ColumnOne & ColumnTwo \\
  \hline
    First data & 932      \\
  \hline
    More data  & 239      \\
  \cline{2}
               & 137      \\
  \cline{2}
               & 319      \\
  \hline
    Last data  & 132      \\
  \hline
\end{tblr}

\end{document}

enter image description here

2

The environment {NiceTabular} of nicematrix is a solution to that problem. The commands \cline are respected. Moreover, \cline{2} can be used as a syntactic shortcut for \cline{2-2}.

\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{|c|c|}
 \CodeBefore 
   \columncolor{red!15}{1} 
   \columncolor{blue!15}{2}
 \Body
  \hline
  \RowStyle{\bfseries}
  ColumnOne    & ColumnTwo \\ \hline
  First data             & 932 \\ \hline
  \Block{3-1}{More data} & 239 \\ \cline{2-2}
                         & 137 \\ \cline{2}
                         & 319 \\ \hline
  Last data              & 132 \\ \hline
\end{NiceTabular}

\end{document}

enter image description here

In fact, in this case, it's possible to simplify the code. With the key hvlines, all the horizontal and vertical rules will be drawn except in the blocks (created by the commands \Block of nicematrix). That's what we need.

\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{cc}[hvlines]
 \CodeBefore
   \columncolor{red!15}{1} 
   \columncolor{blue!15}{2}
 \Body
   \RowStyle{\bfseries}
   ColumnOne              & ColumnTwo \\ 
   First data             & 932 \\ 
   \Block{3-1}{More data} & 239 \\ 
                          & 137 \\ 
                          & 319 \\ 
   Last data              & 132 \\ 
\end{NiceTabular}

\end{document}

Result of the second code

0
1

I'm late to the party. And, to be sure, this fix is hacky and does not provide a solution without some fiddling. It did solve my problem fairly cleanly, and since I found this discussion, I'll add my answer here.

It appears that the color fill on the cell can be "tricked" into not covering up the cline by adding a carriage return and then taking away some vertical space, such that the line is left in place while the cell is slightly moved downward. I had to compile your code a few times with the fix in place, to get the correct vertical spacing of -4.1mm, but it avoids a lot of rework if one needs a quick fix.

\documentclass{article}

\usepackage{colortbl}
\usepackage{multirow}

\begin{document}

  \begin{tabular}{|>{\columncolor{red}}c|>{\columncolor{blue}}c|}
    \hline
    \bfseries ColumnOne & \bfseries ColumnTwo\\ \hline
    First data & 932\\ \hline
     & 239\\ \cline{2-2}\\[-4.1mm]
     & 137\\ \cline{2-2}\\[-4.1mm]
    \multirow{-3}{*}{More data} & 319\\ \hline
    Last data & 132\\ \hline
  \end{tabular}

\end{document}

Result:

result of the hacky fix

1
  • The latecomers are always the coolest. This is a beautifully easy fix. It may not look pretty to the programmer but it's the easy-peasiest answer in the bunch and it works perfectly.
    – user93292
    Jun 8, 2022 at 14:17

You must log in to answer this question.

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