1

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}

the gray colour doesn't fill the row

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}

still too short

Who can I make the gray colour fill the entire row?

6
  • 2
    The multi-column cell is \dimexpr0.6\textwidth+4\tabcolsep+3\arrayrulewidth\relax wide. May 25, 2023 at 5:09
  • 2
    To elaborate on @JasperHabicht's comment: While the usable width of each of the 3 p-type columns is 0.2\textwidth, their combined width is not 0.6\textwidth but 0.6\textwidth+6\tabcolsep+2\arrayrulewidth. (This takes into account that there's whitespace padding on the side of each cell, in the amount of \tabcolsep.) To obtain the usable width of the combined columns, one must subtract 2\tabcolsep from the total, arriving at 0.6\textwidth+4\tabcolsep+2\arrayrulewidth. Hence, do replace p{0.6\textwidth} with p{\dimexpr0.6\textwidth+4\tabcolsep+2\arrayrulewidth\relax}
    – Mico
    May 25, 2023 at 6:02
  • 2
    @Mico Thank you for elaborating on this! I added one \arrayrulewidth for the border at the very right of the tabular, but this border does not exist actually. So it should indeed be 2\arrayrulewidth. May 25, 2023 at 9:12
  • 1
    @Mico @JasperHabicht Thank you, that works! If you care to make that an answer, I'd be able to accept that (if that's too much trouble I'd post it myself as a community wiki). I'm still curious, though: why doesn't multicolumn{3}{l} work? Also, what documentation can I consult to know about \tabcolsep etc. ?
    – Yosh
    May 28, 2023 at 8:56
  • @Yosh - I've gone ahead and posted an answer. The reason why multicolumn{3}{l}{\makecell{...}} doesn't work here is that the tabular setup doesn't contain enough embedded information to let LaTeX perform the calculations on its own. On your final question: I'll admit readily that the existing documention (in places such as the array package) is quite terse and doesn't explain things patiently, step by step. Put differently, I've had to acquire this knowledge over time, at first mainly by trial and error. :-( FWIW, I've been using LaTeX for more than 30 years; it's been worth it!
    – Mico
    May 28, 2023 at 9:43

3 Answers 3

1

(This answer builds on the material I provided in an earlier comment.)

Let's begin with the OP's basic column specification:

\begin{tabular}{c|p{0.2\textwidth}|p{0.2\textwidth}|p{0.2\textwidth}}

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 is 6pt.

  • 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 is 0.4pt. Hence, the total width of the combination of columns 2 thru 4 is 3*(0.2\textwidth+2\tabcolsep)+2*\arrayrulewidth, or 0.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. Subtracting 2\tabcolsep from 0.6\textwidth+6\tabcolsep+2\arrayrulewidth gives 0.6\textwidth+4\tabcolsep+2\arrayrulewidth as the desired length. Thus, you'd write

    \multicolumn{3}{p{\dimexpr0.6\textwidth+4\tabcolsep+2\arrayrulewidth\relax}{...}
    

where \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 the booktabs package basically makes users not use vertical rules. Hence, be sure to leave off quantities involving \arrayrulewidth from your column width calculations.

enter image description here

\documentclass{article} % or some other suitable document class
\usepackage{booktabs}
\usepackage{xcolor} 
\usepackage{colortbl} % for '\rowcolor' command
\usepackage{makecell}

\begin{document}
\hrule % just to illustrate width of textblock
\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{\dimexpr0.6\textwidth+4\tabcolsep+2\arrayrulewidth\relax}}{%
     \makecell[l]{some lines \\ 
                  that may or may not be long\\  
                  within}}\\
\hline
another & line & is & here \\
\hline
\end{tabular}
\caption{multicolumn + makecell with p-specifier in multicolumn}
\end{table}
\hrule
\end{document}
1

The package nicematrix is a package that provides (among others) an environment {NiceTabular} similar to the classical environment {tabular} (of the package array) with additional features.

In the following code, I have replaced your environment {tabular} by an environment {NiceTabular} (with the key colortbl-like to allow a command \rowcolor similar to the command \rowcolor of the package colortbl).

\documentclass{article}
\usepackage{makecell}
\usepackage{nicematrix}

\begin{document}

\begin{table}[hb]
    \centering
    \begin{NiceTabular}{c|p{0.2\textwidth}|p{0.2\textwidth}|p{0.2\textwidth}}[colortbl-like]
        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{NiceTabular}
    \caption{multicolumn + makecell}
\end{table}

\end{document}

You need several compilations.

Output of the above code

You have directly the expected output because the colored panels are drawn by PGF/Tikz using PGF/Tikz nodes constructed by nicematrix.

However, in your example, with nicematrix, there is no point using makecell because nicematrix provides a command \Block to do the job.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\begin{table}[hb]
    \centering
    \begin{NiceTabular}{c|p{0.2\textwidth}|p{0.2\textwidth}|p{0.2\textwidth}}[colortbl-like]
        column1 & column2 & column3 & column4 \\
        \hline
        \rowcolor[gray]{0.9} hi &  \Block[l]{1-3}{some lines \\ that may or may not be long\\  within}\\
        another & line & is & here
    \end{NiceTabular}
    \caption{multicolumn + makecell}
\end{table}

\end{document}

Output of the second code

The package nicematrix is compatible with beamer and with booktabs.

1

By use of the tabularray package the MWE is simple and short:

\documentclass{beamer}
\setbeamertemplate{caption}[numbered]

\usepackage{xcolor}
\usepackage{tabularray}

\begin{document}
    \begin{table}[hb]
    \centering
\begin{tblr}{hline{2-Y} = solid, vline{2-Y} = solid,
             colspec = {c *{3}{X[l]}
             hspan = minimal            
             }
column 1    & column 2  & column 3  & column 4  \\
\SetRow{bg=gray!30}
hi          &   \SetCell[c=3]{l} {some lines \\ 
                                 that may or may not be long\\  
                                 within}   
                        &           &       \\
another     & line      & is        & here
    \end{tblr}
\caption{Table design by using of the \texttt{tabularray} package.}
    \end{table}
\end{document}

enter image description here

You must log in to answer this question.

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