3

Please have a look at the following code:

\documentclass[12pt]{report}
\usepackage[]{graphicx}
\usepackage[]{xcolor}
\usepackage{booktabs}

\begin{document}

\colorbox{yellow!20}{
\begin{table}
\begin{tabular}{lll}
\toprule
\multicolumn{1}{l}{}&\multicolumn{1}{c}{Female}&\multicolumn{1}{c}{Male}\tabularnewline                                   
\midrule
1 year- 2 years&2 (50\%)&2 (13.33\%)\tabularnewline
2 years - 3 years&0 (0\%)&4 (26.67\%)\tabularnewline
3 years +&1 (25\%)&3 (20\%)\tabularnewline
6 months- 1 year&1 (25\%)&3 (20\%)\tabularnewline
Less than 6 months&0 (0\%)&3 (20\%)\tabularnewline
\bottomrule
\end{tabular}
\end{table}
}

\end{document}

I don't get a output from the above code. But if comment out Table environment that is I put the Tabular environment in the colorbox then it works. My objective is to color the entire table but I want to color the Table environment. This is because I am creating table with latex function from Hmisc package from R which automatically creates the Table environment. I know I could use xtable package with floating=False option but I need to use latex function for other purposes.

I also understand I am not supposed to ask R questions here but since the latex related problem arose due to use of certain R package, if any one could suggest a workaround with R tables to created colored background in latex that would be quite helpful.

1
  • You cannot put a float such as table inside a \colorbox which is static. Commented Jul 8, 2015 at 15:39

2 Answers 2

3

You cannot put a float such as table inside a \colorbox which is static. Go the other way around and use \colorbox inside table:

\documentclass[12pt]{report}
\usepackage[]{graphicx}
\usepackage[]{xcolor}
\usepackage{booktabs}

\begin{document}

\begin{table}
\colorbox{yellow!20}{%
\begin{tabular}{lll}
\toprule
\multicolumn{1}{l}{}&\multicolumn{1}{c}{Female}&\multicolumn{1}{c}{Male}\tabularnewline                                   
\midrule
1 year- 2 years&2 (50\%)&2 (13.33\%)\tabularnewline
2 years - 3 years&0 (0\%)&4 (26.67\%)\tabularnewline
3 years +&1 (25\%)&3 (20\%)\tabularnewline
6 months- 1 year&1 (25\%)&3 (20\%)\tabularnewline
Less than 6 months&0 (0\%)&3 (20\%)\tabularnewline
\bottomrule
\end{tabular}%
}
\end{table}

\end{document}

enter image description here

I'd suggest you to use siunitx to properly format and align your data.

5
  • Hello Gonazalo, my problem is that my latex tables are autogenerated in R, therefore only way to put the colorbox is manually or using some postprocessing code. Is there any other way to preprocess, that is putting other latex functions before the Table environment to make the color work? Commented Jul 8, 2015 at 15:49
  • @RushadFaridi And do you want to keep the tables as floating objects or can you restrict/suppress flotation? Commented Jul 8, 2015 at 16:26
  • I don't mind either way as long as the color works. Commented Jul 8, 2015 at 17:53
  • @RushadFaridi The problem then is how to capture the width of the table. I can think on a solution, but it will apply the background color to the entire \textwidth, not just to the tabular. Would this be useful for you or do you want to restrict the color just to the width of the tabular material? Commented Jul 8, 2015 at 18:15
  • Tabular material would be good but little bit more than that won't hurt I guess. But if it the whole width of text area then its a problem. Commented Jul 8, 2015 at 20:20
0

Here is a solution, without the table environment — replaced with center. In case you still want a caption, I used the captionof command, supplied by the capt-of and the caption package. Don't forget % just before \begin{tabular} and just after \end{tabular}.

\documentclass[12pt]{report}
\usepackage[]{graphicx}
\usepackage[table, x11names]{xcolor}
\usepackage{caption, booktabs}
\usepackage{lipsum}

\begin{document}
\lipsum[2]
%
\begin{center}
  \setlength\fboxsep{0pt}
  \vskip-\topsep%
  \captionof{table}{Sexology}
  \smallskip%
  \colorbox{Yellow1!15}{%
    \begin{tabular}{lll}
      \toprule
                         & \multicolumn{1}{c}{\color{HotPink1}Female} & \multicolumn{1}{c}{\color{DodgerBlue2}Male}\tabularnewline
      \midrule
      1 year--2 years & 2 (50\%) & 2 (13.33\%)\tabularnewline
      2 years--3 years & 0 (0\%) & 4 (26.67\%)\tabularnewline
      3 years + & 1 (25\%) & 3 (20\%)\tabularnewline
      6 months--1 year & 1 (25\%) & 3 (20\%)\tabularnewline
      Less than 6 months & 0 (0\%) & 3 (20\%)\tabularnewline
      \bottomrule
    \end{tabular}%
  }
\end{center}%
\lipsum[3]

\end{document} 

enter image description here

9
  • I need a solution with Table environment not just Tabular. Commented Jul 8, 2015 at 20:21
  • What for do you want a table environment? Ìf it is only to be able to cross-reference it? You can do it with \captionof{table}. If you really want to let it float (that is the only difference with ``\captionof, as far as I know), I'm sorry, I don't know how to do it, and I'm afraid it's not possible. Or you will have to modify the code for tabular, adding >{\columncolor{yellow!80!} before each qualifier in the tabular preamble.
    – Bernard
    Commented Jul 8, 2015 at 20:29
  • Thanks Bernard for your response. Can anybody else suggest anything or I should post the question in the R forum? Commented Jul 9, 2015 at 10:34
  • I'm not sure of what is a Table environment here (with a capital T). Is it some spcoal environment defined by R? If it is, yes, you could try to post onthe R forum. If you mean only an ordinary table environment, why do you want to have one, since you answered to @Gonzalo Medina you didn't mind whether your table would be floating or not?
    – Bernard
    Commented Jul 9, 2015 at 10:51
  • sorry for the confusion, I just meant the regular table environment. I don't mind the table to be floating or not as long as it comes out of R code. But the problem is that the Code I am using generates table environment and I don't find any work around to that. Commented Jul 9, 2015 at 13:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .