3

Let's say you have this table:

enter image description here

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}

EDIT made edits to MWE.

  • 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
  • 1
    @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
6

As far as I know, the colortbl package only offers methods to easily color a whole row or a whole column. If you only want to color parts of it, there is the \cellcolor command. If you don't want to repeat it with the same color definition over and over again, you could use an abbreviation. (Might also come in handy, if you later on decide that you want to change the color). Please also note that I have removed the superfluous $ that surrounded every number in your original example code. Also, I guessed the settings for the geometry package as they were not given in your example.

enter image description here

\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{tabularx}
\usepackage{pdflscape}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\newcommand{\mycc}{\cellcolor{lightgray}}

\begin{document}
\begin{landscape}
\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  \\ 
        \mycc$\alpha = 0.4$ & \mycc 0.021494  & \mycc 7/31  & \mycc 1.706627  & \mycc 29  & \mycc 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  & \mycc$\alpha = 0$   &  \mycc 0.01921006  &  \mycc 11/31  &  \mycc 1.725663  &  \mycc 472  &  \mycc 1  \\ \bottomrule
\end{tabularx}

\end{table}
\end{landscape}
\end{document}

You seem to have already noticed that the horizontal lines continue all the way to the right margin even though the table's contents are not that wide. This is due to your usage of tabularx that I have not corrected in the above MWE. If you want your table to be as wide as the textwidth and are open to a slight redesign of your table, you might be interested in the following MWE (that also just needs one \rowcolor command to color the mentioned cells):

enter image description here

\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{pdflscape}
\usepackage{booktabs}
\usepackage[table]{xcolor}

\begin{document}


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


\end{document}

Starting from this redesign you might also want to rethink if a landscape orientation is really necessary, as your table can fit into the text block of a portrait page (even with standard margins) perfectly fine:

enter image description here

\documentclass{article}
\usepackage{booktabs}
\usepackage[table]{xcolor}

\begin{document}


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



\end{document}

Finally, here is the layout I personally would go for. Here I have used the S type columns from the siunitx package in order to align the numbers with respect to the decimal marker and used >{\(}l<{\)} in order to automatically set the comments of the first column in math mode.

enter image description here

\documentclass{article}

\usepackage{booktabs}
\usepackage{siunitx} 
\usepackage[table]{xcolor}

\begin{document}
\begin{table}[ht]
\centering 
\begin{tabular}{@{\quad}>{\(}l<{\)}S[table-format=1.8]cS[table-format=1.6]S[table-format=3]c} \toprule

                       & $\lambda$ & Misclass. & {Deviance} & {Sig. Coef.} & Sig. Groups\\ \midrule
         \multicolumn{6}{@{}l}{Hierarchical Clustering} \\ 
         \alpha = 1    & 0.05873092 & 11/31 & 1.741681 & 1 & 0 \\ 
         \alpha = 0.8  & 0.02822086 & 11/31 & 1.732505 & 108 & 1 \\ 
         \alpha = 0.6  & 0.02380529 & 11/31 & 1.727994 & 226 & 1 \\ 
         \rowcolor{lightgray}\alpha = 0.4  & 0.0214516  & 11/31 & 1.726991 & 319 & 1 \\ 
         \alpha = 0.2  & 0.02007049 & 11/31 & 1.72622  & 393 & 1 \\
         \alpha = 0    & 0.01921006 & 11/31 & 1.725663 & 472 & 1 \\ \midrule
        \multicolumn{6}{@{}l}{$K$-means Clustering}  \\ 
        \alpha = 1   & 0.05873092 & 11/31   & 1.730157 & 1  & 0  \\ 
        \alpha = 0.8 & 0.0281825  & 8/31    & 1.735783 & 7  & 1  \\ 
        \alpha = 0.6 & 0.02425659 & 8/31    & 1.711518 & 26 & 2  \\ 
        \alpha = 0.4 & 0.021494   & 7/31    & 1.706627 & 29 & 2  \\ 
        \alpha = 0.2 & 0.02001998 & 7/31    & 1.70708  & 32 & 2  \\
        \rowcolor{lightgray}\alpha = 0   & 0.01946719 & 7/31    & 1.707758 & 36 & 2  \\ \bottomrule
\end{tabular}
\end{table}
\end{document}
  • 1
    Choosing a format for a table is always a difficult task. I like having it in landscape since I have other tables that go along with it (not shown in MWE), and in landscape it all fits nicely on one page. I do like the idea of separating the long table into two separate ones! I will experiment with all of your answers and see which one is best :) – Aiden Kenny Jul 28 at 18:55
4

To bunch of examples in @leandriis's answer let me add two more examples:

\documentclass{article}
\usepackage{booktabs,tabularx}
\usepackage{siunitx}
\usepackage[table]{xcolor}
\newcommand{\mycc}{\cellcolor{lightgray}}
\newcommand{\myccs}[2]{\cellcolor{lightgray}{\tablenum[table-format=#1]{#2}}}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}

\begin{document}
\begin{table}[ht]
\small
\caption{With use of \texttt{siunitx} and its option \texttt{tablenum} and \texttt{tabularx}}
\setlength\tabcolsep{2.4pt}
\centering
\begin{tabularx}{\linewidth}{@{}
                            >{\columncolor{white}[0pt][\tabcolsep]}l
                            S[table-format=1.8]
                            r
                            S[table-format=1.6]
                            S[table-format=3]
                            >{\centering}X
                            @{\quad}
                            l
                            S[table-format=1.8]
                            r
                            S[table-format=1.6]
                            S[table-format=3]
                            >{\columncolor{white}[\tabcolsep][0pt]\centering\arraybackslash}X
                            @{}}
                    \toprule
    & \multicolumn{5}{c}{$K$-means Clustering} &  
        &  \multicolumn{5}{c}{Hierarchical Clustering} \\ 
    \cmidrule(r){2-6} \cmidrule(r){8-12}
$\alpha$    & $\lambda$ & MC & {Deviance} & {SC} & SG   &
$\alpha$    & $\lambda$ & MC & {Deviance} & {SC} & SG   \\
    \midrule
1   &  0.05873092  &  11/31  &  1.730157  &   1 &  0  & 
1   &  0.05873092  &  11/31  &  1.741681  &   1 &  0    \\
0.8 &  0.0281825   &   8/31  &  1.735783  &   7 &  1  & 
0.8 &  0.02822086  &  11/31  &  1.732505  & 108 &  1    \\
0.6 &  0.02425659  &   8/31  &  1.711518  &  26 &  2  & 
0.6 &  0.02380529  &  11/31  &  1.727994  & 226 &  1    \\
\mycc{0.4} 
    & \myccs{1.8}{0.021494}  
        & \mycc{7/31} 
            & \myccs{1.6}{1.706627}  
                & \myccs{3}{29}    
                    & \mycc{2}  &
0.4 &  0.0214516    & 11/31  & 1.726991   & 319 &  1   \\
0.2 &  0.02001998  &  7/31   &  1.70708   &  32 &  2  & 
0.2 &  0.02007049  & 11/31   &  1.72622   & 393 &  1    \\
0   &  0.01946719  &  7/31   &  1.707758  &  36 &  2  & 
\mycc{0}  
    & \myccs{1.8}{0.01921006} 
        & \mycc{11/31}
            & \myccs{1.6}{1.725663}  
                & \myccs{3}{472} 
                    & \mycc{1}  \\ 
\midrule[\heavyrulewidth]
\multicolumn{12}{p{\linewidth}}{
    MC: Missclass.,
    SG: Sig. Coef.,
    SG: Sig. Groups}
\end{tabularx}
\end{table}

\begin{table}[ht]
\caption{With use of \texttt{siunitx} and \texttt{tabular}}
\centering
\begin{tabular}{S[table-format=1.1]
                S[table-format=1.8]
                c
                S[table-format=1.6]
                S[table-format=3]
                c} 
    \toprule
$\alpha$    & $\lambda$ & Misclass. & {Deviance} & {Sig. Coef.} & Sig. Groups\\ 
    \midrule
\multicolumn{6}{@{}l}{Hierarchical Clustering}                          \\
1    & 0.05873092 & 11/31 & 1.741681 & 1   & 0 \\
0.8  & 0.02822086 & 11/31 & 1.732505 & 108 & 1 \\
0.6  & 0.02380529 & 11/31 & 1.727994 & 226 & 1 \\
    \rowcolor{lightgray}
0.4  & 0.0214516  & 11/31 & 1.726991 & 319 & 1 \\
0.2  & 0.02007049 & 11/31 & 1.72622  & 393 & 1 \\
0    & 0.01921006 & 11/31 & 1.725663 & 472 & 1 \\ 
\midrule
\multicolumn{6}{@{}l}{$K$-means Clustering}   \\
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}

enter image description here

(red lines indicate text borders)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.