[comp.text.tex] TABLES-- Alligning the decimal points.

nqdy@vax5.cit.cornell.edu (01/25/91)

Hi netters,
       I am using LaTeX for making tables. I would like to allign
the numbers in a column so that the decimal points are alligned
rather than the number being left/right/center alligned. This is
especially a problem when the numbers in a column have different number
of digits before and after the decimal point.
       sanjeev
       nqdy@vax5.cit.cornell.edu
       sanjeev@cheme.tn.cornell.edu

golding@Neon.Stanford.EDU (Andy Golding) (01/27/91)

nqdy@vax5.cit.cornell.edu writes:
>       I am using LaTeX for making tables. I would like to allign
>the numbers in a column so that the decimal points are alligned
>rather than the number being left/right/center alligned.

This is not the most elegant solution in the world,
but one thing to do is use a `tabular' environment,
and include 2 fields for each number.  The first field
is for the integer portion of the number and is right-aligned,
the second field is for the fractional portion of the number
and is left-aligned.  To avoid having space between the
integer and fraction, in your column specification
at the top of the environment, say `r@{}l'.
Then you specify a number like 17.25 as `17&.25'.

Andy

moss@cs.umass.edu (Eliot Moss) (01/28/91)

You can also use two table columns. You have to get rid of any extra
inter-column spaces. Going from my LaTeX documentation, the following works (I
tested it):

\begin{tabular}{r@{}lr@{}l}
1. & 0   &  0. & 0003 \\
0. & 333 & -0. & 0001 \\
\end{tabular}

While this is not quite as convenient as you might like, it's not all that
difficult either ....						Eliot
--

		J. Eliot B. Moss, Assistant Professor
		Department of Computer and Information Science
		Lederle Graduate Research Center
		University of Massachusetts
		Amherst, MA  01003
		(413) 545-4206, 545-1249 (fax); Moss@cs.umass.edu

Ray.Nickson@comp.vuw.ac.nz (Ray Nickson) (01/31/91)

In article <1991Jan26.221417.24115@Neon.Stanford.EDU> golding@Neon.Stanford.EDU (Andy Golding) writes:
   nqdy@vax5.cit.cornell.edu writes:
   >       I am using LaTeX for making tables. I would like to allign
   >the numbers in a column so that the decimal points are alligned
   >rather than the number being left/right/center alligned.

   This is not the most elegant solution in the world,
   but one thing to do is use a `tabular' environment ...

(I've run this through LaTeX, but don't have a printer or previewer
handy; apologies and free flames if it's wrong).

I think this is slightly nicer:

\def\dotalign{\catcode`\.=4\begin{tabular}{r@{.}l}}
\def\enddotalign{\end{tabular}\catcode`\.=12}

This allows me to write:

\begin{dotalign}
5.8 \\
111.222 \\
.5 \\
\end{dotalign}

and I think should do align the dots properly.  In case you can't
tell, it works by temporarily telling TeX that . is an alignment
character like &.

I often change catcodes temporarily to make my input look nicer; I've
been known to make newline active and define it to do what \\ does, so
I don't have \\s cluttering up my input.

marcel@cs.caltech.edu (Marcel van der Goot) (01/31/91)

There have been quite some postings concerning alignment of decimal
points, with more and more complicated macro sets. (Nothing wrong
with that.) However, in the TeXbook Knuth gives a simple solution
(well, it's more a "fix") to the problem.
Quoting:

> This isn't especially pleasing to the eye, but that's what
> people do, so you might have to conform to the practice.
> [...]
> But the author usually prefers to use another, less sophisticated
> method, which takes advantage of the fact that the digits 0, 1, ..., 9
> have the same width in most fonts: You can choose a character that's
> not used elsewhere in the table, say `?', and change it to an active
> character that produces a blank space exactly equal to the width of
> a digit. Then it's usually no chore to put such nulls into the table
> entries so that each column can be regarded as either centered or
> right-justified or left-justified. For example. `??0.2010' and
> `297.1???' have the same width, so their decimal points will line up
> easily. Here is one way to set up `?' for this purpose:
> 
> 	\newdimen\digitwidth
> 	\setbox0=\hbox{\rm0}
> 	\dgitwidth=\wd0
> 	\catcode`?=\active
> 	\def?{\kern\digitwidth}
> 
> The last two definitions should be local to some group, e.g., inside a
> \vbox, so that `?' will resume its normal behavior when the table is
> finished.

This is indeed not very elegant, but simple and reliable.

                                          Marcel van der Goot
 .----------------------------------------------------------------
 | Blauw de viooltjes,                    marcel@vlsi.cs.caltech.edu
 |    Rood zijn de rozen;
 | Een rijm kan gezet
 |    Met plaksel en dozen.
 |