使用 siunitx 对齐表格中的列

使用 siunitx 对齐表格中的列

我正在尝试摆放桌子,但就是摆不下来。我开始

\documentclass[twoside,twocolumn]{article}

\RequirePackage[allowlitunits]{siunitx}
\RequirePackage[usenames,dvipsnames,svgnames,table]{xcolor}
\RequirePackage{graphicx}
\RequirePackage{pbox}
\RequirePackage{booktabs}
\RequirePackage{tabularx}
\RequirePackage{makecell}
\RequirePackage{multirow}

\begin{document}

\newcolumntype{Z}{S[
    table-format=1.1e0,
    round-mode=places,
    round-precision=1,
    table-number-alignment = center,
    table-text-alignment = center,
    scientific-notation = false,
    exponent-product = \!\cdot\!
]}
\newcolumntype{L}[1]{>{\RaggedRight\hspace{0pt}}p{#1}}

\definecolor{markcolor}{rgb}{0.68, 1.0, 0.18}
\newcommand{\cellmark}{{\cellcolor{markcolor!35}}}

\newcommand*\rot[1]{\rotatebox{90}{#1}}
\begin{table}
    \begin{tabularx}{\linewidth}{lX *{5}{Z}}
    \toprule
                                            &                       & \multicolumn{5}{c}{\makecell{variance of MISE for miss. perc.}} \\[1pt]
                                            & method                & \multicolumn{1}{c}{5\%} & \multicolumn{1}{c}{10\%} & \multicolumn{1}{c}{20\%} & \multicolumn{1}{c}{35\%} & \multicolumn{1}{c}{50\%} \\
    \cmidrule(r{3pt}){1-2}                                          \cmidrule(l{3pt}r{3pt}){3-7}
%   \multirow{3}{*}{\rotatebox[origin=c]{90}{\pbox{2cm}{\centering virtual\\object KDE}}}
    \multirow{3}{*}{\hspace{-3mm}\pbox{2cm}{virt.\\obj.\\KDE}\hspace{-5mm}}
                                            & W-KDE A   & 2.106430081703724 & 2.99138256327109 &  6.22019189157428 & \cellmark 12.7143637624467 & \cellmark 24.0215821367398 \\
                                            & W-KDE B   & 2.020482332996759 & 2.93267563217202 &  6.62668746749268 & 15.7209477041221 & 37.9264764864483 \\
                                            & W-KDE C   & 2.020482332996759 & 2.93267563217202 &  6.62668746749268 & 15.7209477041221 & 37.9264764864483 \\
    \cmidrule(r{3pt}){1-2}                                          \cmidrule(l{3pt}r{3pt}){3-7}
    \multirow{2}{*}{\hspace{-3mm}\rot{approx.}\hspace{-3mm}}
                                            & semi-par. WKDE    & 4.846089118511761 & 17.76414420915205 & 67.55341169341371 & 201.9098935356314 & 410.4734927212821 \\
                                            & tube WKDE         & \cellmark 0.803445339719881 & \cellmark 1.85794823092129  &  \cellmark 5.24700859976549 & 13.5473466123834 & 25.8150534590904 \\
    \cmidrule(r{3pt}){1-2}                                          \cmidrule(l{3pt}r{3pt}){3-7}
    \multirow{3}{*}{\hspace{-3mm}\rot{impute}\hspace{-3mm}}
                                            & MICE+KDE  & 2.480876100687873 & 8.91565707723542 & 35.87726248460741 & 106.5219627628685 & 230.7999722905524 \\
                                            & mean imp.+KDE     & 2.550059117198298 & 9.15200816320511 & 36.90734292678388 & 109.2582602544040 & 226.1052695773782 \\
                                            & SoftImpute+KDE    & 2.992662848031248 & 8.41849589708940 & 26.94407694112752 & 84.2967975360662 & 569.8177021090986 \\
    \bottomrule
    \end{tabularx}
\end{table}

\end{document}

结果是

在此处输入图片描述

我想减少数字列的宽度,以便为方法列留出更多空间。因此,我修改了代码以

\documentclass[twoside,twocolumn]{article}

\RequirePackage[allowlitunits]{siunitx}
\RequirePackage[usenames,dvipsnames,svgnames,table]{xcolor}
\RequirePackage{graphicx}
\RequirePackage{pbox}
\RequirePackage{booktabs}
\RequirePackage{tabularx}
\RequirePackage{makecell}
\RequirePackage{multirow}

\begin{document}

\newcolumntype{Z}[1]{>{\hspace{-3mm}}S[
    #1,
    round-mode=places,
    round-precision=1,
    table-number-alignment = center,
    table-text-alignment = center,
    scientific-notation = false,
    exponent-product = \!\cdot\!,
    table-column-width = 3mm
]<{\hspace{-3mm}}}
\newcolumntype{L}[1]{>{\RaggedRight\hspace{0pt}}p{#1}}

\definecolor{markcolor}{rgb}{0.68, 1.0, 0.18}
% \definecolor{markcolor}{RGB}{213,229,255}
\newcommand{\cellmark}{{\cellcolor{markcolor!35}}}

\newcommand*\rot[1]{\rotatebox{90}{#1}}
% \let\defaulttabcolsep\tabcolsep
% \setlength\tabcolsep{-3mm}
% \setlength{\tabcolsep}{12pt}
\begin{table}
    % \hspace{-\parindent}
%   \setlength\tabcolsep{10pt}
    \begin{tabularx}{\linewidth}{lX Z{table-format=1.1e0} Z{table-format=1.1e0} Z{table-format=1.1e0} Z{table-format=1.1e0} Z{table-format=1.1e0}}
    \toprule
                                            &                       & \multicolumn{5}{c}{\makecell{variance of MISE for miss. perc.}} \\[1pt]
                                            & method                & \multicolumn{1}{c}{5\%} & \multicolumn{1}{c}{10\%} & \multicolumn{1}{c}{20\%} & \multicolumn{1}{c}{35\%} & \multicolumn{1}{c}{50\%} \\
    \cmidrule(r{3pt}){1-2}                                          \cmidrule(l{3pt}r{3pt}){3-7}
%   \multirow{3}{*}{\rotatebox[origin=c]{90}{\pbox{2cm}{\centering virtual\\object KDE}}}
    \multirow{3}{*}{\hspace{-3mm}\pbox{2cm}{virt.\\obj.\\KDE}\hspace{-5mm}}
                                            & W-KDE A   & 2.106430081703724 & 2.99138256327109 &  6.22019189157428 & \cellmark 12.7143637624467 & \cellmark 24.0215821367398 \\
                                            & W-KDE B   & 2.020482332996759 & 2.93267563217202 &  6.62668746749268 & 15.7209477041221 & 37.9264764864483 \\
                                            & W-KDE C   & 2.020482332996759 & 2.93267563217202 &  6.62668746749268 & 15.7209477041221 & 37.9264764864483 \\
    \cmidrule(r{3pt}){1-2}                                          \cmidrule(l{3pt}r{3pt}){3-7}
    \multirow{2}{*}{\hspace{-3mm}\rot{approx.}\hspace{-3mm}}
                                            & semi-par. WKDE    & 4.846089118511761 & 17.76414420915205 & 67.55341169341371 & 201.9098935356314 & 410.4734927212821 \\
                                            & tube WKDE         & \cellmark 0.803445339719881 & \cellmark 1.85794823092129  &  \cellmark 5.24700859976549 & 13.5473466123834 & 25.8150534590904 \\
    \cmidrule(r{3pt}){1-2}                                          \cmidrule(l{3pt}r{3pt}){3-7}
    \multirow{3}{*}{\hspace{-3mm}\rot{impute}\hspace{-3mm}}
                                            & MICE+KDE  & 2.480876100687873 & 8.91565707723542 & 35.87726248460741 & 106.5219627628685 & 230.7999722905524 \\
                                            & mean imp.+KDE     & 2.550059117198298 & 9.15200816320511 & 36.90734292678388 & 109.2582602544040 & 226.1052695773782 \\
                                            & SoftImpute+KDE    & 2.992662848031248 & 8.41849589708940 & 26.94407694112752 & 84.2967975360662 & 569.8177021090986 \\
    \bottomrule
    \end{tabularx}
\end{table}
% \let\tabcolsep\defaulttabcolsep

\end{document}

结果如下

在此处输入图片描述

可以看出,这并没有像我希望的那样奏效。我尝试了很多方法,但都失败了。

另外:我无法使百分比的标题正确对齐。

答案1

像这样吗?

在此处输入图片描述

我所做的修改包括(i)降低的值\tabcolsep,(ii)删除否定\hspace指令,(iii)缩写多行标题,(iv)允许五个数据列具有不同的宽度,以及(v)缩写词后没有多余的空格。

\documentclass[twoside,twocolumn]{article}

\RequirePackage[allowlitunits]{siunitx}
\RequirePackage[usenames,dvipsnames,svgnames,table]{xcolor}
\RequirePackage{graphicx}
\RequirePackage{pbox}
\RequirePackage{booktabs,ragged2e}
\RequirePackage{tabularx}
\RequirePackage{makecell}
\RequirePackage{multirow}

\begin{document}

\newcolumntype{Z}[1]{S[
    table-format=#1.1,  % specify # of digits before dec. marker
    round-mode  =places,
    round-precision=1,
    table-number-alignment = center,
    table-text-alignment   = center,
    scientific-notation    = false,
    exponent-product = {\cdot}]}
\newcolumntype{Y}{>{\RaggedRight\arraybackslash}X}

\definecolor{markcolor}{rgb}{0.68, 1.0, 0.18}
\newcommand{\cellmark}{{\cellcolor{markcolor!35}}}

\newcommand*\rot[1]{\rotatebox{90}{#1}}
\begin{table}
\setlength\tabcolsep{2.5pt}  % default: 6pt
\begin{tabularx}{\linewidth}{@{} l Y Z{1} *{2}{Z{2}} *{2}{Z{3}} @{}}
\toprule
 & & \multicolumn{5}{c@{}}{\makecell{Var.\ of MISE for miss.\ \%}} \\[0.5ex]
 & method & {5\%} & {10\%} & {20\%} & {35\%} & {50\%} \\
\cmidrule(r){1-2} \cmidrule{3-7}
\multirow{3}{*}{\pbox{2cm}{virt.\\obj.\\KDE}}
 & W-KDE A   & 2.106430081703724 & 2.99138256327109 &  6.22019189157428 & \cellmark 12.7143637624467 & \cellmark 24.0215821367398 \\
 & W-KDE B   & 2.020482332996759 & 2.93267563217202 &  6.62668746749268 & 15.7209477041221 & 37.9264764864483 \\
 & W-KDE C & 2.020482332996759 & 2.93267563217202 &  6.62668746749268 & 15.7209477041221 & 37.9264764864483 \\
\cmidrule(r){1-2} \cmidrule{3-7}
\multirow{2}{*}{\rot{appr.\vphantom{i}}}
 & semi-par.\ WKDE & 4.846089118511761 & 17.76414420915205 & 67.55341169341371 & 201.9098935356314 & 410.4734927212821 \\
 & tube WKDE      & \cellmark 0.803445339719881 & \cellmark 1.85794823092129  &  \cellmark 5.24700859976549 & 13.5473466123834 & 25.8150534590904 \\
\cmidrule(r){1-2} \cmidrule{3-7}
\multirow{3}{*}{\rot{impute}}
 & MICE+KDE  & 2.480876100687873 & 8.91565707723542 & 35.87726248460741 & 106.5219627628685 & 230.7999722905524 \\
 & mean~imp.+KDE     & 2.550059117198298 & 9.15200816320511 & 36.90734292678388 & 109.2582602544040 & 226.1052695773782 \\
 & SoftImpute+KDE    & 2.992662848031248 & 8.41849589708940 & 26.94407694112752 & 84.2967975360662 & 569.8177021090986 \\
\bottomrule
\end{tabularx}
\end{table}

\end{document}

答案2

这是一个变体,使用makecell

\documentclass[twoside, twocolumn]{article}%
\usepackage{geometry}
\usepackage[allowlitunits]{siunitx}
\usepackage[dvipsnames,svgnames,table]{xcolor}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{pbox}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{siunitx}
\usepackage{makecell}
\usepackage{multirow}
\usepackage{lipsum}

\sisetup{
    table-format=3.1,
    round-mode=places,
    round-precision=1,
    table-number-alignment = center,
    table-text-alignment = center,
    scientific-notation = false,
    exponent-product = \!\cdot\!
}
\newcolumntype{L}[1]{>{\RaggedRight\hspace{0pt}}p{#1}}

\definecolor{markcolor}{rgb}{0.68, 1.0, 0.18}
\newcommand{\cellmark}{{\cellcolor{markcolor!35}}}

\newcommand*\rot[1]{\rotatebox{90}{#1}}

\begin{document}

\lipsum[1]
\begin{table}[!htb]
\setlength{\tabcolsep}{3pt}\setlength{\extrarowheight}{2pt}
    \begin{tabularx}{\linewidth}{lXS[table-format=1.1]*{2}{S[table-format=2.1]}SS}
    \toprule
                                         & \multicolumn{6}{r@{}}{{variance of MISE for miss. perc.}} \\[1pt]
                                            & method & {5\,\%} &{10\,\%} & {20\,\%} & {35\,\%} & {50\,\%} \\
    \cmidrule(r{3pt}){1-2} \cmidrule(l{3pt}r{3pt}){3-7}
   \multirowcell{3}{\rot{\parbox{15mm}{virt. obj.\\\centering KDE}}} & W-KDE A & 2.106430081703724 & 2.99138256327109 & 6.22019189157428 & 12.7143637624467 \cellmark & \cellmark 24.0215821367398 \\
                                            & W-KDE B & 2.020482332996759 & 2.93267563217202 & 6.62668746749268 & 15.7209477041221 & 37.9264764864483 \\
                                            & W-KDE C & 2.020482332996759 & 2.93267563217202 & 6.62668746749268 & 15.7209477041221 & 37.9264764864483 \\
    \cmidrule(r{3pt}){1-2} \cmidrule(l{3pt}r{3pt}){3-7}
    \multirowcell{2}[-0.7ex]{\rot{approx.}}
                                            & \makecell[l]{semi-par.\\ WKDE} & 4.846089118511761 & 17.76414420915205 & 67.55341169341371 & 201.9098935356314 & 410.4734927212821 \\[2.5ex]
                                            & \raisebox{0pt}[0pt][0pt]{\makecell[l]{tube\\ WKDE}}& \cellmark 0.803445339719881 & \cellmark 1.85794823092129 & \cellmark 5.24700859976549 & 13.5473466123834 & 25.8150534590904 \\\addlinespace[1.5ex]
    \cmidrule(r{3pt}){1-2} \cmidrule(l{3pt}r{3pt}){3-7}
    \multirowcell{5}{\rot{impute}}
                                            & \makecell[l]{MICE\\+KDE} & 2.480876100687873 & 8.91565707723542 & 35.87726248460741 & 106.5219627628685 & 230.7999722905524 \\
                                            & \makecell[l]{mean imp.\\+KDE} & 2.550059117198298 & 9.15200816320511 & 36.90734292678388 & 109.2582602544040 & 226.1052695773782 \\
                                            & \makecell[l]{SoftImpute\\+KDE} & 2.992662848031248 & 8.41849589708940 & 26.94407694112752 & 84.2967975360662 & 569.8177021090986 \\[-1ex]
    \bottomrule
    \end{tabularx}
\end{table}

\end{document} 

在此处输入图片描述

答案3

我采用了您的 MWE 并使用了p长条目样式。有一个长度决定了表格中的单元格间距:\tabcolsep。将所有内容放在一起:

\documentclass[twoside,twocolumn]{article}

\RequirePackage[allowlitunits]{siunitx}
\RequirePackage[usenames,dvipsnames,svgnames,table]{xcolor}
\RequirePackage{graphicx}
\RequirePackage{pbox}
\RequirePackage{booktabs}
\RequirePackage{tabularx}
\RequirePackage{makecell}
\RequirePackage{multirow}

\begin{document}

\newcolumntype{Z}{S[
    table-format=1.1e0,
    round-mode=places,
    round-precision=1,
    table-number-alignment = center,
    table-text-alignment = center,
    scientific-notation = false,
    exponent-product = \!\cdot\!
    ]}
\newcolumntype{L}[1]{>{\RaggedRight\hspace{0pt}}p{#1}}

\definecolor{markcolor}{rgb}{0.68, 1.0, 0.18}
\newcommand{\cellmark}{{\cellcolor{markcolor!35}}}

\newcommand*\rot[1]{\rotatebox{90}{#1}}

%%% new code:
%%% wider table
\let\tabsepbakup\tabcolsep
\setlength{\tabcolsep}{2em}

%%% global declaration of precision
\sisetup{%
  round-mode=places,
  round-precision=1,
  scientific-notation = false,
  exponent-product = \!\cdot\!,
}

\begin{table}
    \begin{tabularx}{\linewidth}{lp{0.3\linewidth}SSSSS}
    \toprule
                                            &                       & \multicolumn{5}{c}{\makecell{variance of MISE for miss. perc.}} \\[1pt]
                                            & method                & \multicolumn{1}{c}{5\%} & \multicolumn{1}{c}{10\%} & \multicolumn{1}{c}{20\%} & \multicolumn{1}{c}{35\%} & \multicolumn{1}{c}{50\%} \\
    \cmidrule{1-2}                                          \cmidrule{3-7}
%   \multirow{3}{*}{\rotatebox[origin=c]{90}{\pbox{2cm}{\centering virtual\\object KDE}}}
    \multirow{3}{*}{\hspace{-3mm}\pbox{2cm}{virt.\\obj.\\KDE}\hspace{-5mm}}
                                            & W-KDE A   & 2.106430081703724 & 2.99138256327109 &  6.22019189157428 & \cellmark 12.7143637624467 & \cellmark 24.0215821367398 \\
                                            & W-KDE B   & 2.020482332996759 & 2.93267563217202 &  6.62668746749268 & 15.7209477041221 & 37.9264764864483 \\
                                            & W-KDE C   & 2.020482332996759 & 2.93267563217202 &  6.62668746749268 & 15.7209477041221 & 37.9264764864483 \\
    \cmidrule{1-2}                                          \cmidrule{3-7}
    \multirow{2}{*}{\rot{approx.}}
                                            & semi-par. WKDE    & 4.846089118511761 & 17.76414420915205 & 67.55341169341371 & 201.9098935356314 & 410.4734927212821 \\
                                            & tube WKDE         & \cellmark 0.803445339719881 & \cellmark 1.85794823092129  &  \cellmark 5.24700859976549 & 13.5473466123834 & 25.8150534590904 \\
    \cmidrule{1-2}                                          \cmidrule{3-7}
    \multirow{3}{*}{\rot{impute}}
                                            & MICE+KDE  & 2.480876100687873 & 8.91565707723542 & 35.87726248460741 & 106.5219627628685 & 230.7999722905524 \\
                                            & mean imp.+KDE     & 2.550059117198298 & 9.15200816320511 & 36.90734292678388 & 109.2582602544040 & 226.1052695773782 \\
                                            & SoftImpute+KDE    & 2.992662848031248 & 8.41849589708940 & 26.94407694112752 & 84.2967975360662 & 569.8177021090986 \\
    \bottomrule
    \end{tabularx}
\end{table}
\setlength{\tabcolsep}{\tabsepbakup}

%% for nicer pic
\thispagestyle{empty}
\end{document}

在此处输入图片描述

注意事项:旋转的文本不对称。\toprule并且 Co. 没有跨越整个表格长度。

相关内容