1

How do I get the rowcolor spanned for the entire first row?

\documentclass{article}
\usepackage{longtable, makecell, booktabs}
\usepackage[table]{xcolor}
\begin{document}
    \begin{longtable}{m{0.1\linewidth} m{0.88\linewidth}}
        \toprule\endhead
        \rowcolor{gray}\multicolumn{2}{c}{\makecell[cc]{Top line\\Bottom line}}\\\bottomrule
        a & b
    \end{longtable}
\end{document}

Screenshot of the problem

2 Answers 2

0

Combination of longtable, makecell and booktabs is not good combination for what you like to achieve. If you will change your MWE to

\documentclass{article}

\usepackage{longtable, makecell, booktabs}
\usepackage[table]{xcolor}
\begin{document}
    \begin{longtable}{@{} m{0.1\linewidth} m{0.8\linewidth} @{}}
        \toprule
    \rowcolor{gray!30}
    \multicolumn{2}{p{0.9\linewidth}}{\makecell{Top line\\Bottom line}}\\  % <---
        \midrule
        \endhead
        a & b   \\
        \bottomrule
    \end{longtable}
\end{document}

than you will get:

enter image description here

Using longtblr of tabularray package may require some learning steps, but with it you can get the following result:

enter image description here

In this case the MWE is:

\documentclass{article}

\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}
    \begin{longtblr}[
caption = {Caption},
  label = {longtab:1}
                    ]{colspec = {m{0.1\linewidth} m{0.9\linewidth}},
                      cell{1}{1}  = {c=2}{c, bg=gray!30},
                      hspan   = minimal,
                      rowhead = 1
                      }
        \toprule
{Top line\\Bottom line}
    &       \\
        \midrule
a   &   b   \\
        \bottomrule
    \end{longtblr}
\end{document}
0

It seems better to replace \makecell with \shortstack inside the merged header cell. The color is set at the cell level, so \rowcolor is redundant. I added \addlinespace to fine-tune the vertical spacing under the header—feel free to remove it if you don’t need it.

\documentclass{article}

\usepackage{array}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage[table]{xcolor}

\begin{document}

\begin{longtable}{@{}m{0.1\linewidth} m{0.88\linewidth}@{}}
\toprule\endhead
\addlinespace[-0.6ex] 
%\rowcolor{gray!30}
\multicolumn{2}{>{\columncolor{gray!30}}c}{\shortstack{Top line\\Bottom line}}\\
\addlinespace[-0.5ex]
\bottomrule
a & b\\

\end{longtable}

\end{document}

enter image description here

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.