2

If you colourise a row in a table, @{} does not remove the colour from the side bearings, as demonstrated in below figure.

enter image description here

To remove the colour, you may direct a kern into the table column using >{\kern-\tabcolsep}, as demonstrated in below figure:

enter image description here

However, this does not work if the column you want to kern is a p-column. Instead, the cell content no longer lines up with the next cell, as demonstrated below:

enter image description here

Why? Is it possible to use \kern in some way (I know setting \tabcolsep to zero may have the same effect, but doing that has some side effects)?

MWE:

\documentclass[table]{article}
\usepackage{lmodern, microtype, xcolor}

\begin{document}

\begin{tabular}{@{}lcr@{}}

\rowcolor{lightgray}test&test&test\\\hline
test&test&test\\
\rowcolor{lightgray}test&test&test\\

\end{tabular}
\vspace{1cm}

\begin{tabular}{>{\kern-\tabcolsep}lcr<{\kern-\tabcolsep}}

\rowcolor{lightgray}test&test&test\\\hline
test&test&test\\
\rowcolor{lightgray}test&test&test\\
\end{tabular}

\vspace{1cm}

\begin{tabular}{>{\kern-\tabcolsep}p{1cm}cr<{\kern-\tabcolsep}}

\rowcolor{lightgray}test&test&test\\\hline
test&test&test\\
\rowcolor{lightgray}test&test&test\\
\end{tabular}

\end{document}
Sveinung
  • 20,355
  • 1
    See addendum in answer https://tex.stackexchange.com/questions/365685/row-colour-gaps-in-tabularx-with-aboverulesep-and if it ca help you. – Zarko Aug 18 '19 at 08:19
  • Using columncolor with the left and right overhang options mght also be interesting. – leandriis Aug 18 '19 at 08:24
  • @leandriis You may easily(?) circumvent this effect by setting tabcolsep to zero and use narrow, empty columns to set space between the columns. However, I wonder if it is possible to use the \kern trick in some way. – Sveinung Aug 18 '19 at 09:05
  • @Zarko I know it is possible to circumvent the problem by using tabcolsep. But if you have 15 columns with content, you need additional 14 narrow columns to simulate tabcolsep. – Sveinung Aug 18 '19 at 09:21
  • 1
    You added a vertical kern not a horizontal one – David Carlisle Aug 18 '19 at 09:41
  • 1
    Yes or use the latex syntax \hspace" – David Carlisle Aug 18 '19 at 13:37
  • @DavidCarlisle Thank you for this clarification. – Sveinung Aug 18 '19 at 13:44
  • @Sveinung: No, not really, what I actually referred to is the following solution/trick: https://tex.stackexchange.com/a/365742/134144 where ksgj1 used >{\columncolor{white}[0pt][\tabcolsep]} before the first and >{\columncolor{white}[\tabcolsep][0pt]} before the last column and later on colored the row with the usual \rowcolor command. – leandriis Aug 18 '19 at 15:28

1 Answers1

3

For fixed width columns, you may add \leavevmode or use \hskip:

\documentclass[table]{article}
\usepackage{lmodern, microtype, xcolor}

\begin{document}

\begin{tabular}{@{}lcr@{}}

\rowcolor{lightgray}test&test&test\\\hline
test&test&test\\
\rowcolor{lightgray}test&test&test\\

\end{tabular}
\vspace{1cm}

\begin{tabular}{>{\kern-\tabcolsep}lcr<{\kern-\tabcolsep}}

\rowcolor{lightgray}test&test&test\\\hline
test&test&test\\
\rowcolor{lightgray}test&test&test\\
\end{tabular}

\vspace{1cm}

\begin{tabular}{>{\leavevmode\kern-\tabcolsep}p{1cm}cr<{\kern-\tabcolsep}}

\rowcolor{lightgray}test&test&test\\\hline
test&test&test\\
\rowcolor{lightgray}test&test&test\\
\end{tabular}

\vspace{1cm}

\begin{tabular}{>{\hskip-\tabcolsep}p{1cm}cr<{\kern-\tabcolsep}}

\rowcolor{lightgray}test&test&test\\\hline
test&test&test\\
\rowcolor{lightgray}test&test&test\\
\end{tabular}

\end{document} 

enter image description here

Added: In case you have a fixed width column, if a cell has several lines, only the first line has a negative kerning. A workaround consist in including the cell contents in a \parbox[t]{\hsize} (the optional[t] was suggested by @Sveinung – thanks!). This works also for the X column type if you use a tabularx environment.

For the L, R, C, J column types from tabulary, it seems you also have to add \leavevmode (or use \hskip) for the last column, even if it is not a fixed width column.

Sveinung
  • 20,355
Bernard
  • 271,350
  • Give a small moment to test. – Bernard Aug 18 '19 at 10:16
  • It works width tabulary and tabularx. I realised that this was easy to test for myself, so I deleted my comment. – Sveinung Aug 18 '19 at 10:25
  • Unfortunately, \leavevmode only kerns the first line if the cell content occupies more than one line. The same for \hspace{0pt}! – Sveinung Aug 18 '19 at 10:44
  • It doesn't work so well indeed: if the cell is multilined, you have to put it in a \parbox{\hsize}. As to tabulary, the right-hand column, even if it has not a fixed width, also requires \leavevmode or \hskip (not perfect, though). – Bernard Aug 18 '19 at 10:47
  • The \parbox{\hsize}{<cell content>} did the trick, but I had to add the optional alignment command ([t]in my case), because\parbox` influences spacing in the next column (a top aligned parbox with list environment and no spacing at top) – Sveinung Aug 18 '19 at 11:06
  • Please, add these tips to you answer. I will wait a day before accepting, to see if other solutions materialise. – Sveinung Aug 18 '19 at 11:11
  • I've added some explanations Do you think it's clear enough? – Bernard Aug 18 '19 at 12:18
  • Thank you Bernard! David Carlisle confirmed that \leavevmode or \hspace is the correct remedy, so I have accepted your answer. – Sveinung Aug 18 '19 at 13:43