When using \colorbox{color}{text}
to put background color for the text
in a table, the text
with background color seems to take more space and makes the respective cell higher and wider. Is there a way to use \colorbox{color}{text}
in a table cell without affecting the dimension of the cell?
1 Answer
You need to set the value of \fboxsep
to zero to remove the extra margin:
{\setlength{\fboxsep}{0pt}\colorbox{<color>}{<text>}}
You don't need the outer braces if it is the whole cell, however then I would use the macros of the colortbl
or colortab
packages. The new tabu
package also provides a lot of new features for tables, including a lot of different color features, but it might be an effort to change to this package.
You can also use the adjustbox
package for this:
\adjustbox{cframe=<color>}{<text>}
which draws a tight frame as well. It has also an extended syntax which allows to set the rule thickness, inner separation and outer margin:
\adjustbox{cframe=<color> <thickness> <inner sep> <outer margin>}{<text>}
The margin can be negative and if it is -(thickness+inner sep) the original size is kept, e.g.:
\adjustbox{cframe=blue {\fboxrule} {\fboxsep} {-\fboxrule-\fboxsep}}{<text>}
(The { }
around the macros are there to preserve the space behind them.)
-
1Works perfectly, thank you very much. I was trying to use
colorbl
package for highlighting columns of table. However, I am using overlay/transition, and I am not sure how to do it. I may start another question to ask about it later.– Li WangJul 21, 2011 at 8:58 -
1+ 1
\adjustbox
is a perfect instrument, especially for those who are used to HTML formatting– yegor256Dec 10, 2012 at 13:26 -
@yegor256: Thanks, I indeed coded it because I missed the power of CSS. Nov 29, 2017 at 13:36
-
1I think it must be
-\fboxrule-\fboxsep
rather than\fboxrule+\fboxsep
. I also tested this. Unfortunately I cannot edit it since changes must be at least 6 characters long. Dec 3, 2019 at 11:45 -
1@theV0ID: Yes, I originally stated that directly the line before but then made it false myself ;-) I fixed it now. Thanks for reporting it. Dec 6, 2019 at 6:55