A Guide To Typesetting Mathematics Using GNU Eqn
A Guide To Typesetting Mathematics Using GNU Eqn
Ted Harding
Preamble
Like the other troff preprocessors pic and tbl,
which allow the user to format a complex structure
(diagram, table) by typing in a specification of it
expressed in a description languagea reasonably
close approximation to plain English instructions,
so eqn provides a mechanism for specifying how to
lay out mathematical notation. The plain English
aspect of eqn in fact resembles what a lecturer
might speak out loud while writing mathematics on
a blackboard.
Encapsulating eqn code
In order for eqn to recognise a block of user input
as something that eqn itself should interpret, rather
than pass unchanged through to troff, the start
and the end of the eqn block need to be flagged by
marks that eqn will recognise. Then, when eqn
recognises the start of a block, all that follows will
be interpreted by eqn, and transformed into troff
code which will result in suitably formatted output,
until the end of the block is recognised. Then eqn
will revert to ignoring the input until it recognises
the next start of an eqn block.
There are two mechanisms for marking the start and
end of an eqn block. The first is for displayed
equations, which are set apart from surrounding text
by being printed on their own separate lines. In this
case, the start of the block is flagged by .EQ at the
beginning of an input line, and the end of the block
is flagged by .EN at the beginning of an input line.
These serve two purposes. Firstly, the .EQ and
.EN are recognised by eqn as delimiting code that
eqn is to interpret. Secondly, eqn will pass the
.EQ and the .EN through to troff; then
troff itself will evoke macros EQ and EN which
will position the troff-formatted mathematics on
the page. The various standard macro packages
(me, mm, ms) embody implementations of the EQ
and EN macros. The mm implementation behaves
somewhat differently from the other two. Users
may also write their own implementations.
The second is for in-line mathematics, in which
mathematical expressions occur within running text.
Here the eqn blocks are flagged by delimiters
placed at the start and end of the block. The default
delimiter for both ends is $, placed at the start and
at the end. The user may define other delimiters to
be used instead of $...$.
The following section illustrates the most simple
and basic usage of eqn, and shows the formatted
mathematical outputs.
Ted Harding
19 January 2011
Basic Use
Simple algebraic expressions
These are entered much as one would write them on
paper. For example:
.EQ
Z = X + Y
.EN
will produce:
Z = X +Y
which is (by default) centred. By following the .EQ
by a parameter, it may be forced to be left-justified
(L), or indented (I):
.EQ L
Z = X + Y
.EN
Z = X +Y
.EQ I
Z = X + Y
.EN
Z = X +Y
It is also possible to explicitly use C for a centred
equation, but this is already the default.
The above is for the me and ms macro packages; the
mm package behaves differently. With mm, whatever
follows .EQ will be treated as an equation label
(see below); the entire .EQ ... .EN block
needs to be itself encapsulated between .DS and
.DE tags, with justification (left, indented, centred,
or right) controlled by a parameter to .DS, as in
.DS L, .DS I, .DS C, .DS CB, .DS R, .DS RB
(see man groff_mm for details). An example of
how to add an equation number (or label):
.EQ (3)
Z = X + Y
.EN
Z = X +Y
(3)
Page 1
Last Revised
27 October 2013
19 January 2011
a + bX
U
=
c + dX
V
U
X
= a + b + dX
V
c
b
b2 4ac
x=
2a
Note the special token +-, which generates the
plus-or-minus symbol. While $+-$ generates ,
$+ -$ would generate + , so it is important to
pay attention to spaces which separate tokens. The
two spacing tokens and act like spaces in this
respect, so do not need to be separated from their
surroundings (see some of the examples of eqn
code above).
As a general rule of good practice, when entering
eqn code, it is wise to explicitly separate distinct
entities, since eqn may (wisely) generate slightly
different layout with spacing, in some cases, which
is better suited to mathematical typography.
Note how what you type for eqn resembles how
you might read the above equation out loud:
x equals: minus b plus-or-minus the square
root of b-squared-minus-4-a-c, all over 2 a
Page 2
Last Revised
27 October 2013
.EQ
define quadsol %{
$1 = {
- $3 +- sqrt {$3 sup 2 - 4 $2 $4}
} over {2 $2}
}%
.EN
For the quadratic equation
$a x sup 2 + b x + c = 0$,
the solution is:
.EQ
quadsol(x, a, b, c)
.EN
For the quadratic equation ax2 + bx + c = 0, the
solution is:
b
b2 4ac
x=
2a
For the quadratic equation
$P Y sup 2 + Q Y + R = 0$,
the solution is:
.EQ
quadsol(Y, P, Q, R)
.EN
For the quadratic equation PY 2 + QY + R = 0, the
solution is:
Q
Q2 4PR
Y=
2P
Big brackets for tall expressions: left and right
.EQ
( {a + b} over {c + d} )
.EN
a+b
)
(
c+d
certainly doesnt look right!
.EQ
define thing %{$5 + $8}%
thing( A, B, C, D, E, F, G, H, I )
.EN
E+H
.EQ
left (
{a + b} over {c + d}
right )
.EN
a + b
c + d
Ted Harding
19 January 2011
Page 3
Last Revised
27 October 2013
eqn
.EQ
pile {A above {c+d}
above {u sup 2 + v sup 2}
above {cos x}
}
.EN
A
c+d
u2 + v 2
cos x
eqn
.EQ
lpile {A above {c+d}
above {u sup 2 + v sup 2}
above {cos x}
}
.EN
A
c+d
u2 + v 2
cos x
F(X) = 1
if 14 X 34
4(1 X) if 3 X 1
4
Result
A
c+d
u2 + v 2
cos x
Ted Harding
19 January 2011
Result
A
B+C
D
E+F
Page 4
Last Revised
27 October 2013
= cos + i sin
where the three steps in the derivation correspond to
the three = signs, and the second step has itself
been split into two lines first, because of length;
secondly, to highlight the two separate infinite series:
one for cos , and one for sin . Note that the spacing
between the two lines of the second step is narrower
than the spacings between the steps, to emphasise
that these two lines belong together.
The eqn code for the above is shown below. There
are some new elements in it which will be explained
in the following part of this page. Each of the four
lines above has its own .EQ I ... .EN block,
(see below); and the spacings are achieved using
.sp 0.5 and .sp 0.25m .
.EQ I
e sup {i theta} mark =
1 + ( i theta ) +
1 smallover {2!}( i theta ) sup 2
+1 smallover {3!}( i theta ) sup 3
+1 smallover {4!}( i theta ) sup 4
+...
.EN
.sp 0.5m
.EQ I
lineup =
1 - 1 smallover {2!}theta sup 2
+ 1 smallover {4!}theta sup 4
+...
.EN
.sp 0.25m
.EQ I
lineup {hphantom{=1}} +i (theta
- 1 smallover {3!} theta sup 3
+ 1 smallover {5!} theta sup 5
+... )
.EN
.sp 0.5m
.EQ I
lineup = cos theta + isin theta
.EN
Ted Harding
19 January 2011
Page 5
Last Revised
27 October 2013
hphntmsrc
0h 0
0d 0
0skew 0
0skern 0
0s \\&\h\\n(0wu
vphntmsrc
0s
0w 0
0skew 0
0skern 0
phntmsrc
0s
0s \\&\h\\n(0wu
.EQ
define phantom special phntmsrc
define vphantom special vphntmsrc
define hphantom vcenter special hphntmsrc
.EN
Ted Harding
19 January 2011
Page 6
Last Revised
27 October 2013