3

I am attempting to use tabular* to generate a table as I would like to also use the midrule, bottomrule etc. in order to get the different line thicknesses. However, in my attempt below, as I also try to color sections of the table, I get spaces in the color like this:

enter image description here

I have looked at these related solutions but wanted to know if there is a simpler solution to use here as I am still not getting my desired result.

This is my code:

\documentclass{article}
\usepackage{float}
\usepackage{xcolor,colortbl,tabularx}
\usepackage{bigstrut}
\definecolor{LightGray}{gray}{0.9}

\begin{document}

\begin{table}[H] \centering \caption{Not too good looking table here.\label{tab:table1}} \footnotesize % Table generated by Excel2LaTeX from sheet 'Sheet2' \begin{tabularx}{\textwidth}{c @{\extracolsep{\fill}} c|ccc} %\begin{tabular}{cc|cc} %\toprule \hline \multicolumn{2}{c|}{\textbf{Test1}} & \multicolumn{2}{c}{\textbf{Test2}} \bigstrut[t]\ Test11 & Test12 & Test13 & Test14 \bigstrut[b]\ %\midrule \hline FLTR2 & 2 & 40 & FLTR0 \bigstrut[t]\ FLTR0 & 80 & 80 & FLTR0 \ \rowcolor[rgb]{ 1, .78, .808} \textcolor[rgb]{ .612, 0, .024}{FLTR0} & \textcolor[rgb]{ .612, 0, .024}{160} & \textcolor[rgb]{ .612, 0, .024}{204} & \textcolor[rgb]{ .612, 0, .024}{FLTR4} \ FLTR6 & 6 & 44 & FLTR4 \ \rowcolor[rgb]{ 1, .78, .808} \textcolor[rgb]{ .612, 0, .024}{FLTR16} & \textcolor[rgb]{ .612, 0, .024}{16} & \cellcolor[rgb]{ 1, 1, 1}43 & \cellcolor[rgb]{ 1, 1, 1}FLTR3 \ FLTR4 & 44 & 6 & FLTR6 \bigstrut[b]\ \hline %\bottomrule \end{tabularx}% %\end{tabular} \end{table}

\end{document}

How to get the color to fill the cells without any gaps and at the same time use the toprule, midrule etc?

Joe
  • 9,080
  • 4
    colortbl really doesn't want to colour the inter-column spaces produced by tabular* if you really want to stretch the table, you could look at newer packages such as nicematrix or tabularray that use different mechaisms to add the coloured panels. – David Carlisle Oct 15 '22 at 16:52

3 Answers3

5
  • Use of @{\extracolsep{\fill}} insert extra spaces between columns which cannot be colored by \rowcolor{...} So, a way to use of tabularx is promising way, but in it you shouldn't use @{\extracolsep{\fill}}
  • At use of tabularx require that at least one column is type X
  • If all columns can have the same width, than code for your table usingtabularx can be:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}

\begin{table}[ht] \centering \caption{Not too good looking table here.\label{tab:table1}} \footnotesize \begin{tabularx}{\textwidth}{CC|CC} \hline \multicolumn{2}{c|}{\textbf{Test1}} & \multicolumn{2}{c}{\textbf{Test2}} \ Test11 & Test12 & Test13 & Test14 \ \hline FLTR2 & 2 & 40 & FLTR0 \ FLTR0 & 80 & 80 & FLTR0 \ \rowcolor{red!30} \textcolor{purple}{FLTR0} & \textcolor{purple}{160} & \textcolor{purple}{204} & \textcolor{purple}{FLTR4} \ FLTR6 & 6 & 44 & FLTR4 \ \rowcolor{red!30} \textcolor{purple}{FLTR16} & \textcolor{purple}{16} & \textcolor{purple}{43ΒΈ} & \textcolor{purple}{FLTR3} \ FLTR4 & 44 & 6 & FLTR6 \ \hline \end{tabularx}% \end{table}

\end{document}

enter image description here

To my opinion this table has far to much empty spaces between columns contents. It will be nicer, if you are willing to reduce table width.

Edit: An example of narrower table with colored only some cells in the first and second column width use of tabularray and siunitx packages, MWE can be:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx, varwidth}
\NewTableCommand\SCC[2]{\SetCell{bg=#1, fg=#2}}

\begin{document}

\begin{table}[ht]
\centering

\caption{Not too good looking table here.} \label{tab:table1} \begin{tblr}{colspec={Q[l] Q[c, si={table-format=3.0}] | Q[c, si={table-format=3.0}] Q[l] }, row{1} = {font=\bfseries, guard}, row{2} = {guard} } \toprule \SetCell[c=2]{c} Test 1 & & \SetCell[c=2]{c} Test 2
& \ Test11 & Test12 & Test13 & Test14 \ \midrule FLTR2 & 2 & 40 & FLTR0 \ FLTR0 & 80 & 80 & FLTR0 \ \SetRow{bg=red!20, fg=purple} FLTR0 & 160 & 204 & FLTR4 \
FLTR6 & 6 & 44 & FLTR4 \ \SCC{red!20}{purple} FLTR16 & \SCC{red!20}{purple} 16 & 43 & FLTR3 \ FLTR4 & 44 & 6 & FLTR6 \ \bottomrule \end{tblr}% \end{table}

\end{document}

enter image description here

Zarko
  • 296,517
  • thanks for your solution, however, I am running to an error, ! Undefined control sequence. \l_tmpa_tl ->\SCC{red!20}{purple} ... – Joe Oct 16 '22 at 01:36
  • 1
    @Joe, sory, at editing of answer I forgot add definition for \SCC. Now corrected. – Zarko Oct 16 '22 at 01:59
4

maybe this helps--queries are welcome

MWE

\documentclass{article}
\usepackage{nicematrix}
\usepackage{booktabs}
\usepackage{bigstrut}
\definecolor{LightGray}{gray}{0.9}

\begin{document}

\begin{NiceTabular}{c @{\extracolsep{\fill}} c|ccc}[colortbl-like]
    \toprule
\multicolumn{2}{c|}{\textbf{Test1}} & \multicolumn{2}{c}{\textbf{Test2}} \bigstrut[t]\\
    Test11 & Test12 & Test13 & Test14 \bigstrut[b]\\
    \midrule
    FLTR2 & 2     & 40    & FLTR0 \bigstrut[t]\\
    FLTR0 & 80    & 80    & FLTR0 \\
    \rowcolor[rgb]{ 1,  .78,  .808} \textcolor[rgb]{ .612,  0,  .024}{FLTR0} & \textcolor[rgb]{ .612,  0,  .024}{160} & \textcolor[rgb]{ .612,  0,  .024}{204} & \textcolor[rgb]{ .612,  0,  .024}{FLTR4} \\
    FLTR6 & 6     & 44    & FLTR4 \\
    \rowcolor[rgb]{ 1,  .78,  .808} \textcolor[rgb]{ .612,  0,  .024}{FLTR16} & \textcolor[rgb]{ .612,  0,  .024}{16} & \cellcolor[rgb]{ 1,  1,  1}43 & \cellcolor[rgb]{ 1,  1,  1}FLTR3 \\
    FLTR4 & 44    & 6     & FLTR6 \bigstrut[b]\\
    \bottomrule
\end{NiceTabular}

\end{document}

enter image description here

To expand the table over the entire text width--provide X columns similar of the columns of tabularx

\documentclass{article}
\usepackage{nicematrix}
\usepackage{booktabs}
\usepackage{bigstrut}
\definecolor{LightGray}{gray}{0.9}

\begin{document}

\begin{NiceTabular}{c X[c]|c X[c]}[colortbl-like]
    \toprule
    \multicolumn{2}{c|}{\textbf{Test1}} & \multicolumn{2}{c}{\textbf{Test2}} \bigstrut[t]\\
    Test11 & Test12 & Test13 & Test14 \bigstrut[b]\\
    \midrule
    FLTR2 & 2     & 40    & FLTR0 \bigstrut[t]\\
    FLTR0 & 80    & 80    & FLTR0 \\
    \rowcolor[rgb]{ 1,  .78,  .808} \textcolor[rgb]{ .612,  0,  .024}{FLTR0} & \textcolor[rgb]{ .612,  0,  .024}{160} & \textcolor[rgb]{ .612,  0,  .024}{204} & \textcolor[rgb]{ .612,  0,  .024}{FLTR4} \\
    FLTR6 & 6     & 44    & FLTR4 \\
    \rowcolor[rgb]{ 1,  .78,  .808} \textcolor[rgb]{ .612,  0,  .024}{FLTR16} & \textcolor[rgb]{ .612,  0,  .024}{16} & \cellcolor[rgb]{ 1,  1,  1}43 & \cellcolor[rgb]{ 1,  1,  1}FLTR3 \\
    FLTR4 & 44    & 6     & FLTR6 \bigstrut[b]\\
    \bottomrule
\end{NiceTabular}

\end{document}

enter image description here

js bibra
  • 21,280
3

By replacing {tabular*} by {NiceTabular*} of nicematrix with the key colortbl-like, here is what you obtain:

\documentclass{article}
\usepackage{float}
\usepackage{xcolor}
\usepackage{nicematrix}
\usepackage{bigstrut}
\definecolor{LightGray}{gray}{0.9}

\begin{document}

\begin{table}[H] \centering \caption{Not too good looking table here.\label{tab:table1}} \footnotesize % Table generated by Excel2LaTeX from sheet 'Sheet2' \begin{NiceTabular}{\textwidth}{c @{\extracolsep{\fill}} c|ccc}[colortbl-like] %\begin{tabular}{cc|cc} %\toprule \hline \multicolumn{2}{c|}{\textbf{Test1}} & \multicolumn{2}{c}{\textbf{Test2}} \bigstrut[t]\ Test11 & Test12 & Test13 & Test14 \bigstrut[b]\ %\midrule \hline FLTR2 & 2 & 40 & FLTR0 \bigstrut[t]\ FLTR0 & 80 & 80 & FLTR0 \ \rowcolor[rgb]{ 1, .78, .808} \textcolor[rgb]{ .612, 0, .024}{FLTR0} & \textcolor[rgb]{ .612, 0, .024}{160} & \textcolor[rgb]{ .612, 0, .024}{204} & \textcolor[rgb]{ .612, 0, .024}{FLTR4} \ FLTR6 & 6 & 44 & FLTR4 \ \rowcolor[rgb]{ 1, .78, .808} \textcolor[rgb]{ .612, 0, .024}{FLTR16} & \textcolor[rgb]{ .612, 0, .024}{16} & \cellcolor[rgb]{ 1, 1, 1}43 & \cellcolor[rgb]{ 1, 1, 1}FLTR3 \ FLTR4 & 44 & 6 & FLTR6 \bigstrut[b]\ \hline %\bottomrule \end{NiceTabular}% %\end{tabular} \end{table}

\end{document}

Output of the above code

Remark that the preamble of your tabular announces 5 columns but you have used only 4.

F. Pantigny
  • 40,250