Math In Groff

Sat, 22 Apr 2023 12:27:26 -0500

If you ever need to write math papers, Groff provides a great preprocessor for working with basic and advanced math equations. If you've never heard of groff, read this article here. Let's jump right into it.

The basics

We will be using a simple MS document. All simple math equations in Groff are enclosed in a .EQ/.EN block (intuitively, this stands for equation and equation end). Let us begin with the equation for a standard line in ℝ.

.EQ
y = mx + b
.EN

When complied, this block will be centered and italicized as will all math blocks. If you are familiar with TeX or other math formatting markup, the syntax is similar, but there are some substantial differences. In fact, I would argue that the Groff syntax is far more human readable than TeX.

For example, to create characters with bars or hats, we can simply write the following:

.EQ
x bar = x hat 
.EN

You can see that the eqn syntax is very similar to how you would say it in English. Greek letters work much in the same way. We just have to write out in plain English what character we want.

.EQ
 beta, gamma, sigama
.EN

This would output something like this:

β, γ, σ.

Other operations are quite similar. Suppose that we want to use super scripts and sub scripts. Let's look at the following example,

.EQ
y sub 1 = x sub 1 + x sub 2 
.EN

This would create the following:

y1 = x1 + x2.

common errors

Here are a few common errors people make writing equations. The first is not putting a space between characters. Take the following example.

.EQ
(beta + alpha )
.EN

This would not compile properly. Eqn would see the character (beta as an entire character and just print out the text. You will get something like this:

(beta + α).
The correct way to format this is to put a space between the parentheses and the character like this: ( beta + alpha ) so you get the desired output:
(β + α).

Another error is to forget to contain your multi-character subscripts and super scripts in brackets. You might want to write Fa,b, so you write the following markup:

.EQ
F sub a ,b
.EN

This will yield something like Fa,b instead. The correct formatting would be this

.EQ
F sub { a, b }
.EN

complex equation examples

Let's write the markup for the quadratic formula,

.EQ
x = { -b +- sqrt { b sup 2 - 4ac } }  over { 2a }
.EN

Sums work in a similar way. We will use the formula

.EQ
x bar = 1 over n cdot sum from i=1 to n x sub i
.EN

Compiling your document

Again, we will be outputting our document as a PDF file. Notice that the command is identical aside from the -e flag. This tells Groff to use the eqn preprocessor so it knows what to do when it encounters the .EQ/.EN blocks.

$ groff -e -ms math.ms -Tpdf > math.pdf

There is also another, slightly more complicated, way to compile these documents with the eqn command. With the same document we use it as follows,

$ eqn math.ms -Tpdf | groff -ms -Tpdf > math.pdf

More advanced operations

inline math

Users of TeX might be wondering if they can create inline math equations using eqn. The answer is yes. At the top of your document, in a .EQ/.EN block you can specify a delimiter for inline math equations. If you are coming over form TeX, then you may as well use $ so you don't have to completely reinvent the wheel.

.EQ
delim $$
.EN
.PP
Now I am writing a regular paragraph. Did you know that $pi cdot r sup 2$ is
the area of a circle?

variables

Another neat feature of eqn is the ability to set variables. Say, for example, I'm writing a paper on simple linear regression and I have to typeset the RSS and SST over and over again (for those unaware, RSS and SST shows up in a lot of equations when discussing regression). Eqn allows me to set a variable for use later on. The general syntax is as follows:

.EQ
define variable `<eqn syntax>`
.EN

Here is how we might crate a variable for the RSS and the SST:

.EQ
define RSS `sum from i=1 to n { e hat sub i } sup 2`
define SST `sum from i=1 to n { ( Y sub i - Y bar ) sub 2  }`
.EN

Then, later in our document, we can typeset the following.

.EQ
R sup 2 = 1 - RSS over SST
.EQ

Limitations

While I really like eqn and Groff, I would be lying if I said there were no draw backs. Here is a short list of some problems.

  1. There is no easy way to get black board characters like ℝ.
  2. eqn is missing many essential logical quantifiers such as ∀.

I have come up with some easy solutions to this problem by setting variables. For the black board characters, I simply make them bold. At the top of the document I define the following.

.EQ
define NN `bold N`
define RR `bold R`
define CC `bold C`
.EN

The logical quantifiers are a bit tricker, but there is a work around. You can explicitly call certain unicode characters found at this link link or on the man page groff_char.7.en. Either way, here are some that I use frequently.


.EQ
define in  `\[mo]`
define forall `\[fa]`
define exists `\[te]`
define implies `\[rA]`
define iff `\[hA]`
.EN

Macro Guide

There are more symbols than the ones listed. However, I think this is a good place to start.

greek

Markup Output Markup Output
alpha α ALPHA Α
beta β BETA Β
gamma γ GAMMA Γ
pi π PI Π
sigma σ SIGMA Σ
delta δ DELTA Δ
rho ρ RHO Ρ
epsilon ε EPSILON Ε
kappa κ KAPPA Κ
theta θ THETA Θ
phi φ PHI Φ
xi ξ XI Ξ
chi χ CHI Χ
zeta ζ ZETA Ζ

symbols

Markup Output Markup Output
cdot ˙ inf
times × approx
>= <=
== !=
sum int
+- ± half ½
partial grad
union inter

functions

Markup Output Markup Output
cos {x} cos x sin {x} sin x
tan {x} tan x sinh {x} sinh x
cosh {x} cosh x tanh {x} tanh x
ln {x} ln x log {x} log x

markup

Markup Output Markup Output
x sub n xn x sup n xn
g dot ġ y dotdot ÿ
{ } bar { } under _
y hat ŷ sqrt { }

Resources


  1. Man Pages: man eqn
  2. Example MS document and resulting PDF: math.ms math.pdf.
  3. Unicode glyphs in Groff