Typesetting arbitrary fractions

Last-Updated: 2021-08-30

Sources

Description of the issue

Some fractions are readily available in groff like ½, ¾ or ⅝. The following macro tries to extend this to all fractions.

The .frac macro

In this macro (2005-11/msg00029) written by Ted Harding, we are going to use the PostScript symbol named "fraction" whose groff name is "\(f/" to create the .frac macro with the goal to use it like so:

.frac numerator denominator

Here is the definition:

.de frac
.nr n0 \\n[.s]
.nr n1 \\n[n0]*6/10
.nr dn (\\n[n0]-\\n[n1])*8/10
\v'-\\n[dn]p'\s[\\n[n1]]\\$1\s0\v'\\n[dn]p'\[f/]\s[\\n[n1]]\\$2\s0
..

You can test it:

\f[HR]\s[40]\(12
.frac 1 2
.frac 999 1000
.sp
\f[TR]\s[30]\(12
.frac 1 2
.frac 999 1000
.sp
\s[30]\(12\f[TB]
.frac 1 2
.frac 999 1000

Note: Be wary of the registers used here and adapt them to fit your use case. The name used caused issues in the past (2013-09/msg00022).

← Go back