表格列按小数对齐

表格列按小数对齐

Jeff Dean 发布了这张著名的“每个软件开发人员都应该知道的数字”表格。我正在努力将其正确格式化为使用 LaTeX 编写的出版物。

正如你所见,左边的列很简单。它是左对齐的。

右列需要对齐“ns”一词,并在其后偶尔添加注释。此外,第一行有小数,而其他数字是整数。

这是我迄今为止最好的尝试:

\begin{tabular}{ | l | r  |}
\hline
L1 cache reference                  &            0.5 ns       \\
Branch mispredict                   &            5 ns         \\
L2 cache reference                  &            7 ns         \\
Mutex lock/unlock                   &          100 ns (25)    \\
Main memory reference               &          100 ns         \\
Compress 1K bytes with Zippy        &       10,000 ns (3,000) \\
Send 2K bytes over 1 Gbps network   &       20,000 ns         \\
Read 1 MB sequentially from memory  &      250,000 ns         \\
Round trip within same datacenter   &      500,000 ns         \\
Disk seek                           &   10,000,000 ns         \\
Read 1 MB sequentially from network &   10,000,000 ns         \\
Read 1 MB sequentially from disk    &   30,000,000 ns (20,000,000) \\
Send packet CA to Netherlands to CA & 150,000,000 ns \\
\hline
\end{tabular}

我最关心的是如何对齐“ns”。我不太关心 0.5 是否按十进制对齐。

答案1

使用siunitx,单位ns是通过列规范添加的,其空间在具有选项的列<中保留。(最好在头部而不是在每一行中给出单位,因为它可能也适用于括号中的值,不是吗?)Stable-space-text-pst

在最后一列中,()被删除为input-openinput-close-uncertainty字符,以便它们可以在列中自由使用。

代码

\documentclass[varwidth,convert=false]{standalone}
\usepackage{booktabs}
\usepackage{siunitx}
\begin{document}
\begin{tabular}{
  l
  S[table-format=9.1, table-space-text-post={\,\si{\nano\second}}]
    <{\,\si{\nano\second}}
  S[table-format=8.0, table-space-text-pre=(, table-space-text-post=),
                      input-close-uncertainty={[}, input-open-uncertainty={]}]
}
    \toprule
    Description                         & \multicolumn{1}{c}{Some time} & {\footnotesize Other value} \\ \midrule
    L1 cache reference                  & .5                            &  \\
    Branch mispredict                   & 5                             &  \\
    L2 cache reference                  & 7                             &  \\
    Mutex lock/unlock                   & 100                           & (25)                        \\
    Main memory reference               & 100                           &  \\
    Compress 1K bytes with Zippy        & 10 000                        & (3 000)                     \\
    Send 2K bytes over 1 Gbps network   & 20 000                        &  \\
    Read 1 MB sequentially from memory  & 250 000                       &  \\
    Round trip within same datacenter   & 500 000                       &  \\
    Disk seek                           & 10 000 000                    &  \\
    Read 1 MB sequentially from network & 10 000 000                    &  \\
    Read 1 MB sequentially from disk    & 30 000 000                    & (20 000 000)                \\
    Send packet CA to Netherlands to CA & 150 000 000                   &  \\ \bottomrule
\end{tabular}
\end{document}

输出

在此处输入图片描述

答案2

一种可能性是使用dcolumn包裹:

\documentclass{article}
\usepackage{dcolumn}
\usepackage{amsmath}


\begin{document}

\begin{tabular}{ | l | D{;}{\text{\,ns\,}}{-1}  |}
\hline
L1 cache reference                  &            0.5 ;       \\
Branch mispredict                   &            5 ;         \\
L2 cache reference                  &            7 ;         \\
Mutex lock/unlock                   &          100 ; (25)    \\
Main memory reference               &          100 ;         \\
Compress 1K bytes with Zippy        &       10,000 ; (3,000) \\
Send 2K bytes over 1 Gbps network   &       20,000 ;         \\
Read 1 MB sequentially from memory  &      250,000 ;         \\
Round trip within same datacenter   &      500,000 ;         \\
Disk seek                           &   10,000,000 ;         \\
Read 1 MB sequentially from network &   10,000,000 ;         \\
Read 1 MB sequentially from disk    &   30,000,000 ; (20,000,000) \\
Send packet CA to Netherlands to CA & 150,000,000 ; \\
\hline
\end{tabular}

\end{document}

在此处输入图片描述

也许booktabs包可能会让您感兴趣;您的表格看起来会好得多,不鼓励使用垂直规则。

同一张表,使用booktabs

\documentclass{article}
\usepackage{booktabs}
\usepackage{dcolumn}
\usepackage{amsmath}


\begin{document}

\begin{tabular}{  l  D{;}{\text{\,ns\,}}{-1} }
\toprule
L1 cache reference                  &            0.5 ;       \\
Branch mispredict                   &            5 ;         \\
L2 cache reference                  &            7 ;         \\
Mutex lock/unlock                   &          100 ; (25)    \\
Main memory reference               &          100 ;         \\
Compress 1K bytes with Zippy        &       10,000 ; (3,000) \\
Send 2K bytes over 1 Gbps network   &       20,000 ;         \\
Read 1 MB sequentially from memory  &      250,000 ;         \\
Round trip within same datacenter   &      500,000 ;         \\
Disk seek                           &   10,000,000 ;         \\
Read 1 MB sequentially from network &   10,000,000 ;         \\
Read 1 MB sequentially from disk    &   30,000,000 ; (20,000,000) \\
Send packet CA to Netherlands to CA & 150,000,000 ; \\
\bottomrule
\end{tabular}

\end{document}

在此处输入图片描述

相关内容