27

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?

  • I'd recommend reading this answer on avoiding horizontal rules in tables. – Brent.Longborough Jul 30 '12 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 '12 at 13:56
  • @Didii: I agree; you can roll back an edit if you wish. – Werner Jul 30 '12 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. – Brent.Longborough Jul 30 '12 at 14:13
  • On reflection, and considering the opinions of friends, I've rolled it back. Please accept my apologies. – Brent.Longborough Jul 30 '12 at 14:16
23

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

  • Never thought of that solution. It's indeed not the best solution but it works nonetheless :) So thanks! – Didii Jul 30 '12 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 '13 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 '17 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 '20 at 5:10
  • @Jonno_FTW I was using evince, so that is consistent – zje Feb 15 '20 at 17:57
1

The environment {NiceTabular} of nicematrix (≥ 4.1 2020-05-27) is a solution to that problem. The commands \cline are respected.

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

\begin{document}

\begin{NiceTabular}{|C|C|}[standard-cline,
  code-before = \columncolor{red!15}{1} \columncolor{blue!15}{2}
]
  \hline
  \bfseries ColumnOne    & \bfseries ColumnTwo \\ \hline
  First data             & 932 \\ \hline
  \Block{3-1}{More data} & 239 \\ \cline{2-2}
                         & 137 \\ \cline{2-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,
  code-before = \columncolor{red!15}{1} \columncolor{blue!15}{2}
]
  \bfseries ColumnOne    & \bfseries ColumnTwo \\ 
  First data             & 932 \\ 
  \Block{3-1}{More data} & 239 \\ 
                         & 137 \\ 
                         & 319 \\ 
  Last data              & 132 \\ 
\end{NiceTabular}

\end{document}

Result of the second code

  • In this answer, since version 5.0 of nicematrix one must write c instead of C in the preambles of the {NiceTabular} (but there is an option for backward compatibility). – F. Pantigny Jul 18 '20 at 20:43

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.