4

In my answer to question How do I repeat the first table row on every page using longtable inside of tcolorbox?

I suggest to (between others) to use makecell package to define head of table and later I sow that the firs row is not colored as expected. It doesn't cover all row. Than I try to correct this with use of cellcolor construct as should be correctly done, when you use multicolumn (simplified MWE from mentioned question):

\documentclass[12pt]{article} \usepackage[utf8]{inputenc} \usepackage[table]{xcolor} \usepackage{longtable} \usepackage{makecell} \renewcommand\theadfont{\bfseries} \usepackage[active,tightpage]{preview} \PreviewEnvironment{longtable}% \setlength\PreviewBorder{1em} \begin{document} \begin{longtable}{|*{9}{b{\dimexpr 0.11\linewidth-2\tabcolsep}|}} \hline \multicolumn{8}{|c|}{\cellcolor{gray!30}{\thead{head\\ 1}}} & \cellcolor{gray!30}{\thead{head\\ 2}} \\ \hline 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 \\ \hline\hline \endfirsthead \hline 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 \\ \hline\hline \endhead A & B & C & D & E & F & G & H & I \\ J & K & L & M & N & O & P & Q & R \\ S & T & U & V & W & X & Y & Z & \\ \hline\hline \end{longtable} \end{document} 

and discover that that head inmulticolumn doesn't has width of it and consequently head background color doesn't fill complete cell.

enter image description here

I Check makecell documentation, but doesn't find valuable information how to stretch actual head to cover entire cell. How to achieve that entire multicolum cell background will be colored?

addendum: for this particular case the stretch \thead over whole \multicolumn can be done on the following way: instead of

\multicolumn{8}{|c|}{\thead{\hfill head\hfill\\ 1}} 

the multi column cell should be defined as

\multicolumn{8}{|p{\dimexpr 0.88\linewidth-\tabcolsep}|}{\thead{\hfill head\hfill\\ 1}} 

where \dimexpr 0.88\linewidth-\tabcolsep is the width of multi col cell. then the result is:

enter image description here

however, general solutions are given in answers below.

2 Answers 2

4

If you put the \thead in the \multicolumn{|c|} in a \colorbox, it works (somewhat redundantly…):

\documentclass[12pt]{article} \usepackage[utf8]{inputenc} \usepackage{xcolor}%[table] \usepackage{array, colortbl} \usepackage{longtable, ltablex} \usepackage{makecell} \renewcommand\theadfont{\bfseries} \usepackage[active,tightpage]{preview} \PreviewEnvironment{longtable}% \setlength\PreviewBorder{1em} \begin{document} \begin{longtable}{|*{9}{b{\dimexpr 0.11\linewidth-2\tabcolsep}|}}% \hline \rowcolor{gray!30}\multicolumn{8}{|c|}{{\colorbox{gray!30}{\thead{head\\ 1}}}} & \thead{head\\ 2} \\ \hline 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 \\ \hline\hline \endfirsthead \hline 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 \\ \hline\hline \endhead A & B & C & D & E & F & G & H & I \\ J & K & L & M & N & O & P & Q & R \\ S & T & U & V & W & X & Y & Z & \\ \hline\hline \end{longtable} \end{document} 

enter image description here

5
  • Indeed. But now I need to manually calculate width of columns which are spanned by multicolumn. This can become annoying/problematic when the widths of column are not equal and/or determined with specifier with not a priory given width. Commented May 26, 2015 at 6:46
  • 1
    @Zarko: I changed the code to avoid those calculations: it's enough to put to add a \colorbox. See my updated answer. Commented May 26, 2015 at 7:59
  • now I'm total confused. Why now this work and didn't in my first tray? I need detail examination of all my "experiments", to see where are differences. One another observation: you put array package twice in preamble, probably by mistake. I try to compile without it and also without ltablex package and obtain the same results as you show in your answer. Commented May 26, 2015 at 8:45
  • Now I observe differences: you add colortab in preamble and use colorbox instead cellcolor ... Good to know, thank you! I will change now my acceptances of answer. Commented May 26, 2015 at 8:51
  • Yes, array was loaded twice by mistake, I've corrected. I tested, and it doesn't seem to be required. I generally add it because it extends the syntax of the original array environment. As to ltablex it is there because I tested with a tabularx environment (it allows the latter to break across pages), but I get an error which I don't understand, about a spurious }. You can remove it. Commented May 26, 2015 at 9:07
2

I don't think that makecell is fully compatible with colortbl. However, you can easily emulate \thead:

\documentclass[12pt]{article} \usepackage[utf8]{inputenc} \usepackage[table]{xcolor} \usepackage{longtable} \newcommand{\thead}[1]{% \bfseries \begin{tabular}{@{}c@{}} \vrule height 1.2\ht\strutbox width 0pt\ignorespaces #1 \unskip\vrule depth 1.2\dp\strutbox width 0pt \end{tabular}% } \begin{document} \begin{longtable}{|*{9}{b{\dimexpr 0.11\linewidth-2\tabcolsep-\arrayrulewidth}|}} \hline \multicolumn{8}{|c|}{\cellcolor{gray!30}{\thead{head\\ 1}}} & \cellcolor{gray!30}{\thead{head\\ 2}} \\ \hline 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 \\ \hline \endfirsthead \hline 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 \\ \hline \endhead A & B & C & D & E & F & G & H & I \\ J & K & L & M & N & O & P & Q & R \\ S & T & U & V & W & X & Y & Z & \\ \hline \end{longtable} \end{document} 

enter image description here

Here's a minimal example:

\documentclass[12pt]{article} \usepackage[table]{xcolor} \usepackage{makecell} \renewcommand{\theadfont}{\bfseries} \begin{document} \begin{tabular}{|c|c|c|} \hline \multicolumn{2}{|c|}{\cellcolor{gray!30}\thead{head\\1}} & \thead{head\\2} \\ \hline 111 & 222 & 333 \\ \hline \end{tabular} \end{document} 

enter image description here

Using \rowcolor doesn't solve the problem.

1
  • thank you for editing my question and for answer. You confirm my suspect that makecell and colortab are not full compatible. As I see, thead doesn't use complete width of "multi-column" cell.That means, that there is a chance that one day this will be fixed :-). For particular case, mentioned in my question, where the first row is not multi-lined, is then better solution not use makecell and thead at all. Other wise, your solution is what I will use in future in similar cases. Commented May 25, 2015 at 22:53

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.