96

I've found question How do I fill table cells with a background color?, but the link to the documentation isn't helping me at all.

I'd like to know how to color a table column given my column code, but the documentation only gives the syntax of the command. It doesn't give any code example of where to use it.

\usepackage{colortbl}

\begin{table}[h]
    \centering
    \begin{tabular}{|c|c|c|c|c|c|c|c|}
        \hline
        \cellcolor[gray]{0.8}128&64&32&16&8&4&2&1\\\hline
        1&0&1&1&0&1&0&1\\\hline
    \end{tabular}
\end{table}

As you can see, I got \cellcolor to work, but I don't want to color individual cells. I want to color a whole column of the table. Does anybody know how I can go about to do this?

4 Answers 4

119

Here a small example, which might be of help to you.

  • Define your own color using \definecolor{Gray}{gray}{0.85}
  • Define your own columns using \newcolumntype{a}{>{\columncolor{Gray}}c}
  • You are ready to go.

Code

\documentclass{article}
\usepackage{xcolor,colortbl}

\newcommand{\mc}[2]{\multicolumn{#1}{c}{#2}}
\definecolor{Gray}{gray}{0.85}
\definecolor{LightCyan}{rgb}{0.88,1,1}

\newcolumntype{a}{>{\columncolor{Gray}}c}
\newcolumntype{b}{>{\columncolor{white}}c}

\begin{document} 


\begin{table}
\begin{tabular}{l | a | b | a | b}
\hline
\rowcolor{LightCyan}
\mc{1}{}  & \mc{1}{x} & \mc{1}{y} & \mc{1}{w} & \mc{1}{z} \\
\hline
variable 1 & a & b & c & d \\
variable 2 & a & b & c & d \\ \hline
\end{tabular}
\end{table}

\end{document}

Result

image

1
  • 3
    can i make columncolor overwrite rowcolor?
    – mostafax80
    Sep 11, 2016 at 19:59
47

You can use \columncolor as

\begin{tabular}{|>{\columncolor[gray]{0.8}}c|c|c|c|c|c|c|c|}

code:

\documentclass{article}
\usepackage{colortbl}

\begin{document}
\begin{table}[h]
    \centering
    \begin{tabular}{|>{\columncolor[gray]{0.8}}c|c|c|c|c|c|c|c|}
        \hline
        128&64&32&16&8&4&2&1\\\hline
        1&0&1&1&0&1&0&1\\\hline
        1&0&1&1&0&1&0&1\\\hline
    \end{tabular}
\end{table}
\end{document}

enter image description here

6
  • 8
    I think this answer could be much more useful with a bit of explanation about e.g. what 0.8 is and how the syntax can be understood.
    – Cerran
    Mar 18, 2014 at 13:32
  • 2
    0.8 is the gray value (1 means white and 0 means black)
    – TheChymera
    Jun 15, 2014 at 0:33
  • 1
    This code doesn't compile to me, I had to change the brackets of the color with {} Oct 22, 2014 at 2:19
  • 3
    You can also use, in the same way, >{\columncolor[RGB]{230, 242, 255}}. Remember, however, to load the package xcolor together with colortbl. At this link you can find a good color picker.
    – Luigi
    Jun 16, 2016 at 11:02
  • 2
    Why do you need a great than symbol > for this to actually compile?
    – Code Doggo
    Jun 26, 2020 at 16:17
6

It’s easy peasy to color cells/rows/columns with tblr environment of the new LaTeX3 package tabularray:

\documentclass{article}

\usepackage{xcolor}
\usepackage{tabularray}

\begin{document}

\begin{table}[h]
  \centering
  \begin{tblr}{
    colspec = {|c|c|c|c|c|c|c|c|},
    row{2} = {purple7},
    column{3} = {teal7},
    cell{2}{3} = {yellow7},
  }
    \hline
      128 & 64 & 32 & 16 & 8 & 4 & 2 & 1 \\
    \hline
        1 &  0 &  1 &  1 & 0 & 1 & 0 & 1 \\
    \hline
        1 &  0 &  1 &  1 & 0 & 1 & 0 & 1 \\
    \hline
  \end{tblr}
\end{table}

\end{document} 

enter image description here

4

With {NiceTabular} of nicematrix, you can color a column with the same syntax as in {tabular} (when colortbl is loaded) with the advantage that you will have a perfect PDF output: rules won't seem to vanish and you won't see thin white lines, whatever PDF viewer you use.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{c|>{\columncolor{blue!15}}c|cc}[colortbl-like]
A & 1 & 2 & 3 \\
B & 4 & 5 & 10 \\
C & 9 & 12 & 15
\end{NiceTabular}

\end{document}

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

Output of both codes

It's also possible to specify instructions to color columns (or cells, rows, etc.) in the \CodeBefore before the contents of the tabular.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{c|c|cc}
\CodeBefore
  \columncolor{blue!15}{2}
\Body
  A & 1 & 2 & 3 \\
  B & 4 & 5 & 10 \\
  C & 9 & 12 & 15
\end{NiceTabular}

\end{document}

The output is the same.

More generally, you can fill an column (or any zone of the tabular) with any pattern of Tikz.

\documentclass{article}
\usepackage{nicematrix,tikz}

\begin{document}

\begin{NiceTabular}{c|c|cc}
  A & \Block[tikz={top color=blue!15}]{*-1}{}
      1 & 2 & 3 \\
  B & 4 & 5 & 10 \\
  C & 9 & 12 & 15
\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.