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 '14 at 03:46
2 Answers
-
3xcolor: http://www.ctan.org/tex-archive/macros/latex/contrib/xcolor/ colortbl:http://www.tug.ctan.org/tex-archive/macros/latex/contrib/colortbl/ (fixing links above) – flies May 13 '11 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. – Ariel Dec 26 '11 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. – arekolek Aug 17 '16 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}
- 40,250



