2

Summary: can I get a table with narrow columns (about 5 mm), a normally sized capital letter in some cells and some of the cells coloured (filled by a pattern would also work)?

Full question:

I'm trying to create a table with fairly narrow columns (5 mm), in which some cells are gray. Some of the cells (though never any gray cell, if that matters) will contain a single capital letter. In order to keep the column narrow and the letters in default font sice, I'm taking away the column separation by defining each column as @{} m{5mm} @{}. This works well; together with a small \vspace I can get the letters centered vertically and with \centering{} also horizontally.

Problem is, when I try to fill in a single cell using \cellcolor, the colour actually extends to the sides over the cell borders. The command \columncolor has an option for overhang, but as far as I can see, \cellcolor doesn't have this. Is there a way around it?

My code so far:

\documentclass{article}
\usepackage[table]{xcolor} 
\begin{document}

\begin{tabular}{| @{} m{5mm} @{} | @{} m{5mm} @{} | @{} m{5mm} @{} |@{} m{0pt} @{} }
  \hline
  & \vspace{.75mm}\centering{E} & \vspace{.75mm}\centering{C} &\\
  \hline 
  \vspace{0.75mm}
  \centering{P} & \cellcolor{gray!50} & &\\
  \hline 
  \vspace{0.75mm}
  \centering{M} & & &\\
  \hline 
\end{tabular}

\end{document}

Notes on the example:

1) The 0 mm column to the far right is there since the rightmost column, for some reason, cannot contain a \centering. If there's a better solution to this, I'm all ears.

2) The \vspace{0.75mm} is only in order to make the cells square (or very close to square).

Possible workaround: I have thought about instead of using \cellcolor, to insert a grey picture, stretched to the right dimensions, in each cell. This works except for creating a small white gap under the picture, before the \hline. I can move up the line by using \[-1.3 mm], but if I do that on the last line the vertical lines between each column protrudes under the bottom. Either solution would be OK, I have long ago given up the thought of elegant code...

2
  • Don't use \centering all over the place, instead place >{\centering\arraybackslash}m{5mm} in your preamble instead of m{5mm} for those columns. The colouring does stick out because the cell colours do expect the padding you suppress. There should be enough other questions on here dealing with that (I won't search for those now, as I have to leave).
    – Skillmon
    Dec 26, 2018 at 19:37
  • Thanks for the \centering tip, I'll try that. I have searched quite a lot, and read those questions about \cellcolor (and similiar) that I can find. I appreciate your concern, but this was not a case of "too lazy to google, I'll ask instead". Obviously, though, if there is an answer that I have missed, I'm grateful for help in finding it.
    – Wilhelm
    Dec 26, 2018 at 19:44

2 Answers 2

3

Here is a solution, and some simplification to your code. But do you really need to have a 0pt values for \tabcolsep?

\documentclass{article}
\usepackage[table]{xcolor}

\begin{document}

{\setlength{\tabcolsep}{0pt}%
\begin{tabular}{| >{\centering}m{5mm} | >{\centering} m{5mm} | >{\centering\arraybackslash}m{5mm} |}
  \hline
  & \vspace{.75mm}E & \vspace{.75mm} C \\
  \hline
  \vspace{0.75mm}
   P & \cellcolor{gray!50} & \\
  \hline
  \vspace{0.75mm} M & & \\
  \hline
\end{tabular}}% 

\end{document}

enter image description here

2
  • Thank you, that worked a charm! The \tabcolsep doesn't need to be strictly 0, only close to it -- I don't want the columns wider, and the letter takes almost all that width.
    – Wilhelm
    Dec 26, 2018 at 20:15
  • 1
    @Wilhelm you can \makebox[0pt][c]{...} the letters, then they will not take up any horizontal space as far as LaTeX's horizontal alignment is concerned.
    – Skillmon
    Dec 26, 2018 at 20:59
0

With {NiceTabular} of nicematrix which has its own system for coloring the tabulars, you have directly the expected output.

\documentclass{article}
\usepackage{nicematrix}
\begin{document}

\begin{NiceTabular}{| @{} m{5mm} @{} | @{} m{5mm} @{} | @{} m{5mm} @{} |@{} m{0pt} @{} }[colortbl-like]
  \hline
  & \vspace{.75mm}\centering E & \vspace{.75mm}\centering C &\\
  \hline 
  \vspace{0.75mm}
  \centering P & \cellcolor{gray!50} & &\\
  \hline 
  \vspace{0.75mm}
  \centering M & & &\\
  \hline 
\end{NiceTabular}

\end{document}

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

Output of the above code

You must log in to answer this question.

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