6

Yet another question, this time with coloring the table. This is the code for the table I got with your help here:

\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{table}[h]
\begin{tabular}{*{6}{|c}|}
\cline{3-6}
\multicolumn{2}{c|}{} & M1          & M2            & M3           & M4            \\ \hline
\multirow{3}{*}{A1}  & a1 \quad a2 & 0,3 \quad 0,5 & 0,1 \quad 0,2 & 0,3 \quad 0,8 & 0,4 \quad 0,7 \\
                     & b           & 3             & 2             & 0,1           & 1             \\
                     & c           & 1             & 0             & -5            & 3             \\ \hline
\multirow{3}{*}{A2}  & a1 \quad a2 & 0,3 \quad 0,5 & 0,1 \quad 0,2 & 4 \quad 2     & 0,1 \quad 0,2 \\
                     & b           & 3             & 2             & 0,1           & 1             \\
                     & c           & 1             & 0             & -5            & -2            \\ \hline
 \multirow{3}{*}{A3} & a1 \quad a2 & 0,3 \quad 0,5 & 0,1 \quad 0,2 & 4 \quad 2     & 0,1 \quad 0,2 \\
                     & b           & 3             & 2             & 0,1           & 1             \\
                     & c           & 1             & 0             & -5            & -2            \\ \hline
\end{tabular}
\end{table}
\end{document} 

How can I color the table to look like it as follows? Many thanks in advance.

http://i.imgur.com/AY3XwgY.png

3 Answers 3

6

A bit complicated...

\documentclass{article}
\usepackage{multirow}
\usepackage[table]{xcolor}
\begin{document}
\begin{table}[h]
\begin{tabular}{|*{2}{>{\columncolor{black}\color{white}}c}*{4}{|c}|}
\cline{3-6}
\rowcolor{black}\multicolumn{2}{c|}{\cellcolor{white}{}} 
                     & \textcolor{white}{M1}          
                     & \textcolor{white}{M2}            
                     & \textcolor{white}{M3}           
                     & \textcolor{white}{M4}            \\ \hline
                     & a1 \quad a2 & 0,3 \quad 0,5 & 0,1 \quad 0,2 & 0,3 \quad 0,8 & 0,4 \quad 0,7 \\
                     & b           & 3             & 2             & 0,1           & 1             \\
\multirow{-3}{*}{A1} & c           & 1             & 0             & -5            & 3             \\ \hline  
                     & a1 \quad a2 & 0,3 \quad 0,5 & 0,1 \quad 0,2 & 4 \quad 2     & 0,1 \quad 0,2 \\
                     & b           & 3             & 2             & 0,1           & 1             \\
\multirow{-3}{*}{A2} & c           & 1             & 0             & -5            & -2            \\ \hline
                     & a1 \quad a2 & 0,3 \quad 0,5 & 0,1 \quad 0,2 & 4 \quad 2     & 0,1 \quad 0,2 \\
                     & b           & 3             & 2             & 0,1           & 1             \\
\multirow{-3}{*}{A3} & c           & 1             & 0             & -5            & -2            \\ \hline
\end{tabular}
\end{table}
\end{document} 

enter image description here

4

A solution with the makecell (allows for line breaks in cells), cellspace (better vertical spacing above and below cells) and hhline(allows for multicoloured hlines):

\documentclass{article}

\usepackage{amsmath}
\usepackage{multirow, makecell, colortbl, hhline}
\usepackage{xcolor}
\usepackage{cellspace}
\setlength\cellspacetoplimit{3pt}
\setlength\cellspacebottomlimit{3pt}

\begin{document}
\begin{table}[h]
\setlength\tabcolsep{10pt}\setlength\arrayrulewidth{1pt}
\sffamily\bfseries
\begin{tabular}{>{\columncolor{black}\color{white}}c!{\color{white}\vline width1pt} >{\columncolor{black}\color{white}}c *{4}{Sc|}}
\cline{3-6}
\rowcolor{black} \multicolumn{2}{c|}{\cellcolor{white}} & \color{white}M1 &\color{white} M2 &\color{white} M3 & \color{white}M4 \\
\hhline{>{\arrayrulecolor{white}}-->{\arrayrulecolor{black}}|----}
\makebox[1cm]{A1}& \makecell{a1 \quad a2\\b\\c} & \makecell{0,3 \quad 0,5\\3\\1} &\makecell{ 0,1 \quad 0,2\\2\\0} &\makecell{ 0,3 \quad 0,8\\0.1\\$ \boldsymbol - $5} & \makecell{0,4 \quad 0,7\\1\\3} \\
\hhline{>{\arrayrulecolor{white}}-->{\arrayrulecolor{black}}----}
A2 & \makecell{a1 \quad a2\\b\\c} & \makecell{0,3 \quad 0,5\\3\\1} & \makecell{0,1 \quad 0,2\\2\\0} & \makecell{4 \quad 2\\0.1\\$ \boldsymbol - $5} & \makecell{0,1 \quad 0,2\\1\\-2} \\
\hhline{>{\arrayrulecolor{white}}-->{\arrayrulecolor{black}}----}
A3 & \makecell{a1 \quad a2\\b\\c} & \makecell{0,3 \quad 0,5\\3\\1} & \makecell{0,1 \quad 0,2\\2\\0} & \makecell{4 \quad 2\\0.1\\$\boldsymbol - $5} & \makecell{0,1 \quad 0,2\\1\\$\boldsymbol - $2} \\
\hhline{>{\arrayrulecolor{white}}-->{\arrayrulecolor{black}}----}
\end{tabular}
\end{table}
\end{document} 

enter image description here

2

With {NiceTabular} of nicematrix. We won't see thin white lines, whatever PDF viewer you use.

\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}

\begin{document}

\begin{table}
\begin{NiceTabular}{*{2}{>{\columncolor{black}\color{white}\sffamily}c}cccc}[vlines,corners=NW,colortbl-like]
\rowcolor{black}\RowStyle{\color{white}\sffamily}
                &             & M1            & M2            & M3            & M4            \\ \Hline
\Block{3-1}{A1} & a1 \quad a2 & 0,3 \quad 0,5 & 0,1 \quad 0,2 & 0,3 \quad 0,8 & 0,4 \quad 0,7 \\
                & b           & 3             & 2             & 0,1           & 1             \\
                & c           & 1             & 0             & -5            & 3             \\ \Hline  
\Block{3-1}{A2} & a1 \quad a2 & 0,3 \quad 0,5 & 0,1 \quad 0,2 & 4 \quad 2     & 0,1 \quad 0,2 \\
                & b           & 3             & 2             & 0,1           & 1             \\
                & c           & 1             & 0             & -5            & -2            \\ \Hline
\Block{3-1}{A3} & a1 \quad a2 & 0,3 \quad 0,5 & 0,1 \quad 0,2 & 4 \quad 2     & 0,1 \quad 0,2 \\
                & b           & 3             & 2             & 0,1           & 1             \\
                & c           & 1             & 0             & -5            & -2            \\ \Hline
\end{NiceTabular}
\end{table}

\end{document} 

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .