[Tex/LaTex] Formatting tabu table

tablestabu

For this MWE

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}

\RequirePackage{mathbbol}
\newcommand{\hmmax}{0}
\newcommand{\bmmax}{2}
\RequirePackage{bm}
\RequirePackage[allowlitunits]{siunitx}
\RequirePackage[usenames,dvipsnames,svgnames,table]{xcolor}
\RequirePackage{booktabs}
\RequirePackage{tabu}
\usepackage{diagbox}

\begin{document}

% \newcolumntype Z{X[c]{%
%   S[group-four-digits=true,
%   round-mode=places,
%   round-precision=2,
%   round-integer-to-decimal=true,
%   per-mode=symbol]
%   }}

\newcolumntype{Z}{S[
    tight-spacing=true,
    round-mode=places,
    round-precision=2]}


\begin{tabu} to \textwidth {ZZZZZZ}
\toprule
\rowfont[c]\bfseries
\diagbox[width=8.5em]{method}{missing} & 0\% & 5\% & 10\% & 20\% & 50\% \\
\midrule
KNN                     & 0.0 & 0.123456    & 0.123456 & 0.123456 & 0.123456 \\
MICE                    & 0.0 & 0.123456    & 0.123456 & 0.123456 & 0.123456 \\
SimpleFill              & 0.0 & 0.123456    & 0.123456 & 0.123456 & 0.123456 \\
SoftImpute              & 0.0 & 0.123456    & 0.123456 & 0.123456 & 0.123456 \\
\bottomrule
\end{tabu}

\end{document}

I would like the table to be like this:

  • the first column left aligned
  • the column headers to be center aligned
  • the body columns (0.123456) to be center aligned at the dot with siunitx
  • the second column should be half as wide as the following columns
  • the first column should be twice as wide as the later columns (and four times as wide as the second column). For that I had tried X[2] and X[0.5], but I don't manage to transfer this to the Z columns.

I tried two versions of the definition of Z, but it does not work. How do I do that?

Best Answer

Like this?

enter image description here

Edit: From given MWE I assumed, that the table had to have width of \textwidth and columns had to be in ratio 4:1:2. This can be achieved on two ways:

  • superimpose column type p{<width>} to S column type. This can be done by use of \multicolumn{1}{>{\centering\arraybackslash}p{<width>}{...} for column heaser. <width> is calculated from \textwidth considering width ratio of columns (first more complicated solution) with

    \newlength\colwidth <-- new length used for determining column widths \setlength\colwidth{\dimexpr0.077\linewidth-\tabcolsep+\arrayrulewidth\relax} <-- calculation of base column width

    Base width is in p{<width>} multiplied according to desired ratio of columns widths

  • determine S columns width with option `table-column-width= where the base column width is calculated on the same way as above (second example)

First solution:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}

    \RequirePackage{mathbbol}
    \newcommand{\hmmax}{0}
    \newcommand{\bmmax}{2}
\usepackage{bm}
\usepackage[allowlitunits]{siunitx}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{booktabs,tabu}
\usepackage{diagbox}

\newcolumntype{Z}{S[
    table-format=1.2,% added
    %tight-spacing=true,
    round-mode=places,
    round-precision=2]}
\newlength\colwidth% added
\setlength\colwidth{\dimexpr0.077\linewidth-\tabcolsep+\arrayrulewidth\relax}% added
\newcommand\mcp[2]{\multicolumn{1}{>{\centering\arraybackslash}p{#1\colwidth}}{#2}}% added

\begin{document}
\noindent
\begin{tabu} to \textwidth {p{4\colwidth} *{5}{Z}}% changed
    \toprule
\rowfont[c]\bfseries
\diagbox[width=4\colwidth]{method}{missing} % changed
                & \mcp{1}{0\%}
                      &\mcp{2}{5\%} & \mcp{2}{10\%} 
                                              & \mcp{2}{20\%} 
                                                          & \mcp{2}{50\%} \\ % changed
    \midrule
KNN             & 0.0 & 0.123456    & 0.123456 & 0.123456 & 0.123456 \\
MICE            & 0.0 & 0.123456    & 0.123456 & 0.123456 & 0.123456 \\
SimpleFill      & 0.0 & 0.123456    & 0.123456 & 0.123456 & 0.123456 \\
SoftImpute      & 0.0 & 0.123456    & 0.123456 & 0.123456 & 0.123456 \\
    \bottomrule
\end{tabu}
\end{document}

Second solution, where had not be used tabu macro \rowfont[c]\bfseries due to incompatibility with S type headers and therefore columns headers are not in bold face:

enter image description here

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}

    \RequirePackage{mathbbol}
    \newcommand{\hmmax}{0}
    \newcommand{\bmmax}{2}
\usepackage{bm}
\usepackage[allowlitunits]{siunitx}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{booktabs,tabu}
\usepackage{diagbox}

\newlength\colwidth% added
\setlength\colwidth{\dimexpr0.077\linewidth-\tabcolsep+\arrayrulewidth\relax}% added

\begin{document}
\noindent
\begin{tabu} to \textwidth {X % changed
                            S[table-column-width=\colwidth,% added
                              table-format=1.1]% added
                       *{4}{S[table-column-width=2\colwidth,% added
                              table-format=1.2,% added
                              round-mode=places,
                              round-precision=2]}
                            }
    \toprule
%\rowfont[c]\bfseries
\diagbox[width=4\colwidth]{method}{missing}
            & \SI{0}{\%} & \SI{5}{\%}  & \SI{10}{\%} & \SI{20}{\%} & \SI{50}{\%}  \\
    \midrule
KNN         &   0.0         &   0.123456    &   0.123456    &   0.123456    & 0.123456  \\
MICE        &   0.0         &   0.123456    &   0.123456    &   0.123456    & 0.123456  \\
SimpleFill  &   0.0         &   0.123456    &   0.123456    &   0.123456    & 0.123456  \\
SoftImpute  &   0.0         &   0.123456    &   0.123456    &   0.123456    & 0.123456  \\
    \bottomrule
\end{tabu}
\end{document}
Related Question