9

I have a number of tables in a book that I am busy with that have quite a few tables of the general form shown below:

enter image description here

If you notice the fractional part of the numbers 5 1/2 and 16 1/2 protrude to the right (the numbers are right justified).

For this particular example, I achieved the result by adding a phantom character on all the figures except the fractional numbers, for example:

  &5280\z ...
  &660\z ...
  &$16\frac{1}{2}$ ...
  ...

where, \z is defined as \def\z{\phantom{0}}

Any suggestions as to how to automate this?

The figures are all auto-generated by TeX. The input is only the first two rows. Based on this the rest of the numbers are calculated. Fractions are only displayed for numbers ending with 0.25, 0.5 etc..These are detected automatically, so I can add a kern if necessary. Tried this but did not look so good.

Sorry for not posting a minimal as the code is too lengthy.

lockstep
  • 250,273
yannisl
  • 117,160
  • 5
    I applaud you for using oldstyle figures even within tables. – lockstep Feb 26 '11 at 21:11
  • 2
    Couldn't you just use \rlap{\frac{1}{2}} for the fractions? – morbusg Feb 26 '11 at 21:27
  • Have you considered using slightly nicer fractions? Here is a macro that I found somewhere in Knuth's code for nice fractions:

    \def\nicefrac#1/#2{\leavevmode \raise.5ex\hbox{\the\scriptfont0 #1} \kern-.1em/\kern-.15em \lower.25ex\hbox{\the\scriptfont0 #2}}

    – Christian Lindig Feb 26 '11 at 21:41
  • @morsburg never thought of it will give it a try, thanks. – yannisl Feb 26 '11 at 21:47
  • @Christian Thanks! I normally use \xfrac for fractions. Don't really know why I didn't use it here. I guess I was concentrating on the coding part:) – yannisl Feb 26 '11 at 21:50
  • Isn't your table missing the chain? (Four poles, or four rods, as I am more used to calling them.) – Harald Hanche-Olsen Feb 26 '11 at 21:56
  • @Harald it misses the chain and the links plus quite a few others. They are all in other tables as well as the greek pēchys, cubit etc. – yannisl Feb 26 '11 at 22:06
  • Okay … anyhow, you have neglected to say if you're using LaTeX, plain or some other format. It would seem to make a difference here. – Harald Hanche-Olsen Feb 27 '11 at 08:53
  • @Harald I am using LaTeX. – yannisl Feb 27 '11 at 14:18
  • May I suggest the booktabs package? It constructs very beautiful tables and the documentation contains lots of tips on how to create typographically nice tables.

    For example, all those vertical lines are advised against.

    – Ben Feb 27 '11 at 14:57
  • @Ben This is specifically defined as a stepped table. In this case allow me to disagree both with you and the author of booktabs. removing the verticals wouldn't look very nice in this case:) – yannisl Feb 27 '11 at 16:12
  • Since this question was the top unanswered question, I had to take a stab at it! – Alan Munn Mar 14 '11 at 03:21

2 Answers2

3

How about using the dcolumn package?

\documentclass{article}
\usepackage{xfrac}
\usepackage{dcolumn}
\begin{document}
\newcolumntype{d}{D{.}{\,}{2}}
\newcommand*\mc[2]{\multicolumn{#1}{c|}{#2}}
\begin{tabular}{|r|r|r|d|d|}
\hline
\multicolumn{1}{|c|}{Mile} & \mc{1}{Furlong} & \mc{1}{Pole} & \mc{1}{Yard} & \mc{1}{Foot}\\
\hline
1 & 8 & 320 & 1760 & 5280\\
\hline
\mc{1}{} & 1 & 40 & 220 & 660\\
\cline{2-5}
\mc{2}{} & 1 & 5.\sfrac{1}{2} & 16.\sfrac{1}{2}\\ % note . delimeter
\cline{3-5}

\end{tabular}
\end{document}

This aligns the numbers correctly. The input uses the dot as the marker of where the fraction begins, but using dcolumn's ability to change the output marker prints a thin space.

This solution will only work if the columns that have fractions don't also have decimals as in your last column in the example. Also, if you know in advance that some columns won't have any decimals, then you could adjust some of those columns to a simple r column as I did in the example.

code result

David Carlisle
  • 757,742
Alan Munn
  • 218,180
  • thanks, unfortunately this will not work for what I am trying to do. The first two lines of the table are inputted as comma delimited lists. I then generate the conversion factors automatically. The tables are very long - but can print between ranges. When the conversion factors are generated I pass them onto another routine that converts the decimal part to fractional only if it makes nice fractions, i.e., if it is 12.567 it will not. All these is to keep in the spirit of the old units. This means that I do not always know the columns that need fractions. – yannisl Mar 14 '11 at 14:37
  • ..continued, so far from experimenting, for this type of situation it is better to use raw TeX and \halign. So far my thinking is to apply a macro to all cells inserting a phantom at the end - except if there is a fraction I need to set a global boolean. Using rlap as suggested in other comments does no work nice for "fatter" fractions. Just in case you wondering all these are for a book I am busy with on and off discussing weights and measures from antiquity to present. See also for the fractional part http://tex.stackexchange.com/questions/12481/how-to-break-out-of-a-loop – yannisl Mar 14 '11 at 14:42
  • @Yiannis Oh well. It wasn't quite clear how much you would and wouldn't know about the contents of a column. – Alan Munn Mar 14 '11 at 15:29
3
\documentclass{article}
\usepackage{array,mathtools}
\newcolumntype{R}{>{$}r<{\phantom{2}$}}
\def\Frac#1#2{\mathrlap{\frac{#1}{#2}}}
\newcommand\mc[2]{\multicolumn{#1}{c|}{#2}}

\begin{document}
\def\arraystretch{1.2}
\begin{tabular}{|*5{R|}}\hline
\multicolumn{1}{|c|}{Mile} & \mc{1}{Furlong} & \mc{1}{Pole} & \mc{1}{Yard} & \mc{1}{Foot}\\\hline
1 & 8 & 320 & 1760 & 5280\\\hline
\mc{1}{}  & 1 & 40 & 220 & 660\\\cline{2-5}
\mc{2}{} & 1 & 5\Frac{1}{2} & 16\Frac{1}{2}\\\cline{3-5}
\end{tabular}
\end{document}

enter image description here