# Coloring only Part of a Row in a Table

Let's say you have this table:

I want to highlight only parts of certain rows. For example, I want to highlight the fourth row, but only highlight this row for the first six columns. Then for the last six columns, I want to highlight the last row.

I know you can use \cellcolor{}, but it seems rather tedious to do this for every single column. Is there a more elegant way to color only specific cells from a row?

P.S. I know there is extra space toward the end of the table, I have to fix it :)

Here is the code:

\documentclass{article}

\usepackage{tabularx}
\usepackage{multirow}
\usepackage{pdflscape}
\usepackage{booktabs}
\usepackage[margin = 1in]{geometry}

\begin{document}

\begin{table}[ht]

\centering
\def\arraystretch{1.5}

\begin{tabularx}{\linewidth}{lccccclccccc} \toprule
& \multicolumn{5}{c}{$K$-means Clustering} &  &  \multicolumn{5}{c}{Hierarchical Clustering} \\ \cmidrule(r){2-6} \cmidrule(lr){8-12}
& $\lambda$ & Misclass. & Deviance & Sig. Coef. & Sig. Groups & & $\lambda$ & Misclass. & Deviance & Sig. Coef. & Sig. Groups\\ \midrule
$\alpha = 1$   & $0.05873092$ & $11/31$ & $1.730157$ & $1$ & $0$ & $\alpha = 1$   & $0.05873092$ & $11/31$ & $1.741681$ & $1$ & $0$ \\
$\alpha = 0.8$ & $0.0281825$ & $8/31$ & $1.735783$ & $7$ & $1$ & $\alpha = 0.8$  & $0.02822086$ & $11/31$ & $1.732505$ & $108$ & $1$ \\
$\alpha = 0.6$ & $0.02425659$ & $8/31$ & $1.711518$ & $26$ & $2$ & $\alpha = 0.6$ & $0.02380529$ & $11/31$ & $1.727994$ & $226$ & $1$ \\
$\alpha = 0.4$ & $0.021494$ & $7/31$ & $1.706627$ & $29$ & $2$ & $\alpha = 0.4$   & $0.0214516$ & $11/31$ & $1.726991$ & $319$ & $1$ \\
$\alpha = 0.2$ & $0.02001998$ & $7/31$ & $1.70708$ & $32$ & $2$ & $\alpha = 0.2$ & $0.02007049$ & $11/31$ & $1.72622$ & $393$ & $1$ \\
$\alpha = 0$   & $0.01946719$ & $7/31$ & $1.707758$ & $36$ & $2$ & $\alpha = 0$   & $0.01921006$ & $11/31$ & $1.725663$ & $472$ & $1$ \\ \bottomrule
\end{tabularx}

\end{table}

\end{document}


• The MWE cannot produce the image showed. There are several errors, as the lack of booktabs package, not closing the float options and more. Can you fix this first, please? BTW, imho the bottom rule is perfect as is, but in texdoc booktabs you have how to make rules with another vertical spacing. – Fran Jul 28 at 7:54
• @Fran: I suppose that the statement " extra space toward the end of the table" does refer to horizontal end of the table (lines continuing to the right margin) instead of the vertical end of the table. – leandriis Jul 28 at 7:57
• @Aiden Kenny: In addition to the previous comment, the landscape environment is misssing. If I add it, your table is wider than the textwidth. So your MWE must either be missing some font size settings or you changed the margins using for example geometry. Please also add these settings. Could you please also add teh reason why you used math mode for all the numbers? – leandriis Jul 28 at 8:00
• @leandriis The bottom line end well in the image. It is not clear what we had to solve in this example, beside a method to coloring specific a matrix of n × m cells with only one command. – Fran Jul 28 at 8:13
• @AidenKenny: Thanks for updating the MWE. The siunitx package (that I used in the fourth table of my answer) can also handle negative numbers. Maybe you want to use that. You can of course incorporate that in all of the other layouts as well. – leandriis Jul 28 at 18:55

1   & 0.05873092 & 11/31   & 1.730157 & 1  & 0  \\
0.8 & 0.0281825  & 8/31    & 1.735783 & 7  & 1  \\
0.6 & 0.02425659 & 8/31    & 1.711518 & 26 & 2  \\
0.4 & 0.021494   & 7/31    & 1.706627 & 29 & 2  \\
0.2 & 0.02001998 & 7/31    & 1.70708  & 32 & 2  \\
\rowcolor{lightgray}
0   & 0.01946719 & 7/31    & 1.707758 & 36 & 2  \\
\bottomrule
\end{tabular}
\end{table}
\end{document}


(red lines indicate text borders)