10

I have a table where I am filling some of the cells with a gray color. It works fine. The problems is however, that the horizontal lines of the table becomes gray too. How can I solve this so that the lines stays as Black lines and the cell color is gray?

Image of my problem: enter image description here

A complete code example can be found online here or in this code:

\documentclass{article}
\usepackage{slashbox,pict2e}
\usepackage{color, colortbl}
\usepackage{amssymb}

\definecolor{Gray}{gray}{0.9}

\begin{document}

\begin{table}
\centering
\begin{tabular}{|l|c|c|c|}
    \hline\backslashbox{Enhed}{Interface} & ED    & DDG   & \textit{Server} \\
    \hline  ED      & \checkmark        & \checkmark        & {\cellcolor{Gray}} \\
    \hline DDG     & \checkmark        & {\cellcolor{Gray}}  & \checkmark \\
    \hline \textit{Server}  & {\cellcolor{Gray}}  & \checkmark        & {\cellcolor{Gray}}\\\hline
\end{tabular}
\caption{Oversigt over interfaces mellem enhederne.}
\label{tab:interfacesMellemEnheder}
\end{table}
\end{document}
7heViking
  • 1,183

4 Answers4

5

As you can read in the comments there never was a problem with the tex code. The only problem was the pdf viewer. When zooming in the lines gets visible again. This will not be a problem when printing the page.

7heViking
  • 1,183
  • 10
    I currently have this exact issue and both Google Chrome and Adobe Acrobat display my PDF incorrectly. You can say "not a problem with tex code, only problem with pdf viewers", but if practically all viewers of my PDF are going to see it incorrectly, it does become my problem. – bkoodaa Sep 22 '19 at 21:28
2

The environment {NiceTabular} of nicematrix (≥ 4.0 2020-05-08) gives a solution to that problem.

With colortbl, a \hline is drawn and, if there is a colored cell in the following row, the colored rectangle is drawn after the horizontal rule. Even if the position of the rule and of the colored rectangle are mathematically exact, almost all PDF viewers give priority to the colored rectangle (explicitly drawn after in the PDF): they consider that, at a low zoom level, the rules drawn before are only detail less important (that's why it's difficult to say that this is a 'issue' of the viewer).

With nicematrix, this problem is solved because all the rules are drawn after the colored rectangles (rows, columns, cells, etc.).

Moreover, nicematrix provides a command \diagbox and a key hvlines to draw all the rules.

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

\begin{document}

\begin{table} \centering \begin{NiceTabular}{wl{2.2cm}ccc}[hvlines] \CodeBefore \cellcolor{lightgray}{2-4,3-3,4-2,4-4} \Body \rule[-3mm]{0pt}{9mm} \diagbox{Enhed}{Interface} & ED & DDG & \textit{Server} \ ED & \checkmark & \checkmark & \ DDG & \checkmark & & \checkmark \ \textit{Server} & & \checkmark & \ \end{NiceTabular} \caption{Oversigt over interfaces mellem enhederne.} \label{tab:interfacesMellemEnheder} \end{table} \end{document}

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

Result of the above code

F. Pantigny
  • 40,250
  • The same issue persists! At a certain zooming level, the separating line is not visible. – Mohamed Abdelhamid Apr 05 '23 at 14:39
  • @MohamedAbdelhamid : It's difficult to answer without more information. If you want, send me a mail (you will find my address on the documentation of nicematrix) the MWE, the PDF viewer, the level of zoom, etc. and I will try to provide a precise answer. – F. Pantigny Apr 05 '23 at 16:40
1

Sorry, this is too long for a comment. As requested by FireFly3000, an example with booktabs with only a few rules:

MWE

In this type of table diagbox have no utility, so you can put "Interface" in a multicolumn in a previous row, and left only "Enhed" in the cell, or if you prefer this label vertically centered, use the multirow package:

\documentclass{article}
\usepackage{colortbl, booktabs}
\usepackage{multirow}
\usepackage{amssymb}
\definecolor{Gray}{gray}{0.9}
\begin{document}
\begin{table}
\centering
\begin{tabular}{lccc}
\toprule
\multirow{2}{*}{Enhed} & \multicolumn{3}{c}{Interface} \\
\cmidrule{2-4}
            & ED                &  DDG             & \textit{Server} \\
\midrule
ED               & \checkmark        & \checkmark       & \cellcolor{Gray} \\
DDG              & \checkmark        & \cellcolor{Gray} & \checkmark       \\
\textit{Server}  & \cellcolor{Gray}  & \checkmark       & \cellcolor{Gray} \\
\bottomrule
\end{tabular}

\caption{Oversigt over interfaces mellem enhederne.}
\label{tab:interfacesMellemEnheder}
\end{table}
\end{document}
Fran
  • 80,769
1

A simple solution is just to increase the table linewidth. The PDF viewer will then no longer have the issue.

\setlength\arrayrulewidth{1pt} %Increasing linewidth \begin{table} (...) \end{table} \setlength\arrayrulewidth{0.4pt} %Back to default

Einar U
  • 11
  • Welcome to TeX.SE! This is an issue with the pdf viewer, changing linewidth is not needed ... – Mensch Jun 10 '20 at 08:30
  • 2
    Latex generated documents is supposed to Interface with PDF-Viewers, so I do not get this argument. If it is the norm for PDF-Viewers is to display Tables in this manner, then I would argue that it effectively is a Latex-issue. And I provided one simple way to handle it. See discussion of "F. Pantigny", which it seems provided a nice solution to the problem without need of increasing the linewidth. – Einar U Jun 10 '20 at 11:59
  • @Einar U: I agree with you and think that the problem can't be considered only as a 'issue of the viewer'. – F. Pantigny Jun 10 '20 at 16:23
  • It's certainly not just an "issue of the viewer", because I've printed the document and the lines were not there either. Also, PDFs are thought to be both printed and read on screen, so what's the point if they do not show correctly on screen? – José Tomás Tocino Jul 01 '21 at 21:54