Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 21 down vote accepted

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

share|improve this answer
add comment

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

share|improve this answer
2  
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 at 13:32
    
0.8 is the gray value (1 means white and 0 means black) –  TheChymera Jun 15 at 0:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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