7

Is it a bug in the numprint package .. or maybe a missing feature? I can't align on the plus/minus sign in tabular environments, padding numbers with spaces.

\documentclass{article}
\usepackage{booktabs}
\usepackage{numprint}

\begin{document}
\begin{center}
\nplpadding[\ ]{4}
\begin{tabular}{*{2}{N{4}{2}}}
  \toprule
  -940    & +1000   \\
  +952.38 & -1000 \\ \midrule
  +12.38 &     0 \\
  \bottomrule
\end{tabular}
\end{center}

\end{document}

Any hint welcome.

10
  • 1
    I do not know the numprint package, but siunitx offers you aligning at the decimal sign. Oct 14, 2012 at 16:32
  • In fact, numprint works correctly with the alignment at the decimal sign. The problem is the plus/minus signs ...
    – green diod
    Oct 14, 2012 at 16:37
  • 1
    First instinct: \nplpadding[\hphantom{0}]{4}, but it fails. (A space has not the same width as a digit.) Oct 14, 2012 at 16:58
  • 1
    I came to the same conclusion about the space width. Now, how to tell \nplpadding to use a blank character the same width as the surrounding digits?
    – green diod
    Oct 14, 2012 at 17:01
  • I can only offer a manual solution (separate columns for + and $-$): {*{2}{r@{}N{4}{2}}} Oct 14, 2012 at 17:26

1 Answer 1

2

I don't know about the “good ol’ plainTeX” way, but you can use an additional column that is right aligned (as + and have the same horizontal width the alignment doesn't really matter).

Basis

\begin{tabular}{*{2}{r@{}N{4}{2}}}
  \toprule
  $-$ & 940    &  +  & 1000 \\
   +  & 952.38 & $-$ & 1000 \\ \midrule
   +  &  12.38 &     &    0 \\
  \bottomrule
\end{tabular}

The padding is still be done by numprint but with @{} we force that + and are being typeset without any further space.

As this is probably needed a lot more times, with the use of the array package we define another column specification:

\newcolumntype{M}[2]{r@{}N{#1}{#2}}

It can be used, for example, as M{4}{2}.

Typing all those ampersands is a bit tedious so we make + and - active so that they insert the ampersands on its own, but we don't want this to affect the whole document, only our special tabular should have this effect, therefore I defined a new ntabular environment that is basically just a wrapper for a tabular environment with active + and -.

(I didn't introduce another character that “automatically” expands to &, because … you can just use &, can't you? Unfortunatly, this is still needed if you have no sign in front of a number.)

Code

\documentclass{article}
\usepackage{booktabs,numprint,array}
\begingroup
    \catcode`-=\active
    \catcode`+=\active
    \gdef-{$\char`-$&}
    \gdef+{\char`+&}
\endgroup
\newenvironment{ntabular}[1]{%
    \catcode`-=\active
    \catcode`+=\active
    \tabular{#1}
}{%
    \endtabular
}
\newcolumntype{M}[2]{r@{}N{#1}{#2}}
\begin{document}
Is it safe to use + and -?
\begin{table}[h]
\begin{ntabular}{M{3}{2}M{4}{2}}
  \toprule
   - 940    &  + 1000 \\
   + 952.38 &  - 1000 \\ \midrule
   +  12.38 &  &    0 \\
  \bottomrule
\end{ntabular}
\end{table}

Is it safe to use + and -?
\end{document}

Output

enter image description here

3
  • Why is there more space after the signs in the left hand column than the right hand column?
    – Thruston
    Oct 15, 2012 at 16:05
  • @Thruston Because the first N column uses 4 digits before the decimal point, too. Better use would be {M{3}{2} M{4}{0}}, I guess, but I didn't want to change the OP's column specification. Oct 15, 2012 at 16:16
  • I like your solution. For the coumn specs, I wanted it simple. But indeed, I shouldn't be so lazy and adjust each column appropriately ;)
    – green diod
    Oct 16, 2012 at 8:44

You must log in to answer this question.

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