Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
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. –  Yiannis Lazarides 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:) –  Yiannis Lazarides Feb 26 '11 at 21:50

2 Answers 2

up vote 3 down vote accepted
\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

share|improve this answer

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

share|improve this answer
    
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. –  Yiannis Lazarides 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 tex.stackexchange.com/questions/12481/… –  Yiannis Lazarides 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.