How to align numbers to en-dash in a table?

One solution with and one without siunitx:

Code

\documentclass[varwidth]{standalone}
\usepackage{booktabs,siunitx,calc}
\newcommand*{\alignUnderEnDash}[1]{%
  \multicolumn{1}{r@{}}{\llap{\makebox[\widthof{--}][c]{#1}}}}
\begin{document}
\begin{tabular}{@{}lr@{--}l@{}}
    \toprule
    AA &    \multicolumn{2}{c}{BB}     \\ \midrule
    A  &                      1 & 2    \\
    B  &                     10 & 50   \\
    C  &                    500 & 1000 \\
    D  &                  0.001 & 0.5  \\
    E  & \alignUnderEnDash{10} &  \\ \bottomrule
\end{tabular}\qquad
\begin{tabular}{@{}   l
                      S[table-format=3.3]
                @{--} S[table-format=4.1]
                @{}}
    \toprule
    AA &    \multicolumn{2}{c}{BB}    \\ \midrule
    A  & 1                     & 2    \\
    B  & 10                    & 50   \\
    C  & 500                   & 1000 \\
    D  & 0.001                 & 0.5  \\
    E  & \alignUnderEnDash{10} &  \\ \bottomrule
\end{tabular}
\end{document}

Output

enter image description here


The following example is perhaps not the most elegant variant. But it centers the column title BB on the en dash. It uses separate columns for the numbers and the dash:

\documentclass{article}
\usepackage{booktabs}
\usepackage{array}

\begin{document}
  \begin{tabular}[!hb]{lr@{}>{--}c@{}l}
    \toprule
    AA & & \multicolumn{1}{@{}c@{}}{\makebox[0pt]{BB}} &\\
    \midrule
    A & 1&&2 \\
    B & 10&&50 \\
    C & 500&&1000 \\
    D & 0.001&&0.5 \\
    \bottomrule
  \end{tabular}
\end{document}

Result

Remarks:

  • Because the width of column title "BB" is ignored by \makebox(0pt){...}, TeX will not warn, if the entry is too large.
  • I have added the column separation space at the left and right of the table, because it looks better with horizontal lines IMHO.

Support for longer column titles

The following example addresses the problem of the previous solution that the width of the column header is not taken into the account for the column widths. The example measures the width of the column header and reserves space in the first and third column of the three columns for the number range.

\documentclass{article}
\usepackage{booktabs}
\usepackage{array}

\newdimen\tempwidthBB

\begin{document}
  \begin{tabular}[!hb]{lr@{}>{--}c@{}l}
    \toprule
    AA &
      \sbox0{B tattarrattat B}%
      \sbox2{--}%
      \global\tempwidthBB=\wd0 %
      \global\advance\tempwidthBB by -\wd2 %
      \rlap{\copy0 }%
      \kern.5\tempwidthBB
    & \multicolumn{2}{@{}l}{%
        \hphantom{--}\kern.5\tempwidthBB
      }\\
    \midrule
    A & 1&&2 \\
    B & 10&&50 \\
    C & 500&&1000 \\
    D & 0.001&&0.5 \\
    \bottomrule
  \end{tabular}
\end{document} 

Result with longer column header


A slight abuse of dcolumn perhaps, but here you go:

\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{dcolumn,booktabs}
\usepackage{preview}
\begin{document}
\newcolumntype{d}[1]{D{-}{\mbox{--}}{#1}}
\begin{table}
    \centering
    \begin{tabular}{ld{-1}}
        \toprule
        AA & \multicolumn{1}{c}{BB} \\
        \midrule 
        A & 1-2 \\
        B & 10-50 \\
        C & 500-1000 \\
        D & 0.001-0.5 \\
        \bottomrule
    \end{tabular}
\end{table}
\end{document}

We make use of the fact that dcolumn allows the input separator to be different to the output separator, avoiding the need to pick up an endash in math mode (dcolumn works in math mode).

Example output

Edit based on Heiko Oberdiek's input - thanks

Tags:

Tables