3

I have a tabular that has altering background color (every second row is colored). Some cells use a separator character instead of a vertical line. The following code tries to implement this first using !{} for the separator character and then @{}:

\documentclass{article}

\usepackage{array}
\usepackage[table]{xcolor}

\newcommand{\dotsep}{\footnotesize\raisebox{0.2pt}{\bullet}}

\newcommand{\row}[6][]{
  #1#2 & \centering #1#3 & \centering #1#4 & \centering #1#5 & #1#6
}
\newcommand{\testContent}{
  \row{A}{B}{C}{D}{E} \\ \hline
  \row[\cellcolor{gray!30}]{A}{B}{C}{D}{E} \\ \hline
  \row{A}{B}{C}{D}{E} \\ \hline
  \row[\cellcolor{gray!30}]{A}{B}{C}{D}{E}
}

\begin{document}
  \begin{tabular}{p{1cm}|p{0.5cm}!{\dotsep}p{0.5cm}!{\dotsep}p{0.5cm}|p{1cm}}
    \testContent
  \end{tabular}

  \begin{tabular}{p{1cm}|p{0.5cm}@{\dotsep}p{0.5cm}@{\dotsep}p{0.5cm}|p{1cm}}
    \testContent
  \end{tabular}
\end{document}

This is the result:

As you can see, if I use !{} I do get the separator characters but they don't get the background color (as they are between the cells). If I use @{} instead, they vanish if I set the background color.

I did use the tabu package with \rowcolor in the past but it seems to be unmaintained and causes other problems.

How can I color the whole row including the separator characters while keeping them visible?

2 Answers 2

2

With {NiceTabular} of nicematrix.

\documentclass{article}

\usepackage{nicematrix}
\usepackage{xcolor}

\newcommand{\dotsep}{\footnotesize\raisebox{0.2pt}{\textbullet}}

\newcommand{\row}[6][]{
  #1#2 & \centering #1#3 & \centering #1#4 & \centering #1#5 & #1#6
}

\begin{document}
  \begin{NiceTabular}{p{1cm}|p{0.5cm}!{\dotsep}p{0.5cm}!{\dotsep}p{0.5cm}|p{1cm}}[colortbl-like]
  \row{A}{B}{C}{D}{E} \\ \hline
  \row[\cellcolor{gray!30}]{A}{B}{C}{D}{E} \\ \hline
  \row{A}{B}{C}{D}{E} \\ \hline
  \row[\cellcolor{gray!30}]{A}{B}{C}{D}{E}
  \end{NiceTabular}
\end{document} 

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

Output of the above code

3
  • This works, but does make the document pretty unstable. I need 3-4 compilations to get the desired output which is a problem since I do want to create it as part of a script. Is there any way to check whether the table is finished or needs another compilation?
    – flyx
    May 10, 2021 at 21:44
  • You can use latexmk. It will do the right number of compilations. May 11, 2021 at 10:44
  • 1
    After some testing this works fine indeed. I integrated the other answer as well by doing \CodeBefore\rowcolors{1}{}{gray!30}\Body inside the NiceTabular instead of setting the color for each cell.
    – flyx
    May 11, 2021 at 13:31
2

Another possibility: adding supplementary c columns for the bullets and a modified \dotsep command:

\documentclass{article}

\usepackage{array}
\usepackage[table]{xcolor}
\newcommand{\dotsep}{\makebox[2\tabcolsep]{\footnotesize\raisebox{0.2pt}{\textbullet}}}

\begin{document}

\rowcolors{1}{}{gray!30}
  \begin{tabular}{p{1cm}|p{0.5cm}@{\!}>{\dotsep}c@{\,}p{0.5cm} @{\!}>{\dotsep}c@{\,}p{0.5cm}@{\!}|p{1cm}}
    A & B && C && D & E \\ \hline
    A & B && C && D & E \\ \hline
    A & B && C && D & E \\ \hline
  A & B && C && D & E
  \end{tabular}

\end{document} 

enter image description here

5
  • Certainly good to know about \rowcolors which I seem to have overlooked. This approach does not center the bullets though – in your screenshot you can see that D has more space between bullet and vertical line than B has (better visible with smaller \tabcolsep). I am unsure why that happens.
    – flyx
    May 11, 2021 at 12:01
  • @flyx: This is due to the fact that in a p column, the contents is aligned in paragraph mode, so thet , since there's just a single letter in the columns, there remains some white space. When I try centering these columns, the letters are not entirely drawn in rows with a background. (for a reason I don't understand).
    – Bernard
    May 11, 2021 at 12:27
  • I am talking about the distance between the bullet and the vertical bar. Since all p columns are defined to have equal lengths, this distance should be the same for the B and D column, but it is not (measured on-screen: 61 pixels from | to • at B, 70 pixels from • to | at D)
    – flyx
    May 11, 2021 at 12:35
  • @flyx: I've posted a small modification in the table preamble(replaces the last | with @{\!}), which makes it more like you want, albeit not perfect: the difference is 1 pt, which is not really visible for normal contents.
    – Bernard
    May 11, 2021 at 12:59
  • Thanks for looking into it. This certainly helps but I guess I will go with the NiceTabular approach since that looks less like a workaround. Another strange thing I saw when toying around with your approach: If you add \cellcolor{green} to the \dotsep, you can see how the following cell slightly paints into the separator area in colored rows. This causes cuts from the bullet for small \tabcolseps.
    – flyx
    May 11, 2021 at 13:16

You must log in to answer this question.

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