将 siunitx 列中心的数字与标题中的长文本右对齐

将 siunitx 列中心的数字与标题中的长文本右对齐

在这个 MWE 中,我希望能够换行列标题,同时利用 siunitx 的 S 列右对齐列中间的数字。我尝试过 tabu、warpcol 和 dcolumn。它们都不允许右对齐中间的数字列,并且可能换行列标题。

\documentclass[a4paper,11pt,oneside,openany,extrafontsizes,article]{memoir}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[normalem]{ulem}
\usepackage{siunitx}
\usepackage{warpcol,array,tabulary,tabu,booktabs}
\usepackage{fontspec,xltxtra,polyglossia}

\begin{document}


\begin{tabular}{@{}cSSS@{}}
\toprule
  {This is label of the first column} &
  {This is a long label} &
  {This column has a very very long long label} &
  {This is also not a short label} \\
\midrule
  Item 1 & 10 & 12 & 15 \\
  Item 2 & 20 & 22 & 25 \\
  Item 3 & 30 & 32 & 35 \\
\bottomrule
\end{tabular}

\end{document}

答案1

使用tabularx

在此处输入图片描述

\documentclass[a4paper,11pt,oneside,openany,extrafontsizes,article]{memoir}
%\usepackage[utf8]{inputenc}
%\usepackage[T1]{fontenc}
\usepackage{fontspec,xltxtra,polyglossia}

%\usepackage[normalem]{ulem}
\usepackage{siunitx}
\usepackage{booktabs, tabularx}
\newcommand\mcx[1]{\multicolumn{1}{>{\centering\arraybackslash}X}{#1}}

\begin{document}
    \begin{tabularx}{\linewidth}{@{}c*{3}{S[table-format=3.0]}@{}}
\toprule
  \mcx{This is label of the first column} &
  \mcx{This is a long label} &
  \mcx{This column has a very very long long label} &
  \mcx{This is also not a short label} \\
\midrule
  Item 1 & 10 & 12 & 15 \\
  Item 2 & 20 & 22 & 25 \\
  Item 3 & 30 & 32 & 35 \\
\bottomrule
    \end{tabularx}
\end{document}

答案2

您可以\theadmakecell包装如下。

单元格的垂直和水平对齐方式可以单独设置。还可以使用 设置单元格宽度\thead[{{p{1in}}}]{Cell text},这样就可以自动进行换行。有关更多信息,请参阅文档。

\documentclass[a4paper,11pt,article]{memoir}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{siunitx}
\usepackage{array,booktabs}
\usepackage{makecell}

\renewcommand\theadfont{\normalsize}

\begin{document}

\begin{tabular}{@{}cSSS@{}}
\toprule
  {\thead[t]{This is label of\\the first column}} &
  {\thead[t]{This is a\\long label}} &
  {\thead[t]{This column has\\a very very long\\long label}} &
  {\thead[t]{This is also not\\a short label}} \\
\midrule
  Item 1 & 10 & 12 & 15 \\
  Item 2 & 20 & 22 & 25 \\
  Item 3 & 30 & 32 & 35 \\
\bottomrule
\end{tabular}

\end{document}

输出

相关内容