对齐小数位

对齐小数位

我有一张表格,我想根据小数位对齐数字,同时数字仍然居中。我尝试使用 siunitx 包,但没有成功 :(

非常感谢您的帮助!(数据不是真实的)

\usepackage[format=plain,
            labelfont=it,
            textfont=it]{caption}
\usepackage{array}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{tabu}
\usepackage{booktabs}
\usepackage[tableposition=top]{caption}


\begin{document}

\begin{table}[hbt!]
    \centering
    \caption{Time measurements for oil samples with different weights of additive X for 40 and 60$^\circ C$. Dynamic viscosity is then calculated using the viscometer constant, $C$.}
    \begin{tabular}[t]{c c c c c c}
    \toprule
         Concentration (wt\%) & $T$ ($^\circ$C) & \multicolumn{3}{c}{$t$ ($s$, $\pm1$)} & Dynamic Viscosity ($cP$, $\pm0.1$)\\
          & & 1$^{\text{st}}$ & 2$^{\text{nd}}$ & 3$^{\text{rd}}$ & \\
        \midrule
        \multirow{2}{*}{0}& 40 & 10 & 10 & 10 & 68.3 \\
           & 60 & 10 & 10 & 10 & 4.3 \\ \\
           
        \multirow{2}{*}{1.875}& 40 & 10 & 10 & 10 & 42.3\\
           & 60 & 10 & 10 & 10 & 3.2\\ \\
           
        \multirow{2}{*}{2.5}& 40 & 10 & 10 & 10 & 31.4\\
           & 60 & 10 & 10 & 10 & 4.9\\ \\
           
        \multirow{2}{*}{5}& 40 & 10 & 10 & 10 & 12.2\\
           & 60 & 10 & 10 & 10 & 13.0\\ \\
           
        \multirow{2}{*}{7}& 40 & 10 & 10 & 10 & 43.2\\
           & 60 & 10 & 10 & 10 & 1.0\\
        \bottomrule
         
    \end{tabular}
    \label{tab:my_label}
\end{table} ```

答案1

除了将数据列中的数字与其(显式或隐式)小数点对齐之外,您还应尝试 (a) 通过在标题单元格中提供合适的换行符使表格更紧凑,以及 (b) 更仔细地排版科学单位和相关数量。借助包的\unit\qty宏,可以轻松实现后一个目标siunitx

在此处输入图片描述

\documentclass{article}
\usepackage[format=plain,font=it,tableposition=top]{caption}
\usepackage{array,siunitx,booktabs,amsmath}
\newcolumntype{T}[1]{S[table-format=#1]}
\newcommand\mytab[1]{%
  \smash[b]{\begin{tabular}[t]{@{}c@{}} #1 \end{tabular}}}

\begin{document}

\begin{table}[hbt!]
    \centering
    \caption{Time measurements for oil samples with different weights of
    additive~X for \qty{40}{\celsius} and \qty{60}{\celsius}. Dynamic 
    viscosity is then calculated using the viscometer constant $C$.}
    \label{tab:my_label}
    \begin{tabular}{@{} T{1.3} *{4}{T{2.0}} T{2.1} @{}}
    \toprule
    \mytab{Concentration\\(wt \%)} & 
    \mytab{Temp.\\(\unit{\celsius})} & 
    \multicolumn{3}{c}{Time (\unit{\second}, $\pm1$)} & 
    \mytab{Dynamic viscosity\\($cP$, $\pm0.1$)}\\
    \cmidrule(lr){3-5}
    && {1\textsuperscript{st}} & {2\textsuperscript{nd}} &  {3\textsuperscript{rd}} \\
    \midrule
    0     & 40 & 10 & 10 & 10 & 68.3\\
          & 60 & 10 & 10 & 10 &  4.3\\ 
    \addlinespace    
    1.875 & 40 & 10 & 10 & 10 & 42.3\\
          & 60 & 10 & 10 & 10 &  3.2\\ 
    \addlinespace    
    2.5   & 40 & 10 & 10 & 10 & 31.4\\
          & 60 & 10 & 10 & 10 &  4.9\\ 
    \addlinespace     
    5     & 40 & 10 & 10 & 10 & 12.2\\
          & 60 & 10 & 10 & 10 & 13.0\\ 
    \addlinespace    
    7     & 40 & 10 & 10 & 10 & 43.2\\
          & 60 & 10 & 10 & 10 &  1.0\\
    \bottomrule  
    \end{tabular}
\end{table}
\end{document}

答案2

使用tabularray包(版本 2022B)及其库booktabs(用于表规则)和siunitx(用于S列类型,其中数字在小数点对齐)加载同名的包:

\documentclass{article}
\usepackage[format=plain,
            labelfont=it,
            textfont=it]{caption}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}
  \ExplSyntaxOn
\NewChildSelector{eachtwo}
  {
    \int_step_inline:nnnn {3}{2}{\l_tblr_childs_total_tl}
      { \clist_put_right:Nn \l_tblr_childs_clist {##1} }
  }
\ExplSyntaxOff

\begin{document}

\begin{table}[hbt!]
    \centering
    \caption{Time measurements for oil samples with different weights of
    additive~X for \qty{40}{\celsius} and \qty{60}{\celsius}. Dynamic
    viscosity is then calculated using the viscometer constant $C$.}
    \label{tab:my_label}
        \begin{tblr}{colspec = {@{} S[table-format=1.3] 
                               *{4}{S[table-format=2.0]} 
                                    S[table-format=2.1]  @{}},
                    row{1,2} = {guard},
                    row{3-Z} = {rowsep=0pt},
                    row{eachtwo} = {abovesep=4pt}
                    }
        \toprule
    \SetCell[r=2]{c}    {Concentration\\(wt \%)} 
        &   \SetCell[r=2]{c}    {Temp.\\(\unit{\celsius})} 
            &   \SetCell[c=3]{c}    Time (\unit{\second}, $\pm1$) 
                &   &   &   \SetCell[r=2]{c}    {Dynamic viscosity\\($cP$, $\pm0.1$)}   \\
    \midrule
        &   & {1\textsuperscript{st}} 
                & {2\textsuperscript{nd}} 
                    &  {3\textsuperscript{rd}} 
                        &               \\
    \midrule
    0     & 40 & 10 & 10 & 10 & 68.3    \\
          & 60 & 10 & 10 & 10 &  4.3    \\
    1.875 & 40 & 10 & 10 & 10 & 42.3    \\
          & 60 & 10 & 10 & 10 &  3.2    \\
    2.5   & 40 & 10 & 10 & 10 & 31.4    \\
          & 60 & 10 & 10 & 10 &  4.9    \\
    5     & 40 & 10 & 10 & 10 & 12.2    \\
          & 60 & 10 & 10 & 10 & 13.0    \\
    7     & 40 & 10 & 10 & 10 & 43.2    \\
          & 60 & 10 & 10 & 10 &  1.0    \\
    \bottomrule
    \end{tblr}
\end{table}
\end{document}

在此处输入图片描述

相关内容