Graphics, Figures & Tables ⇒ A table in a colored box
A table in a colored box
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?
Learn LaTeX easily with newest books:
The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis
The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more
LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis
A table in a colored box
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}
Re: A table in a colored box
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.
A table in a colored box
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}
A table in a colored box
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}