23

In a table, containing colored cells, i want to have a \hline dividing only some cells, similar to \cline but compatible with coloring single cells with \cellcolor, which is possible by using \hhline with fitting parameters.

Now I have come to the appended code, which results in: tables_hhline

There are two points I want to work around.

First, the black cells should not be crossed by a white line. Since they will not be black in the final document, I need them to be not black, either. Is there a way to not draw any line at all? These cells should look like a multirow cell, colored consistently.

The second problem are the hline fragments at the right end of the table. How can I get rid of them?

Complete code:

\documentclass[11pt]{article}

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

\setlength{\arrayrulewidth}{1pt}


\begin{document}
% Empty table
\begin{tabular}{*{5}{!{\vrule width 1.5pt}p{1.5cm}}!{\vrule width 1.5pt}l}
\centering 4 &
\centering 10 &
\centering 24 &
\centering 48 &
\centering 72 &\\ \hhline{*{5}{|-}|~|}
 &  &  &  &  & Optic tectum \\ \hhline{*{2}{|~}*{3}{|-}|~|}
 &  &  &  &  & Retina \\ \hhline{*{2}{|~}*{3}{|-}|~|}
 &  &  &  &  & Myomeres \\ \hhline{*{5}{|-}|~|}
 &  &  &  &  & Arches \\ \hhline{*{5}{|-}|~|}
\end{tabular} \\

\hspace*{1cm}

% Partically filled table
\begin{tabular}{*{5}{!{\vrule width 1.5pt}p{1.5cm}}!{\vrule width 1.5pt}l}
\centering 4 &
\centering 10 &
\centering 24 &
\centering 48 &
\centering 72 &\\ \hhline{*{5}{|-}|~|}
 & \cellcolor{black} &  & \cellcolor{green} &  & Optic tectum \\ \hhline{*{2}{|~}*{3}{|-}|~|}
 & \cellcolor{black} &  & \cellcolor{red} &  & Retina \\ \hhline{*{2}{|~}*{3}{|-}|~|}
 & \cellcolor{black} &  & \cellcolor{blue} &  & Myomeres \\ \hhline{*{5}{|-}|~|}
 & \cellcolor{blue} &  & \cellcolor{yellow} &  & Arches \\ \hhline{*{5}{|-}|~|}
\end{tabular}

\end{document}

3 Answers 3

13

You are specifying a final hhline fragment

\hhline{*{2}{|~}*{3}{|-}|~|}

You need

 \hhline{*{2}{|~}*{3}{|-}|~}

The "white line" is not a rule drawn over the black background it is just a lack of background, thus you need to fill it, the easiest way, as shown in the previous question is not to use ~ in the \hhline (which does exactly what you ask and does not draw any line at all) but instead use - but colour the rule using >{...} to match the cell colour.

9
  • 2
    Thank you for the effort, I'm glad you people are not only competent but also quick and friendly.
    – Dennis
    Commented Aug 2, 2012 at 15:30
  • 6
    Could you please explain a bit more on how to apply colour to \hhline. I don't quite understand >{}. Thank you in advance.
    – 71GA
    Commented Nov 18, 2013 at 21:38
  • @71GA There are lots of examples in the hhline documentation, texdoc hhline then search for Richard Commented Nov 18, 2013 at 21:44
  • 3
    I found this document ctan.ijs.si/tex-archive/macros/latex/required/tools/hhline.pdf but there is no description on how to use color. There is allso no Richard string inside. I don't think I understand your reply.
    – 71GA
    Commented Nov 18, 2013 at 22:15
  • 3
    @71GA sorry the coloured hhline examples are in colortbl (by the way you should have hhline documentation on your local system, not only on ctan) but anyway page 6 of texdoc.net/texmf-dist/doc/latex/colortbl/colortbl.pdf Commented Nov 18, 2013 at 22:18
4
  1. If the look of black cells should be \multirow-esque, then colouring the lines black would do it. For this, I used a - instead of ~ to draw the horizontal rules for the second column;
  2. Using | indicates a "vline which 'cuts' through a double (or single) hline" (according to the hhline documentation). Removing this from your \hhline specification at the end drops the "hline fragments."
  3. Since you're using \vline 1.5pt as your column divisions, you might as well use

    \setlength{\arrayrulewidth}{1.5pt}
    

    to have a consistent look for your table. Otherwise you will notice little indents in your vertical rules.

Here's a MWE showcasing the above:

enter image description here

\documentclass[11pt]{article}
%\usepackage{color}% http://ctan.org/pkg/color
%\usepackage{multirow}% http://ctan.org/pkg/multirow
\usepackage{colortbl}% http://ctan.org/pkg/colortbl
\usepackage{hhline}% http://ctan.org/pkg/hhline

\begin{document}
% Empty table
\setlength{\arrayrulewidth}{1pt}%
\begin{tabular}{*{5}{!{\vrule width 1.5pt}p{1.5cm}}!{\vrule width 1.5pt}l}
\centering 4 &
\centering 10 &
\centering 24 &
\centering 48 &
\centering 72 &\\ \hhline{*{5}{|-}|~|}
 &  &  &  &  & Optic tectum \\ \hhline{*{2}{|~}*{3}{|-}|~|}
 &  &  &  &  & Retina \\ \hhline{*{2}{|~}*{3}{|-}|~|}
 &  &  &  &  & Myomeres \\ \hhline{*{5}{|-}|~|}
 &  &  &  &  & Arches \\ \hhline{*{5}{|-}|~|}
\end{tabular}

\bigskip

% Partically filled table
\setlength{\arrayrulewidth}{1.5pt}%
\begin{tabular}{*{5}{|p{1.5cm}}|l}
\centering 4 &
\centering 10 &
\centering 24 &
\centering 48 &
\centering 72 &\\ \hhline{*{5}{|-}|~}
 & \cellcolor{black} &  & \cellcolor{green} &  & Optic tectum \\ \hhline{|~*{4}{|-}|~}
 & \cellcolor{black} &  & \cellcolor{red} &  & Retina \\ \hhline{|~*{4}{|-}|~}
 & \cellcolor{black} &  & \cellcolor{blue} &  & Myomeres \\ \hhline{*{5}{|-}|~}
 & \cellcolor{blue} &  & \cellcolor{yellow} &  & Arches \\ \hhline{*{5}{|-}|~}
\end{tabular}

\end{document}
1
  • @1. - I chose the color black only to contrast the white line. The cells will not be black in the final document, as I said, but thanks for the effort =)
    – Dennis
    Commented Aug 2, 2012 at 15:28
-1

You can easily construct that table with {NiceTabular} of nicematrix. The rules specified the command \Hline (of nicematrix) are not drawn in the blocks (constructed by the command \Block).

\documentclass[11pt]{article}
\usepackage{nicematrix}
\setlength{\arrayrulewidth}{1pt}

\begin{document}

\begin{NiceTabular}{*{5}{p{1.5cm}}}[colortbl-like,last-col,vlines,rules/width=1.5pt]
\centering 4 &
\centering 10 &
\centering 24 &
\centering 48 &
\centering 72 &\\ 
\Hline
\Block{*-1}{} 
 & \Block[fill=red!15]{3-1}{} 
    &  & \cellcolor{green} &  & Optic tectum \\ 
\Hline
 &  &  & \cellcolor{red} &  & Retina \\ 
\Hline
 &  &  & \cellcolor{blue} &  & Myomeres \\ 
\Hline
 & \cellcolor{blue} 
    &  & \cellcolor{yellow} &  & Arches \\ 
\Hline
\end{NiceTabular}

\end{document}

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

Output of the above code

You must log in to answer this question.

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