40

I'd like to fill the cells of a table with a background color. How can this be done?

1
33

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.

3
  • 2
  • 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
  • 4
    This 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
2

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).

Output of the first code

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}

Output of the second code

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}

Output of the third code

Eventuallay, 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}

Output of the above code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.