18

I'd like to create a table that is aligned left, but minus signs are ignored. E.g., the following gives a normal left-aligned table

  \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}

normal

while the following code returns what I'd actually like

  \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}

desired

Q: Is there a smarter way of doing this?

1

5 Answers 5

20

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

4
  • I wished I'd heard about dcolumn years ago. I've been using the r@{.}l hack for ages and hating it all the while. Nov 12, 2011 at 21:19
  • 1
    +1: does the dcolumn enter Math mode automatically?
    – cmhughes
    Dec 7, 2011 at 1:01
  • 1
    @cmhughes: yes. Dec 7, 2011 at 1:06
  • This approach "works" if the header cells indeed just consist of "a", "b", and "c" -- more generally, of single characters. The results are going to be quite ugly, though, if the contents of the header cells are somewhat more complicated.
    – Mico
    Mar 7, 2017 at 8:55
8

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

3
  • To produce the desired alignment for the column headers you could use something like \multicolumn{1}{l}{\phantom{0}a}. Nov 12, 2011 at 20:24
  • @GonzaloMedina -- If a table has quite a few columns all of the same (not-yet-defined) type, I think it's worth the effort to define a new column type.
    – Mico
    Nov 12, 2011 at 22:24
  • @Werner -- Many thanks for the edit!
    – Mico
    Mar 7, 2017 at 8:57
7

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.

1
  • This result works best in my simple example, where I have an array whose elements are -1, 0, or 1 and where I want the center of the "1"'s and "0"'s to align with \vdots. With a centered environment and \phantom{-} before each of my non-negative numbers AS WELL as my \vdots, I elegantly get the alignment I want, while aligning with a virtual decimal point would not put the \vdots in the correct place.
    – djconnel
    Apr 30, 2015 at 19:43
5

Your ultimate goal is to typeset aligned numbers in the table. There is a package named dcolumn that does just that: adds a new column specifier D that aligns numbers on their decimal point. It also allows a number sign, and automatically enters math mode (which will typeset the correct "minus", not a hyphen).

\documentclass{article}

\usepackage{dcolumn}

\newcolumntype{d}{D{.}{.}{-1}}

\begin{document}

\begin{tabular}{lddd}
  & \multicolumn{1}{c}{a} & \multicolumn{1}{c}{b} & \multicolumn{1}{c}{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}
2
  • 1
    It seems as if this can be simplified as the \multicolumn does not seem necessary, at least in this case. Nov 12, 2011 at 19:58
  • 1
    @PeterGrill: Something has to be done to keep the column headers from being typeset in math mode (in which case they'd be rendered in math italics, which does not appear to be what the OP wants). That said, given the OP's question it might be better to left-align rather then center the entries.
    – Mico
    Nov 12, 2011 at 22:44
2

I don't think there are very many smarter ways of doing it, since this depends on the scope (how many of these tables you have, or how big they are, and to what extent you want to automate the process) and some aesthetics. However, there are many other ways of doing it:

enter image description here

\documentclass{article}
\begin{document}
\begin{tabular}{lrrr}
      & \multicolumn{1}{l}{$\phantom{-}$a} & 
        \multicolumn{1}{l}{$\phantom{-}$b} & 
        \multicolumn{1}{l}{$\phantom{-}$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}

The above uses math mode to typeset the numbers in you tabular. Alternatively, you could also typeset the entire table in an array environment and merely switch to text mode where needed (for the column/row indexes, say). For this, amsmath provides \text{<stuff>}, where <stuff> can include spaces.

Additionally, the motivation for the above suggestion is to use right alignment for the columns containing numbers, and switch to left alignment for the column indexes only (row 1). That way you only have to worry about the minuses in the "header". Also, the alignment adjustment for the minus sign is accomplished via \phantom{<stuff>} which leaves space equivalent to <stuff> without actually typesetting it. This can help in a general setting where you want to leave space for something other than a minus sign, and spacing is adjusted for by the font and text mode.

Here is another alternative that exploits a zero-width column separation @{} to remove the gap between successive columns:

\documentclass{article}
\begin{document}
\begin{tabular}{l*{3}{r@{}l}}
      & &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}
2
  • Very helpful information. Wasn't aware my minus signs weren't printing out correctly. Also, never knew about \phantom. Surprised there isn't an alignment parameter that takes care of all this. Definitely makes for the best-looking tables IMHO.
    – lowndrul
    Nov 12, 2011 at 19:42
  • @brianjd: I've added another alternative. Yes, there are little/subtle things that one can do to make the output look more appealing. Then it boils down to what you're more comfortable with (say, neat/elegant code, understandable code given your skill level, or just get-'er done code). There are many things to consider, and I'm sure you've learned plenty from the examples given. The upvotes reflex @ Gonzalo's answer to be best in terms of elegance. You choice is accepting one.
    – Werner
    Nov 12, 2011 at 20:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .