4

I want to create a tabular with an empty top left cell and a background color on the column and row headers (except the empty top left).

I try to combine this two answers:
color only a cell of a table
Remove border of top left table cell

But the background color seams to be drawn on top of the upper rule when using \cline. This does not happen with \hline.

How do I draw \cline on top of the background color?

$ lualatex --version
This is LuaTeX, Version beta-0.80.0 (TeX Live 2015/Debian) (rev 5238)

MCVE:

\documentclass[24pt a6paper, landscape]{article}
\usepackage[utf8]{inputenc}
\usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor
\usepackage[paperheight=3.7in,paperwidth=6.2in, margin=0.5cm]{geometry}
\begin{document}

\newcommand{\sometext}{this is just to demonstrate line breaking}   
\definecolor{headercolor}{RGB}{209,220,204}

\newcommand{\cellDefinition}[1]{\parbox{3cm}{\vspace{3mm}#1\vspace{3mm}}}

This table has a line at the top of the gray cell (1-3) because of \verb|\hrule| :

\begin{tabular}{c|c|c|c|}\hline
 \multicolumn{1}{c|}{} &    \cellDefinition{\sometext} & \cellcolor{headercolor}    \cellDefinition{\sometext} &    \cellDefinition{\sometext} \\ \hline
    \cellDefinition{\sometext} &    \cellDefinition{\sometext}  &   \cellDefinition{\sometext}  &   \cellDefinition{\sometext} \\ \hline
\end{tabular} 
\vspace{1em}

How do I draw the same (evenly thick) line on top of the gray cell with \verb|\crule| ?

\begin{tabular}{c|c|c|c|}\cline{2-4}\cline{2-4}
    \multicolumn{1}{c|}{} & \cellDefinition{\sometext} & \cellcolor{headercolor}    \cellDefinition{how do I bring the line at top before the background? } &   \cellDefinition{\sometext} \\\hline
    \cellDefinition{\sometext}  & \cellcolor{headercolor}       \cellDefinition{I want such top line at the other cell too.} &  \cellDefinition{\sometext} &    \cellDefinition{\sometext} \\ \hline
\end{tabular} 


\end{document}

result

enter image description here

1
  • this is explicitly documented in the colortbl documentation. Commented Jan 3, 2017 at 18:50

2 Answers 2

6

You have to \usepackage{hhline} and have to replace \cline{2-4} by \hhline{~|-|-|-|}. The argument of \hhline is a specification of what to draw: ~ corresponds to an empty column, - to a line spanning one column, and | signals a junction with a vertical line.

enter image description here

\documentclass[24pt a6paper, landscape]{article}
\usepackage[utf8]{inputenc}
\usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor
\usepackage[paperheight=3.7in,paperwidth=6.2in, margin=0.5cm]{geometry}
\usepackage{hhline}
\begin{document}

\newcommand{\sometext}{this is just to demonstrate line breaking}   
\definecolor{headercolor}{RGB}{209,220,204}

\newcommand{\cellDefinition}[1]{\parbox{3cm}{\vspace{3mm}#1\vspace{3mm}}}

This table has a line at the top of the gray cell (1-3) because of \verb|\hrule| :

\begin{tabular}{c|c|c|c|}\hline
 \multicolumn{1}{c|}{} &    \cellDefinition{\sometext} & \cellcolor{headercolor}    \cellDefinition{\sometext} &    \cellDefinition{\sometext} \\ \hline
    \cellDefinition{\sometext} &    \cellDefinition{\sometext}  &   \cellDefinition{\sometext}  &   \cellDefinition{\sometext} \\ \hline
\end{tabular} 
\vspace{1em}

How do I draw the same (evenly thick) line on top of the gray cell with \verb|\crule| ?

\begin{tabular}{c|c|c|c|}
    \hhline{~|-|-|-|}
    \multicolumn{1}{c|}{} & \cellDefinition{\sometext} & \cellcolor{headercolor}    \cellDefinition{how do I bring the line at top before the background? } &   \cellDefinition{\sometext} \\\hline
    \cellDefinition{\sometext}  & \cellcolor{headercolor}       \cellDefinition{I want such top line at the other cell too.} &  \cellDefinition{\sometext} &    \cellDefinition{\sometext} \\ \hline
\end{tabular} 


\end{document}
1

For historical reasons, the command \cline draws a rule which protrude in the cell which is below (and \hline does not). In {NiceTabular} of nicematrix, that behaviour is corrected.

So, by using {NiceTabular} instead of {tabular}, you have directly the expected output.

\documentclass[24pt a6paper, landscape]{article}
\usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor
\usepackage[paperheight=3.7in,paperwidth=6.2in, margin=0.5cm]{geometry}
\usepackage{nicematrix}
\begin{document}

\newcommand{\sometext}{this is just to demonstrate line breaking}   
\definecolor{headercolor}{RGB}{209,220,204}

\newcommand{\cellDefinition}[1]{\parbox{3cm}{\vspace{3mm}#1\vspace{3mm}}}

How do I draw the same (evenly thick) line on top of the gray cell with \verb|\crule| ?

\begin{NiceTabular}{c|c|c|c|}
\cline{2-4}
    \multicolumn{1}{c|}{} & \cellDefinition{\sometext} & \cellcolor{headercolor}    \cellDefinition{how do I bring the line at top before the background? } &   \cellDefinition{\sometext} \\\hline
    \cellDefinition{\sometext}  & \cellcolor{headercolor}       \cellDefinition{I want such top line at the other cell too.} &  \cellDefinition{\sometext} &    \cellDefinition{\sometext} \\ \hline
\end{NiceTabular} 

\end{document}

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

Output of the first code

However, although mathematically perfect, the PDF produced by using the tools of colortbl is sometimes difficult to render by the PDF viewers: at some levels of zoom, some rules seems to vanish (even in Adobe Reader).

That's why nicematrix provides tools to solve those problems. With the key colortbl-like (and without loading colortbl), the PDF will be perfect in all PDF viewers at all levels of zoom.

\documentclass[24pt a6paper, landscape]{article}
\usepackage{xcolor}
\usepackage[paperheight=3.7in,paperwidth=6.2in, margin=0.5cm]{geometry}
\usepackage{nicematrix}
\begin{document}

\newcommand{\sometext}{this is just to demonstrate line breaking}   
\definecolor{headercolor}{RGB}{209,220,204}

\newcommand{\cellDefinition}[1]{\parbox{3cm}{\vspace{3mm}#1\vspace{3mm}}}

How do I draw the same (evenly thick) line on top of the gray cell with \verb|\crule| ?

\begin{NiceTabular}{c|c|c|c|}[colortbl-like]
\cline{2-4}
    \multicolumn{1}{c|}{} & \cellDefinition{\sometext} & \cellcolor{headercolor}    \cellDefinition{how do I bring the line at top before the background? } &   \cellDefinition{\sometext} \\\hline
    \cellDefinition{\sometext}  & \cellcolor{headercolor}       \cellDefinition{I want such top line at the other cell too.} &  \cellDefinition{\sometext} &    \cellDefinition{\sometext} \\ \hline
\end{NiceTabular} 

\end{document}

Output of the second code

You must log in to answer this question.

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