In a tabular, how to left align ignoring minus signs?

I would use the dcolumn package to define a new column type with alignment at the decimal point with "two places to the left and five to the right" (in this case, the desired alignment for the headings will be obtained automatically):

\documentclass{article}
\usepackage{dcolumn}

\newcolumntype{L}{D{.}{.}{2,5}}

\begin{document}

\begin{tabular}{lLLL}
    &  $a$ & $b$ & $c$ \\
    A & -0.62645 & -0.82047 &  1.51178  \\
    B &  0.18364 &  0.48743 &  0.38984  \\
    C & -0.83563 &  0.73832 & -0.62124 \\
  \end{tabular}

\end{document}

enter image description here


In addition to the dcolumn package, there's also the siunitx package, which provides a column type named S that allows alignment on the decimal marker.

The following MWE uses this column type. Note that numeric data in an S column are automatically put into math mode -- hence the correctly employed "math minus" symbol. Also observe that the cells that in the header row are typeset using a dedicated column type (named "N"), which acts like a text-mode l column except that the entries are automatically shifted to the right by the amount of an (invisible) math-minus symbol.

\documentclass{article}
\usepackage{siunitx,array}
\sisetup{table-format=-1.5, group-digits=false}
\newcolumntype{L}{>{\phantom{$-$}}l}       % prefix some whitespace
\newcommand\mcL[1]{\multicolumn{1}{L}{#1}} % handy shortcut macro
\let\familydefault\sfdefault % optional: switch to a sans-serif font
\begin{document}
  \begin{tabular}{lSSS}
      & \mcL{a}  & \mcL{b}  & \mcL{c}   \\
    A & -0.62645 & -0.82047 &  1.51178  \\
    B &  0.18364 &  0.48743 &  0.38984  \\
    C & -0.83563 &  0.73832 & -0.62124  \\
  \end{tabular}
\end{document}

producing:

enter image description here


One way to to align left ignoring the minus sign is to use the collcell pacakge to process table entries as follows:

  • if the number is negative, put the number in math mode
  • if the number is a positive, add a \phantom{-} and put the number in math mode
  • otherwise just add a \phantom{-}. This is to skip over for the column headers

With this I defined a new column type L, and this allows you not to have to bother with any formatting in the actual table. So, you just write (manual spacing here just for readability):

\begin{tabular}{lLLL}
      &  a       &  b       &  c       \\
    A & -0.62645 & -0.82047 &  1.51178 \\
    B &  0.18364 &  0.48743 &  0.38984 \\
    C & -0.83563 &  0.73832 & -0.62124
\end{tabular}

and you get:

enter image description here

Below I have used the xstring package to parse the content but there are probably pure TeX ways of doing this. I prefer xstring as it is easier to read.

\documentclass{standalone}
\usepackage{xstring}
\usepackage{collcell}

\newcommand{\AddPhantomMinusIfNeeded}[1]{%
\IfDecimal{#1}{% Is a decimal, so if not negative add a \phantom{-}%
    \IfBeginWith{#1}{-}{\ensuremath{#1}}{\ensuremath{\phantom{-}#1}}%
    }{%
        \ensuremath{\phantom{-}}#1% Not a decimal number so just leave it alone
    }%
}%

\newcolumntype{L}{>{\collectcell\AddPhantomMinusIfNeeded}{l}<{\endcollectcell}}

\begin{document}
\begin{tabular}{lLLL}
      & a        & b        &  c       \\
    A & -0.62645 & -0.82047 &  1.51178 \\
    B &  0.18364 &  0.48743 &  0.38984 \\
    C & -0.83563 &  0.73832 & -0.62124
\end{tabular}
\end{document}

It should be noted that this solution will produce good results if all the cell entries have have the same number of digits. The other solutions here are better suited for the general case of decimal alignment.