5

I have a latex table as follows

\documentclass[12pt]{report}

\usepackage{colortbl}

\begin{document}

\begin{table}
\centering
\begin{tabular}{|@{\,}c@{\,}|@{\,}c@{\,}|@{\,}c@{\,}|@{\,}c@{\,}|}
\hline
Column1 & Column2 & Column3 & Column4 \\
\hline
A & \cellcolor[gray]{0.5} & D & E \\
\hline
 &  &  & F \\
\hline
 &  &  & \\
\hline
\end{tabular}
\end{table}

\end{document}

The cell color is overlaping to the nearby columns. It may be because of @{\,} I used with tabular to remove text padding in columns(It is required since my actual table has more width). Anyone know how prevent the cell shading from overlapping to other cells in this case?

2
  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}.
    – user31729
    Commented Sep 14, 2014 at 10:13
  • Hi, I have added a working example and refined my code a bit.
    – Josin
    Commented Sep 14, 2014 at 10:36

2 Answers 2

5

colortbl adds a padding \tabcolsep wide; instead of using @{\,} you should reduce \tabcolsep:

\documentclass[12pt]{report}

\usepackage{colortbl}

\begin{document}

\begin{table}
\centering
\setlength{\tabcolsep}{0.3em}
\begin{tabular}{|c|c|c|c|}
\hline
Column1 & Column2 & Column3 & Column4 \\
\hline
A & \cellcolor[gray]{0.5} & D & E \\
\hline
 &  &  & F \\
\hline
 &  &  & \\
\hline
\end{tabular}
\end{table}

\end{document}

enter image description here

0
0

For information, with {NiceTabular} of nicematrix (with the key color-inside --- alias: colortbl-like), you have directly the expected ouptut with your code.

However, as said by egreg, the good solution to your problem is to change \tabcolsep.

\documentclass[12pt]{report}
\usepackage{nicematrix}

\begin{document}

\begin{table}
\centering
\begin{NiceTabular}{|@{\,}c@{\,}|@{\,}c@{\,}|@{\,}c@{\,}|@{\,}c@{\,}|}[color-inside]
\hline
Column1 & Column2 & Column3 & Column4 \\
\hline
A & \cellcolor[gray]{0.5} & D & E \\
\hline
 &  &  & F \\
\hline
 &  &  & \\
\hline
\end{NiceTabular}
\end{table}

\end{document}

You need several compilations.

Output of the above code

You must log in to answer this question.

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