Graphics, Figures & TablesBackground Color for Table Cells

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
User avatar
devilsadvocate
Posts: 13
Joined: Thu Jun 16, 2011 10:21 pm

Background Color for Table Cells

Post by devilsadvocate »

Dear latexians,

Suppose if I have a table as follows,

Code: Select all

\begin{table}[!b]
\renewcommand{\arraystretch}{1.3}
\caption{Account of My Goats}
\label{tab:goat_acc_table}
\centering
\begin{tabular}{|c||c|}
   \hline
   \textbf{\textit{Color}}  & \textbf{\textit{Number}} \\
   \hline
   White & 1 \\
   \hline
   Black & 2 \\
   \hline
\end{tabular}
\end{table}

Now if I want to invert the color of first row of my table (white fonts with black background), what shall I do ?

Regards,
Last edited by devilsadvocate on Sat Jun 18, 2011 2:13 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

Background Color for Table Cells

Post by Stefan Kottwitz »

Hi,

you could use the colortbl package.

Stefan
LaTeX.org admin
User avatar
devilsadvocate
Posts: 13
Joined: Thu Jun 16, 2011 10:21 pm

Re: Background Color for Table Cells

Post by devilsadvocate »

Thanks for the info. Any alternative package? Just used this package and the output is out of the world! so much that it is too unusual for IEEE Transaction format.

Regards,
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Background Color for Table Cells

Post by localghost »

It works even with the IEEEtran class. The below example gives some inspiration. But it looks really ugly.

Code: Select all

\documentclass[11pt]{IEEEtran}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{array}
\usepackage[table]{xcolor}  % 'table' option loads »colortbl«

% from the UK TeX FAQ
\newcolumntype{$}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{%
  \gdef\currentrowstyle{#1}%
  #1\ignorespaces
}

\begin{document}  
  \begin{table}[!ht]
    \renewcommand{\arraystretch}{1.3}
    \caption{Account of My Goats}\label{tab:goat-acc-table}
    \centering
    \begin{tabular}{|$c||^c|}\hline
      \rowstyle{\bfseries\itshape}
      Color  & Number \\ \hline
      \rowcolor{black}
      \rowstyle{\color{white}\bfseries}
      White & 1 \\ \hline
      Black & 2 \\ \hline
    \end{tabular}
  \end{table}
\end{document}
Note that the sequence of \rowcolor and \rowstyle is important here.


Thorsten
User avatar
Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

Re: Background Color for Table Cells

Post by Stefan Kottwitz »

I think inverted text is unusual, this kind of emphasizing is harder to read. It's used in fancy design books though.

The output of colortbl depends on how you use it. You could also use \fcolorbox or \colorbox instead.

Stefan
LaTeX.org admin
User avatar
devilsadvocate
Posts: 13
Joined: Thu Jun 16, 2011 10:21 pm

Re: Background Color for Table Cells

Post by devilsadvocate »

Hello localghost, now this worked. At least looks better than before. However, Stafan's statement is very correct. I would like to stay as I was. In any case changed looks very unusual in comparison to existing articles and will throw my article out of league, which I do not want.

Thanks for giving a wing of my idea. Although it did not went well with its look and feel, but I learnt a lot.

Regards,
Post Reply