将表格列中的文本对齐为小数

将表格列中的文本对齐为小数

我想对齐列中多行共有的一些文本,其方式类似于使用 dcolumn 对齐列中的小数点。我还没有弄清楚如何将 dcolumn 用于文本(它需要数学运算?)。\llap 改善了外观,但并没有完全按照我想要的方式对齐。我还尝试在表格内进行制表,但无法绕过错误(它需要一个 \item)。

\documentclass{article}
\usepackage{tabularx,tabu,arraycols,multirow,booktabs}
\usepackage{textcomp}
\usepackage{dcolumn}
\begin{document}
\begin{table}
\centering
\caption{Table.}
\begin{tabular}{c D{,}{\pm}{-1} c c}
\toprule
\textbf{some text} & \multicolumn{1}{c}{\textbf{more text}} & \textbf{other text} & \textbf{\% of something} \\
\midrule
\multirow{5}{*}{x to y} & 170 , 2.5 & $\sim$no wake & \multirow{5}{*}{<29\%} \\
& 175 , 2.5 & $\sim$half wake & \\
& 180 , 2.5 & full wake & \\
& 185 , 2.5 & $\sim$half wake & \\
& 190 , 2.5 & $\sim$no wake & \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

我的桌子

我不知道为什么我会得到颠倒的 !,但这不会发生在我的真实文件中。我希望第三列通常水平居中,同时与 w 对齐。

答案1

两个版本,要么是单一r类型列,要么是r和一个l类型列:

在此处输入图片描述

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tabularx,arraycols,multirow,booktabs}
\usepackage{textcomp}
\usepackage{dcolumn}
\begin{document}
\begin{table}
\centering
\caption{Table.}
\begin{tabular}{c D{,}{\pm}{-1} r c}
\toprule
\textbf{some text} & \multicolumn{1}{c}{\textbf{more text}} & \textbf{other text} & \textbf{\% of something} \\
\midrule
\multirow{5}{*}{x to y} & 170 , 2.5 & $\sim$no wake & \multirow{5}{*}{<29\%} \\
& 175 , 2.5 & $\sim$half wake & \\
& 180 , 2.5 & full wake & \\
& 185 , 2.5 & $\sim$half wake & \\
& 190 , 2.5 & $\sim$no wake & \\
\bottomrule
\end{tabular}
\end{table}

\begin{table}
\centering
\caption{Table.}
\begin{tabular}{c D{,}{\pm}{-1} r@{~}l c}
\toprule
\textbf{some text} & \multicolumn{1}{c}{\textbf{more text}} & \multicolumn{2}{c}{\textbf{other text}} & \textbf{\% of something} \\
\midrule
\multirow{5}{*}{x to y} & 170 , 2.5 & $\sim$no &wake & \multirow{5}{*}{<29\%} \\
& 175 , 2.5 & $\sim$half& wake & \\
& 180 , 2.5 & full& wake & \\
& 185 , 2.5 & $\sim$half& wake & \\
& 190 , 2.5 & $\sim$no& wake & \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tabularx,arraycols,multirow,booktabs}
\usepackage{textcomp}
\usepackage{dcolumn}
\usepackage{calc}

\begin{document}
\begin{table}
\centering
\newlength{\mywidth}
\setlength{\mywidth}{\widthof{a longer column header}}
\caption{Table.}
\begin{tabular}{c D{,}{\pm}{-1} Wr{0.5\mywidth}@{~} Wl{0.5\mywidth} c}
\toprule
\textbf{some text} & \multicolumn{1}{c}{\textbf{more text}} & \multicolumn{2}{c}{\textbf{a longer column header}} & \textbf{\% of something} \\
\midrule
\multirow{5}{*}{x to y} & 170 , 2.5 & $\sim$no &wake & \multirow{5}{*}{<29\%} \\
& 175 , 2.5 & $\sim$half& wake & \\
& 180 , 2.5 & full& wake & \\
& 185 , 2.5 & $\sim$half& wake & \\
& 190 , 2.5 & $\sim$no& wake & \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

在此处输入图片描述

答案2

以下是通过创建 2 列来解决的解决方法:

\documentclass{article}
\usepackage{tabularx,arraycols,multirow,booktabs}
\usepackage{textcomp}
\usepackage{dcolumn}
\begin{document}
\begin{table}
\centering
\caption{Table.}
\begin{tabular}{c D{,}{\pm}{-1} r @{\space} l c }
\toprule
\textbf{some text} & \multicolumn{1}{c}{\textbf{more text}} & \multicolumn{2}{c}{\textbf{other text}} & \textbf{\% of something} \\ 
\midrule
\multirow{5}{*}{x to y} & 170 , 2.5 & $\sim$no & wake & \multirow{5}{*}{$<29\%$} \\
& 175 , 2.5 & $\sim$half&wake & \\
& 180 , 2.5 & full&wake & \\
& 185 , 2.5 & $\sim$half&wake & \\
& 190 , 2.5 & $\sim$no&wake & \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

上述代码的结果

相关内容