Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!ucbvax!WUVMD.BITNET!C38871CF From: C38871CF@WUVMD.BITNET ("Charles H. Franklin") Newsgroups: comp.text.tex Subject: Decimal Alignment in LaTeX tables Message-ID: <425D375D00003736@Post-Office.UH.EDU> Date: 29 Apr 91 06:27:23 GMT Sender: daemon@ucbvax.BERKELEY.EDU Lines: 151 Since asking about decimal alignment on Saturday I've received about a dozen responses. I can't believe how helpful you all are. After trying unsuccessfully several times in the past to get a solution to decimal alignment, it is really nice to get so much help so fast. From the comments in the replies, it seems I am not the only one who has wanted decimal alignment. So I thought I would post a summary of the various solutions so we can all see what the options appear to be. 1) Use \hphantom to add spaces where needed. eg \newcommand{\Z}{\hphantom{0}} \newcommand{\M}{\hphantom{$-$}} then 1.0\Z\Z & \M0.0003 0.333 & $-0.0001$ line up like you want. {Note you can also do this for the ")" on the standard errors in my example. This is essentially the solution I have been using.} 2) Use the @{.} argument in the tabular declaration to create two columns with no spaces and a decimal point between them. eg \begin{tabular}{r@{.}l} 10&7 \\ 1&3 \\ 0&75 \end{tabular} produces 10.7 1.3 0.75 3) Use DECALIGN.STY (available from FILESERV@SHSU) which implements a "d" alignment argument for the tabular environment. This "d" alignment type is in fact a version of (2) above. It defines two columns with a decimal between them and uses the decimal as the column separator, thus allowing you to write 3.14 instead of 3&14. This means that "d" is really r@{.}l, so you must remember that it is in fact two columns instead of one, important when making the headings for the column. 4) Follow the lead of REVTEX and define \dec x.xx which produces a box of equal width on both sides of the decimal place. When used with a center aligned column, this produces decimal alignment. To see the REVTEX code, scan REVTEX.STY for the string "\dec". (Or see below.) OK, those are the proposed solutions. Each will work. Obviously preferences over these is partly subjective and I guess they depend on what you are used to. But here are my own thoughts on the solutions, and a solution of my own. 1) The \hphantom space is what I've been doing. It suffers from having to think too much in constructing tables, and more so from the problem of what to do when you have too much, rather than too little, after the decimal. For example, if an asterisk is used to indicate a statistically significant coefficient, eg 2.227* then the problem with alignment is the asterisk takes up too much space. Fixing this using phantom space is possible but painful. 2) and 3) The r@{.}l solution is attractive because it is built in to LaTeX. The DECALIGN style is really the same thing but makes "d" do what r@{.}l does, plus a little more. Either of these is workable. My objection is the fact that this produces two columns masquerading as only one. I'm sure those who use this solution quickly get used to it, but I think a column should be a column and not two. With DECALIGN this is especially inconsistent, since "d" looks just like "r" or "l" but it behaves quite differently. This caused me trouble when I tried DECALIGN on a table I had in a paper. The table used blank cells for some coefficients which where estimated in one column but not in others. This gave DECALIGN fits. Presumably because my one column of blank cells was now only half of the expected two columns. Certainly I could fix it, but I don't like that kind of inconsistency. I also don't like 3&14 in place of 3.14, (though this can be avoided with DECALIGN), because it seems more error prone. 4) \dec x.xx has the major drawback that you have to enter it for every cell you want aligned on the decimal. A global declaration would be much nicer. In the end, I've settled on 4) as the less worse solution. A "d" alignment primitive in LaTeX is the obvious "right" solution, but is not available. For me, I think the "two-columns-in-one" solution of 2) and 3) is just too inconsistent with the rest of the information in the table. So I've opted for the \dec x.xx solution, even though it has the problem of having to include it in each cell. This seems to me better because it keeps the consistency of the column declarations. Plus, the style file required to implement this is much smaller than that for DECALIGN. An unexpected benefit of using the \dec approach is that other entries in the column need not be decimal aligned and are easily manipulated. For example, at the bottom of a column of regression coefficients it is customary to have the number of cases, the r-square and assorted other stuff. These entries are probably best given right aligned, which is easily done when using \dec. I've put together a little file called DECTAB.STY which is appended below. It steals the \dec definitions from REVTEX and adds a little for convenience. You may want to redefine \dec to \da or even \d to reduce the required typing in tables. I've stuck with \dec simply for clarity. DECTAB.STY also provides \la, \ra and \ca for local left, right and center alignment. This is often convenient and again saves typing. I've tried it for one whole day on my tables and it seems to work. Beyond that, I offer no warranties. If you find problems with it, please let me know. Once more, thanks to all who sent their advice. Even if I don't take it, I have profited from it. ----------------------------Begin DECTAB.STY-------------------------------- % DECTAB.STY (April 27, 1991) % Allows decimal alignment in tabular environment % \dec definitions taken from REVTEX.STY % by Charles Franklin, April 27, 1991 % (C38871CF@WUVMD.WUSTL.EDU or C38871CF@WUVMD.Bitnet) % % USAGE: For decimal alignment in a table, use \dec 3.14 for % the cell entry. The column should be center aligned % to achieve the desired effect. % For example: % % \begin{tabular}{lcc} % % Constant & \dec -1.461 & \dec -0.724 \\ % Standard Error & \dec (0.422) & \dec (0.455) \\ % % Note that a `.' MUST appear for the alignment to work. % Use `\dec 520.' NOT `\dec 520' for integers. % % DECTAB also defines \ra, \la, \ca for easy modification of % global alignments. These produce local right, left and center % alignments of the cell entry. If there are spaces in the cell % entry, enclose the contents in {...}, eg \la {This Column} % \typeout{Document Style Option `dectab', version April 27, 1991} \newdimen\@Ldec \newdimen\@Rdec \def\dec #1.#2 {\hbox to\@Ldec{\hss#1}\def\test{#2}% \ifx\test\empty\hbox to\@Rdec{\hfill}\else\hbox to\@Rdec{.#2\hss}\fi } \def\setdec #1.#2 {\relax% \bgroup \setbox0\hbox{\kern1pt\relax#1}\setbox1\hbox{\kern1pt\relax#2}% \global\@Ldec\wd0\global\@Rdec\wd1% \egroup } \setdec 000.000 % default size of 3 digits on each side % Following defs allow easy modification of global alignment in % tables. \ra, \la, \ca for right, left and center alignments. \def\ra #1 {\multicolumn{1}{r}{#1}} \def\la #1 {\multicolumn{1}{l}{#1}} \def\ca #1 {\multicolumn{1}{c}{#1}} % end DECTAB.STY