4

I have the following table in Microsoft Excel:

enter image description here

I tried to reproduce the same in LaTeX:

\documentclass[border=1cm]{standalone}
\usepackage{amsmath}
\usepackage[table,xcdraw]{xcolor}

\begin{document}
\begin{tabular}{l
    |>{\columncolor[HTML]{B9EDFC}}r 
    >{\columncolor[HTML]{B9EDFC}}r 
    |>{\columncolor[HTML]{FFFC9E}}r 
    >{\columncolor[HTML]{FFFC9E}}r 
    |>{\columncolor[HTML]{FFCE93}}r 
    >{\columncolor[HTML]{FFCE93}}r 
    |>{\columncolor[HTML]{CBCEFB}}r 
    >{\columncolor[HTML]{CBCEFB}}r |}
& \multicolumn{2}{c}{\cellcolor[HTML]{B9EDFC}\texttt{I}} & \multicolumn{2}{c}{\cellcolor[HTML]{FFFC9E}\text{II}} & \multicolumn{2}{c}{\cellcolor[HTML]{FFCE93}\text{III}} & \multicolumn{2}{c}{\cellcolor[HTML]{CBCEFB}\text{III}} \\ \hline
& \multicolumn{1}{c}{\cellcolor[HTML]{B9EDFC}$x$} & \multicolumn{1}{c}{\cellcolor[HTML]{B9EDFC}$y$} & \multicolumn{1}{c}{\cellcolor[HTML]{FFFC9E}$x$} & \multicolumn{1}{c}{\cellcolor[HTML]{FFFC9E}$y$} & \multicolumn{1}{c}{\cellcolor[HTML]{FFCE93}$x$} & \multicolumn{1}{c}{\cellcolor[HTML]{FFCE93}$y$} & \multicolumn{1}{c}{\cellcolor[HTML]{CBCEFB}$x$} & \multicolumn{1}{c}{\cellcolor[HTML]{CBCEFB}$y$} \\ \hline
& $10.00$ & $8.04$ & $10.00$ & $9.14$ & $10.00$ & $7.46$ & \cellcolor[HTML]{CBCEFB}$8.00$ & $6.58$ \\
& $8.00$ & $6.95$ & $8.00$ & $8.14$ & $8.00$ & $6.77$ & $8.00$ & $5.76$ \\
& $13.00$ & $7.58$ & $13.00$ & $8.74$ & $13.00$ & $12.74$ & $8.00$ & $7.71$ \\
& $9.00$ & $8.81$ & $9.00$ & $8.77$ & $9.00$ & $7.11$ & $8.00$ & $8.84$ \\
& $11.00$ & $8.33$ & $11.00$ & $9.26$ & $11.00$ & $7.81$ & $8.00$ & $8.47$ \\
& $14.00$ & $9.96$ & $14.00$ & $8.10$ & $14.00$ & $8.84$ & $8.00$ & $7.04$ \\
& $6.00$ & $7.24$ & $6.00$ & $6.13$ & $6.00$ & $6.08$ & $8.00$ & $5.25$ \\
& $4.00$ & $4.26$ & $4.00$ & $3.10$ & $4.00$ & $5.39$ & $19.00$ & $12.50$ \\
& $12.00$ & $10.24$ & $12.00$ & $9.13$ & $12.00$ & $8.15$ & $8.00$ & $5.56$ \\
& $7.00$ & $4.82$ & $7.00$ & $7.26$ & $7.00$ & $6.42$ & $8.00$ & $7.91$ \\
& $5.00$ & $5.68$ & $5.00$ & $4.74$ & $5.00$ & $5.73$ & $8.00$ & $6.89$ \\ \hline
\hline
Mean & $9.0000$ & $7.5009$ & $9.0000$ & $7.5009$ & $9.0000$ & $7.5000$ & \cellcolor[HTML]{CBCEFB}$9.0000$ & $7.5009$ \\
Median & $9.0000$ & $7.5800$ & $9.0000$ & $8.1400$ & $9.0000$ & $7.1100$ & \cellcolor[HTML]{CBCEFB}$8.0000$ & $7.0400$ \\
Variance & $10.0000$ & $3.7521$ & $10.0000$ & $3.7524$ & $10.0000$ & $3.7478$ & \cellcolor[HTML]{CBCEFB}$10.0000$ & $3.7484$ \\
Skewness & $0.0000$ & $-0.0650$ & $0.0000$ & $-1.3158$ & $0.0000$ & $1.8555$ & \cellcolor[HTML]{CBCEFB}$3.3166$ & $1.5068$ \\
Kurtosis & $1.2000$ & $-0.5349$ & $-1.2000$ & $0.8461$ & $1.2000$ & $4.3841$ & \cellcolor[HTML]{CBCEFB}$11.0000$ & $3.1513$ \\
Correlation & $0.8164$ &  & $0.8162$ &  & $0.8164$ &  & \cellcolor[HTML]{CBCEFB}$0.8165$ & \\ \hline
\end{tabular}
\end{document}

That gives:

enter image description here

But as you can see i failed for some minor details (borders especially!):

Any help would be greatly appreciated

2
  • 1
    How is this question related to latex3?
    – cabohah
    Commented May 21 at 12:56
  • 2
    BTW: Not to use borders would be a good idea and an improvement. See, e.g., the booktabs manual for more information. BTW: For colorful tables, package tabularray is very useful, i.e., if you decide to use vertical lines and horizontal lines.
    – cabohah
    Commented May 21 at 12:59

3 Answers 3

2

Some PDF viewers (Adobe Reader) tend to hide borders when showing tables with colored cells. If you zoom in the page, you'll see the missing ones (except for the first one, which is missing naturally). But for this table I'd suggest to use tabularray and omit all formatting from the cells (put it into the table preamble). The following code

\documentclass{article}
\usepackage{amsmath}
\usepackage[table,xcdraw]{xcolor}
\usepackage{tabularray}

\definecolor{c1}{HTML}{B9EDFC}
\definecolor{c2}{HTML}{FFFC9E}
\definecolor{c3}{HTML}{FFCE93}
\definecolor{c4}{HTML}{CBCEFB}

\begin{document}
\begin{tblr}{
    colspec={l|rr|rr|rr|rr|},
    column{2,3}={c1},
    column{4,5}={c2},
    column{6,7}={c3},
    column{8,9}={c4},
    cell{1}{2,4,6,8}={c=2}{halign=c,font=\ttfamily},
    cell{2}{1-Z}={halign=c},
    row{2-Z}={mode=math},
    column{1}={mode=text},
    rowsep=0pt,
    hline{1,2,3,Z}={2-Z}{},
    hline{14}={1}{2-Z}{},
    hline{14}={2}{2-Z}{},
}
& I & & II & & III & & IV \\
& x & y & x & y & x & y & x & y \\
& 10.00 & 8.04 & 10.00 & 9.14 & 10.00 & 7.46 & 8.00 & 6.58 \\
& 8.00 & 6.95 & 8.00 & 8.14 & 8.00 & 6.77 & 8.00 & 5.76 \\
& 13.00 & 7.58 & 13.00 & 8.74 & 13.00 & 12.74 & 8.00 & 7.71 \\
& 9.00 & 8.81 & 9.00 & 8.77 & 9.00 & 7.11 & 8.00 & 8.84 \\
& 11.00 & 8.33 & 11.00 & 9.26 & 11.00 & 7.81 & 8.00 & 8.47 \\
& 14.00 & 9.96 & 14.00 & 8.10 & 14.00 & 8.84 & 8.00 & 7.04 \\
& 6.00 & 7.24 & 6.00 & 6.13 & 6.00 & 6.08 & 8.00 & 5.25 \\
& 4.00 & 4.26 & 4.00 & 3.10 & 4.00 & 5.39 & 19.00 & 12.50 \\
& 12.00 & 10.24 & 12.00 & 9.13 & 12.00 & 8.15 & 8.00 & 5.56 \\
& 7.00 & 4.82 & 7.00 & 7.26 & 7.00 & 6.42 & 8.00 & 7.91 \\
& 5.00 & 5.68 & 5.00 & 4.74 & 5.00 & 5.73 & 8.00 & 6.89 \\
Mean & 9.0000 & 7.5009 & 9.0000 & 7.5009 & 9.0000 & 7.5000 & 9.0000 & 7.5009 \\
Median & 9.0000 & 7.5800 & 9.0000 & 8.1400 & 9.0000 & 7.1100 & 8.0000 & 7.0400 \\
Variance & 10.0000 & 3.7521 & 10.0000 & 3.7524 & 10.0000 & 3.7478 & 10.0000 & 3.7484 \\
Skewness & 0.0000 & -0.0650 & 0.0000 & -1.3158 & 0.0000 & 1.8555 & 3.3166 & 1.5068 \\
Kurtosis & 1.2000 & -0.5349 & -1.2000 & 0.8461 & 1.2000 & 4.3841 & 11.0000 & 3.1513 \\
Correlation & 0.8164 &  & 0.8162 &  & 0.8164 &  & 0.8165 & \\
\end{tblr}
\end{document}

renders the following result, which is perfectly shown in Adobe Reader:

enter image description here

0
2

I don't know that the colors are all that helpful. I would go with a booktabs approach, taking out the colors and vertical lines:

\documentclass[border=1cm]{standalone}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage[table,xcdraw]{xcolor}

\begin{document}
\begin{tabular}{r *8{S[table-format=-2.2,table-auto-round]} }\toprule
& \multicolumn{2}{c}{I} & \multicolumn{2}{c}{II} & \multicolumn{2}{c}{III} & \multicolumn{2}{c}{IV} \\ \cmidrule(lr){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7}\cmidrule(l){8-9}
& {$x$} & {$y$} & {$x$} & {$y$} & {$x$} & {$y$} & {$x$} & {$y$} \\
& 10.00 & 8.04 & 10.00 & 9.14 & 10.00 & 7.46 & 8.00 & 6.58 \\
& 8.00 & 6.95 & 8.00 & 8.14 & 8.00 & 6.77 & 8.00 & 5.76 \\
& 13.00 & 7.58 & 13.00 & 8.74 & 13.00 & 12.74 & 8.00 & 7.71 \\
& 9.00 & 8.81 & 9.00 & 8.77 & 9.00 & 7.11 & 8.00 & 8.84 \\
& 11.00 & 8.33 & 11.00 & 9.26 & 11.00 & 7.81 & 8.00 & 8.47 \\
& 14.00 & 9.96 & 14.00 & 8.10 & 14.00 & 8.84 & 8.00 & 7.04 \\
& 6.00 & 7.24 & 6.00 & 6.13 & 6.00 & 6.08 & 8.00 & 5.25 \\
& 4.00 & 4.26 & 4.00 & 3.10 & 4.00 & 5.39 & 19.00 & 12.50 \\
& 12.00 & 10.24 & 12.00 & 9.13 & 12.00 & 8.15 & 8.00 & 5.56 \\
& 7.00 & 4.82 & 7.00 & 7.26 & 7.00 & 6.42 & 8.00 & 7.91 \\
& 5.00 & 5.68 & 5.00 & 4.74 & 5.00 & 5.73 & 8.00 & 6.89 \\ \midrule
Mean & 9.0000 & 7.5009 & 9.0000 & 7.5009 & 9.0000 & 7.5000 & 9.0000 & 7.5009 \\
Median & 9.0000 & 7.5800 & 9.0000 & 8.1400 & 9.0000 & 7.1100 & 8.0000 & 7.0400 \\
Variance & 10.0000 & 3.7521 & 10.0000 & 3.7524 & 10.0000 & 3.7478 & 10.0000 & 3.7484 \\
Skewness & 0.0000 & -0.0650 & 0.0000 & -1.3158 & 0.0000 & 1.8555 & 3.3166 & 1.5068 \\
Kurtosis & 1.2000 & -0.5349 & -1.2000 & 0.8461 & 1.2000 & 4.3841 & 11.0000 & 3.1513 \\
Correlation & 0.8164 &  & 0.8162 &  & 0.8164 &  & 0.8165 & \\ \bottomrule
\end{tabular}
\end{document}

output table

I've also used the siunitx package to get the numbers to align on the decimal point, and rounded the calculations (since it doesn't make sense to have calculations that are more accurate than the input).

2

Here is a way to create that table with {NiceTabular} of nicematrix. The rules won't seem to vanish, and you won't see thin white lines in the colored panels, whatever PDF viewer you use.

However, I recommend a design in the spirit of booktabs (see Teepeemm's answer).

\documentclass[border=1cm]{standalone}
\usepackage[xcdraw]{xcolor}

\usepackage{nicematrix,tikz}

\begin{document}

$\begin{NiceArray}{|rr|rr|rr|rr|}[cell-space-limits=2pt,first-col]
\CodeBefore
  \columncolor[HTML]{B9EDFC}{1,2}
  \columncolor[HTML]{FFFC9E}{3,4}
  \columncolor[HTML]{FFCE93}{5,6}
  \columncolor[HTML]{CBCEFB}{7,8}
\Body
\Hline
& \Block{1-2}{\text{I}} && \Block{1-2}{\text{II}} && \Block{1-2}{\text{III}} && \Block{1-2}{\text{III}} \\ \Hline
& \Block[c]{}{x} & \Block[c]{}{y} & \Block[c]{}{x} & \Block[c]{}{y} & \Block[c]{}{x} & \Block[c]{}{y} & \Block[c]{}{x} & \Block[c]{}{y} \\ \Hline
& 10.00 & 8.04 & 10.00 & 9.14 & 10.00 & 7.46 & 8.00 & 6.58 \\
& 8.00 & 6.95 & 8.00 & 8.14 & 8.00 & 6.77 & 8.00 & 5.76 \\
& 13.00 & 7.58 & 13.00 & 8.74 & 13.00 & 12.74 & 8.00 & 7.71 \\
& 9.00 & 8.81 & 9.00 & 8.77 & 9.00 & 7.11 & 8.00 & 8.84 \\
& 11.00 & 8.33 & 11.00 & 9.26 & 11.00 & 7.81 & 8.00 & 8.47 \\
& 14.00 & 9.96 & 14.00 & 8.10 & 14.00 & 8.84 & 8.00 & 7.04 \\
& 6.00 & 7.24 & 6.00 & 6.13 & 6.00 & 6.08 & 8.00 & 5.25 \\
& 4.00 & 4.26 & 4.00 & 3.10 & 4.00 & 5.39 & 19.00 & 12.50 \\
& 12.00 & 10.24 & 12.00 & 9.13 & 12.00 & 8.15 & 8.00 & 5.56 \\
& 7.00 & 4.82 & 7.00 & 7.26 & 7.00 & 6.42 & 8.00 & 7.91 \\
& 5.00 & 5.68 & 5.00 & 4.74 & 5.00 & 5.73 & 8.00 & 6.89 \\ 
\Hline[tikz=very thick]
\text{Mean} & 9.0000 & 7.5009 & 9.0000 & 7.5009 & 9.0000 & 7.5000 & 9.0000 & 7.5009 \\
\text{Median} & 9.0000 & 7.5800 & 9.0000 & 8.1400 & 9.0000 & 7.1100 & 8.0000 & 7.0400 \\
\text{Variance} & 10.0000 & 3.7521 & 10.0000 & 3.7524 & 10.0000 & 3.7478 & 10.0000 & 3.7484 \\
\text{Skewness} & 0.0000 & -0.0650 & 0.0000 & -1.3158 & 0.0000 & 1.8555 & 3.3166 & 1.5068 \\
\text{Kurtosis} & 1.2000 & -0.5349 & -1.2000 & 0.8461 & 1.2000 & 4.3841 & 11.0000 & 3.1513 \\
\text{Correlation} & 0.8164 &  & 0.8162 &  & 0.8164 &  & 0.8165 & \\ \Hline
\end{NiceArray}$

\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 .