Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
See also tex.stackexchange.com/questions/2746/… –  Joseph Wright Nov 12 '11 at 19:48
add comment

5 Answers

up vote 17 down vote accepted

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

share|improve this answer
    
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. –  dmckee Nov 12 '11 at 21:19
1  
+1: does the dcolumn enter Math mode automatically? –  cmhughes Dec 7 '11 at 1:01
    
@cmhughes: yes. –  Gonzalo Medina Dec 7 '11 at 1:06
add comment

In addition to the dcolumn package, there's also the siunitx package, which provides a column type labelled S that allows alignment on the decimal marker. The following MWE uses this column type,

\documentclass{article}
\usepackage{siunitx,array}
\sisetup{table-format=2.5,group-digits=false}
\newcolumntype{L}{>{\phantom{$-$}}l}
\begin{document}\Huge
  \begin{tabular}{lSSS}
      & \multicolumn{1}{L}{a} 
      & \multicolumn{1}{L}{b}        
      & \multicolumn{1}{L}{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

Note that numeric data are automatically put into math mode (hence the correctly employed "math minus" symbol).

Finally, in order to left-align the column headers while leaving space for the (non-visible) math minus sign, I've defined a new column type called L, which is like the ordinary l column type except that a blank space the width of $-$ is inserted first.

share|improve this answer
    
To produce the desired alignment for the column headers you could use something like \multicolumn{1}{l}{\phantom{0}a}. –  Gonzalo Medina Nov 12 '11 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 '11 at 22:24
add comment

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}
share|improve this answer
1  
It seems as if this can be simplified as the \multicolumn does not seem necessary, at least in this case. –  Peter Grill Nov 12 '11 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 '11 at 22:44
add comment

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.

share|improve this answer
add comment

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}
share|improve this answer
    
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. –  brianjd Nov 12 '11 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 '11 at 20:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.