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

myb100@csc.anu.edu.au (01/30/91)

In article <1991Jan25.114530.2289@vax5.cit.cornell.edu>, nqdy@vax5.cit.cornell.edu writes:
> 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

Hi

	One of the people in our computer section found the following
stuff somewhere a loooong time ago. Anyway, it allows you to use
'd' in the column description the same way as 'l' 'r' and 'c'. This
provides you with decimal point alignment of numbers (or commas, for the
Europeans out there, who don't like decimal points, or any other character)

What follows: (i) a system message describing 'decalign.sty', (ii) an
example file in LaTeX and (iii) the decalign.sty file itself. 

The example is, I think, excessively complex. Just use 'd' in place of
'lrc' in the usual way.

Hope it helps ! If the originator of this stuff recognises his/her work, please
step forward for the recognition you deserve.

Any questions should be sent to Stephen Meatheringham (sjm@merlin.anu.edu.au)
 - our computer section TeX guru. (He doesn't know I said that :-) )

===============================================================================
 Markus Buchhorn                                           ///  | This space
 Mt Stromlo and Siding Spring Observatories, Canberra     ///   | 
 PMB Weston Ck. P.O. A.C.T. 2611, Australia           \\\///    | intentionally
 markus@mso.anu.edu.au                                 \XX/     | left blank
===============================================================================

Begin include:
-----------------------------------------------------------------------------

System message:

A problem occurs in TEX and LATEX when you try to align columns in a table on
the decimal point (or any other character).   There is now a document style
option in LATEX called `decalign', which adds a new column specifier to the
tabular and array environments.  Use a `d' to indicate that a column should
contain entries aligned on the decimal point.  An optional character after the
`d' indicates the character to be used as the separator; the default is a
period.  To use this option simply put :
   \documentstyle[decalign]{report}    <---- decalign in the document style

Use a `d' to indicate that a column should contain entries aligned on the
decimal point.  An optional character after the d indicates the character to be
used as the separator; the default is a period, but Europeans will probably use
commas. 

To align along the decimal point in plain TeX, define two columns in the
template like this:
  &\catcode`.=4 \tabskip0pt \hfil#&.#\hfil\tabskip=<whatever> &

                                 Stephen Meatheringham

-----------------------------------------------------------------------------
Example document:

\documentstyle[decalign]{report}

\begin{document}

\begin{tabular}{|r||r@{--}l|p{1.2in}|d,|}   % Separator will be a comma.
\hline
\multicolumn{6}{|c|}{GG\&A Hoofed Stock}
  \\ \hline\hline
&\multicolumn{2}{c|}{Price}& &
\multicolumn{2}{c|}{}              % Must treat the d column as two columns.
\\ \cline{2-3}
\multicolumn{1}{|c||}{Year}
& \multicolumn{1}{l@{\,\vline\,}}{high}
& low & \multicolumn{1}{c|}{Comments} & \multicolumn{2}{c|}{Other}  % here too.
  \\ \hline
1971 & 97 & 245 & Bad year for farmers in the west. & 23,45 \\ \hline
72 & 245 & 245 & Light trading due to a heavy winter. & 435,23 \\ \hline
73 & 245 & 2001 & No gnus was very good gnus this year. & 3,8756 \\ \hline
\end{tabular}

\end{document}

%  There are a few caveats.  The `column' is actually two columns, so one
%  must remember that when using \multicolumn or \cline.  Also, to skip
%  the column, you can't just put an ampersand, because that will only skip
%  one column.  You can't put two ampersands, either, because the separator
%  is automatically included as the first thing in the second column.  You
%  must use \multicolumn{2}.  None of the other column specifiers can be used
%  as separators, and the use of certain characters (&, {, }, #, etc.) will
%  probably produce chaos.  This is not a severe inconvenience, because only
%  periods and commas are likely to be used.  Finally, the `d' specifier
%  makes no sense in the {col} argument of the \multicolumn command, and
%  will in fact wreak havoc there.

-----------------------------------------------------------------------------
DECALIGN.STY:



\typeout{Document Style option 'decalign', Released 1 October 1987}
\def\dsep{}  % This is necessary for some unknown reason.
\def\@testpach#1{\@chclass \ifnum \@lastchclass=\tw@ 4 \else
    \ifnum \@lastchclass=3 5 \else
     \z@ \if #1c\@chnum \z@ \else
                              \if #1l\@chnum \@ne \else
                              \if #1r\@chnum \tw@ \else
          \@chclass \if #1|\@ne \else
                    \if #1@\tw@ \else
                    \if #1p3 \else
                    \if #1d7 \else
                    \ifnum \@lastchclass=7 8 \else \z@ \@preamerr 0\fi
  \fi  \fi  \fi  \fi  \fi  \fi  \fi  \fi
\fi}
%  Additions to these macros handle the extra classes.
\def\@mkpream#1{\@firstamptrue\@lastchclass6
\def\@preamble{}\def\protect{\noexpand\protect\noexpand}\let\@sharp\relax
\let\@startpbox\relax\let\@endpbox\relax
\@expast{#1}\expandafter\@tfor \expandafter
  \@nextchar \expandafter:\expandafter=\@tempa\do{\@testpach\@nextchar
  \ifnum \@lastchclass=7 \ifnum \@chclass=8 \else\@classviii{.}\fi\fi
  \ifcase \@chclass \@classz \or \@classi \or \@classii \or \@classiii
    \or \@classiv \or\@classv \or \or\@classvii \or\@classviii{\@nextchar}%
    \fi\@lastchclass\@chclass}%
\ifcase \@lastchclass \@acol
    \or \or \@preamerr \@ne\or \@preamerr \tw@\or \or \@acol \fi}
\def\@tabular{\leavevmode \hbox \bgroup $\let\@acol\@tabacol
   \let\@classz\@tabclassz
   \let\@classiv\@tabclassiv
   \let\@classviii\@tabclassviii
   \let\\\@tabularcr\@tabarray}
\def\array{\let\@acol\@arrayacol \let\@classz\@arrayclassz
   \let\@classiv\@arrayclassiv
   \let\@classviii\@arrayclassviii
   \let\\\@arraycr\def\@halignto{}\@tabarray}
\def\@arrayclassz{\ifcase \@lastchclass \@acolampacol \or \@ampacol \or
   \or \or \@addamp \or
   \@acolampacol \or \@firstampfalse \@acol \or
   \@acolampacol \or \@acolampacol
   \fi
\edef\@preamble{\@preamble
  \ifcase \@chnum
     \hfil$\relax\@sharp$\hfil \or $\relax\@sharp$\hfil
    \or \hfil$\relax\@sharp$\fi}}
\def\@tabclassz{\ifcase \@lastchclass \@acolampacol \or \@ampacol \or
   \or \or \@addamp \or
   \@acolampacol \or \@firstampfalse \@acol \or
   \@acolampacol \or \@acolampacol
   \fi
\edef\@preamble{\@preamble
  \ifcase \@chnum
     \hfil\ignorespaces\@sharp\unskip\hfil
     \or \ignorespaces\@sharp\unskip\hfil
     \or \hfil\hskip\z@ \ignorespaces\@sharp\unskip\fi}}
\def\@classi{\ifcase \@lastchclass \@acol \@arrayrule \or
   \@addtopreamble{\hskip \doublerulesep}\@arrayrule\or
   \or \or \@arrayrule \or
   \@acol \@arrayrule \or \@arrayrule \or
   \@acol \@arrayrule \or \@acol \@arrayrule
   \fi}
\def\@classiii{\ifcase \@lastchclass \@acolampacol \or
   \@addamp\@acol \or
   \or \or \@addamp \or
   \@acolampacol \or \@ampacol \or
   \@acolampacol \or \@ampacol
   \fi}
\def\@classvii{\ifcase \@lastchclass \@acolampacol \or
   \@addamp\@acol \or
   \or \or \@addamp \or
   \@acolampacol \or \@ampacol \or
   \@acolampacol \or \@acolampacol \fi}
\def\@tabclassviii#1{\@addtopreamble{\catcode`#1=4 \hfil\ignorespaces\@sharp
   \unskip&#1\@sharp\unskip\hfil}}
\def\@arrayclassviii#1{\@addtopreamble{\catcode`#1=4 \hfil$\relax\@sharp$&
   $\relax.\@sharp$\hfil}}
%%%---------------End o' example file.
-----------------------------------------------------------------------------

panos@db.toronto.edu (Panos Economopoulos) (02/04/91)

I repost a beautifully simple solution by Eric Domenjoud (domen@loria.crin.fr)
that appeared in this newsgroup some time ago. 
--------------------------------------------------------------

   1) How does one align a column of numbers which need to be right aligned
   under a central heading?

   e.g.        The Heading
		    27
		   113
		  1251
		     4
		  etc.....

\begin{tabular}{c}
The Heading \\
	\begin{tabular}{r}
	  27 \\
	 113 \\
	1251 \\
	   4
	\end{tabular}
\end{tabular}

   2) What do you do if you have a lot of numbers to be aligned on a decimal
   point if the numbers have varying numbers of decimal places?

   e.g.           10.7
		   1.3
		   0.75
		   etc....

\begin{tabular}{r@{.}l}
	10&7 \\
	 1&3 \\
	 0&75
\end{tabular}
------------------------------------------------------------