I'm creating a table. I need a cell that (1) spans 3 columns and (2) has multiple lines inside it; I will specify where the line breakings occur. Also, I want the row that contains that cell to be coloured. Because I will be using the table in beamer, I need to specify the (maximum) widths of the columns myself (otherwise, the table will stretch outside the right border). Although I removed from the working examples below, I will be using booktabs
package, too, so I'd be happier with solutions that don't conflict with that.
I choose p{0.2\textwidth}
for the tabular specification (as stated above, I want to specify the widths of the columns). This is my try:
\documentclass{article}
\usepackage{colortbl}
\usepackage{makecell}
\begin{document}
\begin{table}[hb]
\centering
\begin{tabular}{c|p{0.2\textwidth}|p{0.2\textwidth}|p{0.2\textwidth}}
column1 & column2 & column3 & column4 \\
\hline
\rowcolor[gray]{0.9} hi & \multicolumn{3}{l}{ \makecell[l]{some lines \\ that may or may not be long\\ within}}\\
another & line & is & here
\end{tabular}
\caption{multicolumn + makecell}
\end{table}
\end{document}
Obviously, the gray colour doesn't fill the entire row.
Also I tried with 0.6 \textwidth
(0.2+0.2+0.2) specifier with multicolumn, partially hoping that it'd eliminate the need for makecell
but with no luck (probably because the spacing between columns etc):
\begin{table}[hb]
\centering
\begin{tabular}{c|p{0.2\textwidth}|p{0.2\textwidth}|p{0.2\textwidth}}
column1 & column2 & column3 & column4 \\
\hline
\rowcolor[gray]{0.9} hi & \multicolumn{3}{p{0.6\textwidth}}{ \makecell[l]{some lines \\ that may or may not be long\\ within}}\\
another & line & is & here
\end{tabular}
\caption{multicolumn + makecell with p-specifier in multicolumn}
\end{table}
Who can I make the gray colour fill the entire row?
Best Answer
(This answer builds on the material I provided in an earlier comment.)
Let's begin with the OP's basic column specification:
The OP's question is: What's the usable width of the combination of columns 2, 3, and 4?
This table has 4 columns; of interest are columns 2 thru 4. When determining the desired widths of combined columns in a LaTeX table, it's very important to distinguish between a column's usable width and its total width.
For
p
-type columns, the usable width is stated in its argument -- here,0.2\textwidth
for columns 2 thru 4.A column's total width is the sum of the usable width and the whitespace padding (if any) that LaTeX inserts at either side of the column. The amount of whitespace padding is governed by the parameter
\tabcolsep
. In most document classes I'm familiar with, the default value of this parameter is6pt
.Thus, the total width of each of column 2, 3, and 4 is
0.2\textwidth+2\tabcolsep
. To get the total width of the combined set of columns 2 thru 4, one has to take into account that they're separated by interior vertical rules, whose width is governed by the parameter\arrayrulewidth
; the default value of this parameter is0.4pt
. Hence, the total width of the combination of columns 2 thru 4 is 3*(0.2\textwidth+2\tabcolsep)+2*\arrayrulewidth, or0.6\textwidth+6\tabcolsep+2\arrayrulewidth
.Finally, the usable width of the combined columns has to take into account that LaTeX sets aside whitespace -- in the amount of
\tabcolsep
-- on either side of the combined column. Subtracting2\tabcolsep
from0.6\textwidth+6\tabcolsep+2\arrayrulewidth
gives0.6\textwidth+4\tabcolsep+2\arrayrulewidth
as the desired length. Thus, you'd writewhere
\dimexpr
and\relax
are TeX's (admittedly not exactly elegant) tools to perform dimension calculations "on the fly".An additional comment: You mention that you wish to use the tools of the
booktabs
package. That's great. You're most likely already aware that thebooktabs
package basically makes users not use vertical rules. Hence, be sure to leave off quantities involving\arrayrulewidth
from your column width calculations.