I'd like to fill the cells of a table with a background color. How can this be done?
-
1For examples of how to do this, see color only a cell of a table and/or Color merged and regular cells in a table individually.– Werner ♦Jan 11, 2014 at 3:46
2 Answers
The xcolor provides support for coloring table rows in a variety of patterns. The colortbl can color rows, columns, cells and rules.
There is a nice article by Lapo Filippo Mori in the PracTeX Journal that describes the usage of both packages. Scroll down to section 4.
-
2xcolor: ctan.org/tex-archive/macros/latex/contrib/xcolor colortbl:tug.ctan.org/tex-archive/macros/latex/contrib/colortbl (fixing links above)– fliesMay 13, 2011 at 19:03
-
1@Sharpie Can the the xcolor package be used for completely coloring the table in one specific color in one go? I am trying to replicate the look of this table for my report. I read up on the colortbl package but it has a pretty limited palette. Do you have any pointers on how to write minimal code for that table using xcolor? Most tips involving xcolor are about alternating colored rows.– ArielDec 26, 2011 at 16:16
-
4This answer could be improved if instead of just linking, you could quote the relevant parts to make this a complete answer. Links tend to get stale over time.– arekolekAug 17, 2016 at 20:24
The package nicematrix
provides tools to color background of cells in tabulars. With those tools, you will have a good output in the PDF viewers: the rules won't seem to disappear in those PDF viewers at some levels of zoom (eg Adobe Reader) and you won't see the thin white lines you have in some PDF viewers (eg Sumatra PDF).
First, there are tools with the syntax of colortbl
.
\documentclass{article}
\usepackage{nicematrix}
\begin{document}
\begin{NiceTabular}{ccc>{\columncolor{blue!15}}c}[hvlines,colortbl-like]
\rowcolor{red!15}1 & 22 & 34 & 2346 \\
2346 & 2 & 246 & 2 \\
1 & 34 & 2346111 & 1 \\
\cellcolor{yellow!50}1222 & 0 & 34 & 463
\end{NiceTabular}
\end{document}
You need several compilations (because nicematrix
uses PGF/Tikz nodes under the hood).
If you prefer to have the formatting instructions outside the main array, it's also possible with instructions in the \CodeBefore
.
\documentclass{article}
\usepackage{nicematrix}
\begin{document}
\begin{NiceTabular}{cccc}[hvlines,colortbl-like]
\CodeBefore
\columncolor{blue!15}{3}
\rowcolor{red!15}{1}
\cellcolor{yellow!50}{4-1}
\Body
1 & 22 & 34 & 2346 \\
2346 & 2 & 246 & 2 \\
1 & 34 & 2346111 & 1 \\
1222 & 0 & 34 & 463
\end{NiceTabular}
\end{document}
There is also a command \Block
(initially designed to merge cells) which may be used to color zones in the array.
\documentclass{article}
\usepackage{nicematrix}
\begin{document}
\begin{NiceTabular}{cccc}
\Block[fill=red!15,rounded-corners]{1-*}{}1 & 22 & 34 & 2346 \\
2346 & 2 & 246 & 2 \\
\Block[fill=yellow!50,rounded-corners]{2-2}{0} & & 2346111 & 1 \\
& & 34 & 463
\end{NiceTabular}
\end{document}
Eventually, if you have something special to draw, you may use Tikz and the PGF/Tikz nodes created by nicematrix
under the array.
\documentclass{article}
\usepackage{nicematrix,tikz}
\begin{document}
\begin{NiceTabular}{cccc}
\CodeBefore
\tikz \fill [red!15] (1-|1) -- (5-|3) -- (1-|4) -- cycle ;
\Body
1 & 22 & 34 & 2346 \\
2346 & 2 & 246 & 2 \\
1 & 34 & 2346111 & 1 \\
1222 & 0 & 34 & 463
\end{NiceTabular}
\end{document}