Graphics, Figures & TablesA table in a colored box

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
cozmo87
Posts: 3
Joined: Sun Apr 04, 2010 1:02 am

A table in a colored box

Post by cozmo87 »

Hi,

I want to create a table with a colored background. I tried looking into the colortbl package but couldn't get anything usefull out of it. I included an example of the kind of table I want to achieve. Any ideas how this can be done?
Table.jpg
Table.jpg (103.11 KiB) Viewed 3781 times

Recommended reading 2024:

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

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

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

A table in a colored box

Post by gmedina »

Hi,

you can use the shaded environment provided by the framed package; you have to be careful, however, since that environment doesn't cooperate with floating objects (such as thos declared by using the figure or table environments), so you cannot place your table inside a table environment. This implies that you will have to use the \captionof command (provided by the caption package) if you want a caption for your table.

The following code illustrates the above ideas:

Code: Select all

\documentclass{article}
\usepackage{xcolor}
\usepackage{caption}
\usepackage{framed}
\usepackage{booktabs}
\usepackage{lipsum} % just to generate some text

\definecolor{shadecolor}{rgb}{0.8,0.8,0.8}

\begin{document}

\lipsum[1]
\begin{shaded}
  \centering
  \captionof{table}{A test table inside a colored box.}
  \noindent\begin{tabular}{p{.9\textwidth}}
    \toprule
    \lipsum[1]\\
    \bottomrule
  \end{tabular}
\end{shaded}
\lipsum[1]

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
cozmo87
Posts: 3
Joined: Sun Apr 04, 2010 1:02 am

Re: A table in a colored box

Post by cozmo87 »

Many thanks,

but now I have the problem that the shaded environment can't be stretched over the entire width of the page in a two column document. For regular tables u can use the starred version of the table environment to achieve this:

\begin{table*}

But this doesn't work for the shaded environment unfortunately.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

A table in a colored box

Post by gmedina »

Which document class are you using? and how are you setting the two column mode (by using the twocolumn class option or by using the multicol package, for example)?

The following code should do the job in one of the standard document classes using two column mode:

Code: Select all

\documentclass[twocolumn]{article}
\usepackage{xcolor}
\usepackage{caption}
\usepackage{booktabs}
\usepackage{lipsum} % just to generate some text

\definecolor{shadecolor}{rgb}{0.8,0.8,0.8}

\begin{document}

\lipsum[1-10]
\begin{table*}
  \caption{A test table inside a colored box.}
  \colorbox{shadecolor}{
  \centering
  \noindent\begin{tabular}{p{.9\textwidth}}
    \toprule
    \lipsum[1]\\
    \bottomrule
  \end{tabular}}
\end{table*}
\lipsum[1-10]

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
cozmo87
Posts: 3
Joined: Sun Apr 04, 2010 1:02 am

A table in a colored box

Post by cozmo87 »

No I'm not using multicol, just the standard way of doing 2 columns.
The code you posted works for me when I add a minipage environment:

Code: Select all

    
    \documentclass[twocolumn]{article}
    \usepackage{xcolor}
    \usepackage{caption}
    \usepackage{booktabs}
    \usepackage{lipsum} % just to generate some text

    \definecolor{shadecolor}{rgb}{0.8,0.8,0.8}

    \begin{document}

    \lipsum[1-10]
    \begin{table*}
      \caption{A test table inside a colored box.}
      \colorbox{shadecolor}{
      \begin{minipage}{17cm}
      \centering
      \noindent\begin{tabular}{p{.9\textwidth}}
        \toprule
        \lipsum[1]\\
        \bottomrule
      \end{tabular}
      \end{minipage}}
    \end{table*}
    \lipsum[1-10]

    \end{document}
Post Reply