7

Considering the following code (MWE) :

\documentclass{article}
\usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor
\begin{document}
\begin{tabular}{|l|c|r|}
  \hline
  Some & \cellcolor{blue!25}coloured & contents \\
  \hline
  \multicolumn{2}{|c|}{\cellcolor{blue!25} multicolumn coloured} & \cellcolor{blue!6} other color \\ \hline
  Some & \cellcolor{blue!15}coloured & contents \\
  \hline
\end{tabular}
\end{document}

I am having issues with tabular lines displaying, as you can notice on the following pictures.


With Evince (LINUX) :

Linux displays

No problem so far.


With Adobe (WINDOWS) :

windows display

As you can see, lines are not displayed properly.

How can I fix this problem ? I wish my document would be displayable whatever the operating system.


NOTES :

  • I tried to follow this post proposal link, but unfortunately, it didn't fixed the problem.
  • I compile on Linux with pdflatex, version 3.1415926-2.5-1.40.14 (TeX Live 2013/Debian)
  • That's the same issue with no multicolumn as you can see on below : with no multicolumn, windows
enigmator
  • 479

2 Answers2

4

A quite crude workaround is to set the table twice, the second time without background colors. Also the rule thickness can be increased by setting \arrayrulewidth:

\documentclass{article}
\usepackage{array}
\usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor
\begin{document}

\setlength{\arrayrulewidth}{1.5\arrayrulewidth}% 50% thicker
\def\tmp{%
  \begin{tabular}{|l|c|r|}
    \hline
    Some & \cellcolor{blue!25}coloured & contents \\
    \hline
    \multicolumn{2}{|c|}{\cellcolor{blue!25}multicolumn coloured} &
    \cellcolor{blue!6}other color \\ \hline
    Some & \cellcolor{blue!15}coloured & contents \\
    \hline
  \end{tabular}%
}
\leavevmode
\rlap{\tmp}%
\begingroup
  \renewcommand*{\cellcolor}[1]{}%
  \tmp
\endgroup
\end{document}

Result

Remarks:

  • LaTeX removes spaces at the begin and end of a cell. But the space after \cellcolor{...} is inside the cell and is not automatically removed.

Automation

The procedure can be automated to some degree. The following example at least supports column types l, c, r, p, m, b. Internals are redefined to catch the cell contents in boxes and to replace them with empty boxes of the same dimensions.

Unsupported are text inside @{...}, longtable, ...

\documentclass{article}
\usepackage{array}
\usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor

\makeatletter
\let\org@insert@column\insert@column
\newcommand*{\dummy@insert@column@h}{%
  \begingroup
    \setbox0=\hbox\bgroup\begingroup
      \org@insert@column
    \endgroup\egroup
    \setbox2=\hbox{}%
    \wd2=\wd0 %
    \ht2=\ht0 %
    \dp2=\dp0 %
    \copy2 %
  \endgroup
}
\newcommand*{\dummy@endpbox}{%
  \@finalstrut\@arstrutbox
  \egroup
  \begingroup
    \setbox0=\lastbox
    \setbox2=\hbox{}%
    \wd2=\wd0 %
    \ht2=\ht0 %
    \dp2=\dp0 %
    \copy2 %
  \endgroup
  \hfil
}
\newcommand*{\dummy@classz}{%
  \@classx
  \@tempcnta \count@
  \prepnext@tok
  \@addtopreamble{%
    \ifcase \@chnum
      \hfil \d@llarbegin
      \dummy@insert@column@h
      \d@llarend \hfil
    \or
      \hskip1sp\d@llarbegin
      \dummy@insert@column@h
      \d@llarend \hfil
    \or
      \hfil\hskip1sp\d@llarbegin
      \dummy@insert@column@h
      \d@llarend
    \or
      $\vcenter
      \@startpbox{\@nextchar}\insert@column \dummy@endpbox
      $%
    \or
      \vtop
      \@startpbox{\@nextchar}\insert@column \dummy@endpbox
    \or
      \vbox
      \@startpbox{\@nextchar}\insert@column \dummy@endpbox
    \fi
  }%
  \prepnext@tok
}
\newcommand*{\tabulardummysetup}{%
  \renewcommand*{\cellcolor}[1]{\null}%
  \let\@classz\dummy@classz
}
\newcommand{\tabularfix}[1]{%
  \def\tabularfix@contents{\ignorespaces#1\ifhmode\unskip\fi}%
  \leavevmode
  \rlap{\tabularfix@contents}%
  \begingroup
    \tabulardummysetup
    \tabularfix@contents
  \endgroup
}
\makeatother

\setlength{\arrayrulewidth}{1.5\arrayrulewidth}

\begin{document}

\tabularfix{%
  \begin{tabular}[t]{|l|c|r|}
    \hline
    Some & \cellcolor{blue!25}coloured & contents \\
    \hline
    \multicolumn{2}{|c|}{\cellcolor{blue!25}multicolumn coloured} &
    \cellcolor{blue!6}other color \\ \hline
    Some & \cellcolor{blue!15}coloured & \multicolumn{1}{p{20mm}|}{contents} \\
    \hline
  \end{tabular}%
}

\bigskip

\begingroup
  \tabulardummysetup
  \begin{tabular}{|l|c|r|}
    \hline
    Some & \cellcolor{blue!25}coloured & contents \\
    \hline
    \multicolumn{2}{|@{\kern\tabcolsep}c|}{\cellcolor{blue!25}multicolumn coloured} &
    \cellcolor{blue!6}other color \\ \hline
    Some & \cellcolor{blue!15}coloured & \multicolumn{1}{p{20mm}|}{contents} \\
    \hline
  \end{tabular}%
\endgroup

\end{document}

Result

Heiko Oberdiek
  • 271,626
  • Indeed, it seems to fix the bad displays. Two questions now: 1) is it possible to only double all except the text in the tabular (now it seems weird on evince). 2) Is there a way to automate this process into my file (I actually have several tables in that way). – enigmator Apr 25 '14 at 08:19
1

The package nicematrix has tools designed to address that problem. With the environment {NiceTabular} of that package, the rules won't disappear because the colored panels are drawn before the rules (with informations written previously on the aux file: that's why you need several compilations).

\documentclass{article}
\usepackage{nicematrix}
\begin{document}
\begin{NiceTabular}{lcr}[colortbl-like,hvlines]
  Some & \cellcolor{blue!25}coloured & contents \\
  \Block[fill=blue!25]{1-2}{multicolumn} & & \cellcolor{blue!6}other color \\ 
  Some & \cellcolor{blue!15}coloured & contents \\
\end{NiceTabular}
\end{document}

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

Output of the above code

If you prefer, it's also possible to specify the formatting instructions outside the main array (the output is the same).

\documentclass{article}
\usepackage{nicematrix}
\begin{document}
\begin{NiceTabular}{lcr}[hvlines]
\CodeBefore
\cellcolor{blue!25}{1-2,2-1,2-2,3-2}
\cellcolor{blue!6}{2-3}
\Body
  Some        & coloured & contents \\
  \Block{1-2}{multicolumn} & & other color \\ 
  Some        & coloured & contents \\
\end{NiceTabular}
\end{document}
F. Pantigny
  • 40,250