<<

-- --

A System for Mathematics

Brian W. Kernighan and Lorinda L. Cherry Bell Laboratories Murray Hill, New Jersey 07974

ABSTRACT

This paper describes the design and implementation of a system for typesetting mathemat- ics. The language has been designed to be easy to learn and to use by people (for example, secretaries and mathematical typists) who know neither mathematics nor typesetting. Experience indicates that the language can be learned in an hour or so, for it has few rules and fewer excep- tions. For typical expressions, the size and changes, positioning, line drawing, and the like necessary to print according to mathematical conventions are all done automatically. For exam- ple, the input sum from i=0 to in®nity x sub i = pi over 2 produces ∞ π __ Σxi = i =0 2

The syntax of the language is speci®ed by a small -free grammar; a compiler- compiler is used to make a compiler that translates this language into typesetting commands. Output may be produced on either a phototypesetter or on a terminal with forward and reverse half-line motions. The system interfaces directly with text formatting programs, so mixtures of text and mathematics may be handled simply.

This paper is a revision of a paper originally published in CACM, March, 1975.

1. Introduction limits in the preceding example showed in its simplest ``Mathematics is known in the trade as form. This is carried further by dif®cult, or penalty, because it is slower, more b +______1 dif®cult, and more expensive to set in than any a 0 b 2 a +______other kind of copy normally occurring in and 1 b +______3 journals.'' [1] a 2 . . . a 3+ One dif®culty with mathematical text is the multiplicity of characters, sizes, and . An expression such as and still further by  sin 2x √ mx −√ lim (tan x ) = 1 ______1 ______a e b →π⁄  log x 2  2m √ ab √ a e mx +√ b requires an intimate mixture of roman, italic and  √ dx 1 − a greek letters, in three sizes, and a special character or ∫ ______= ______tanh 1(____ e mx ) ae mx −be −mx  √ √ two. (``Requires'' is perhaps the wrong word, but m ab b  √ mathematics has its own typographical conventions −1 − a ______coth 1(____ e mx ) which are quite different from those of ordinary text.)  m √ ab √ b Typesetting such an expression by traditional methods These examples also show line-drawing, built-up is still an essentially manual operation. characters like braces and radicals, and a spectrum of A second dif®culty is the two dimensional positioning problems. (Section 6 shows what a user character of mathematics, which the superscript and has to type to produce these on our system.) -- --

- 2 -

2. Photocomposition bars have to be made the right length and positioned Photocomposition techniques can be used to at the right height. And so on. Indeed a mechanism solve some of the problems of typesetting mathemat- for overriding default actions has to exist, but its ics. A phototypesetter is a device which exposes a application is the exception, not the rule. piece of photographic paper or ®lm, placing charac- We assume that the typist has a reasonable pic- ters wherever they are wanted. The Graphic Systems ture (a two-dimensional representation) of the desired phototypesetter[2] on the [3] ®nal form, as might be handwritten by the of a works by shining light through a character stencil. paper. We also assume that the input is typed on a The character is made the right size by lenses, and terminal much like an ordinary . the light beam directed by ®ber optics to the desired This implies an input of perhaps 100 charac- place on a piece of photographic paper. The exposed ters, none of them special. paper is developed and typically used in some form A secondary, but still important, goal in our of photo-offset reproduction. design was that the system should be easy to imple- On UNIX, the phototypesetter is driven by a ment, since neither of the had any desire to formatting program called [4]. TROFF was make a long-term project of it. Since our design was designed for setting running text. It also provides all not ®rm, it was also necessary that the program be of the facilities that one needs for doing mathematics, easy to change at any time. such as arbitrary horizontal and vertical motions, To make the program easy to build and to line-drawing, size changing, but the syntax for change, and to guarantee regularity (``it should work describing these special operations is dif®cult to learn, everywhere''), the language is de®ned by a context- and dif®cult even for experienced users to type free grammar, described in Section 5. The compiler correctly. for the language was built using a compiler-compiler. For this reason we decided to use TROFF as an A priori, the grammar/compiler-compiler ``assembly language,'' by designing a language for approach seemed the right thing to do. Our subse- describing mathematical expressions, and compiling it quent experience leads us to believe that any other into TROFF. course would have been folly. The original language was designed in a few days. Construction of a work- 3. Language Design ing system suf®cient to try signi®cant examples The fundamental principle upon which we required perhaps a person-month. Since then, we based our language design is that the language should have spent a modest amount of additional time over be easy to use by people (for example, secretaries) several years tuning, adding facilities, and occasion- who know neither mathematics nor typesetting. ally changing the language as users make criticisms This principle implies several things. First, and suggestions. ``normal'' mathematical conventions about operator We also decided quite early that we would let precedence, parentheses, and the like cannot be used, TROFF do our work for us whenever possible. for to give special meaning to such characters means TROFF is quite a powerful program, with a macro that the user has to understand what he or she is typ- facility, text and arithmetic variables, numerical com- ing. Thus the language should not assume, for putation and testing, and conditional branching. Thus instance, that parentheses are always balanced, for we have been able to avoid writing a lot of mundane they are not in the half-open interval (a ,b ]. Nor but tricky software. For example, we store no text should it assume that that √ a +b can be replaced by strings, but simply pass them on to TROFF. Thus we

1 1 (a +b ) ¤2 , or that 1⁄(1−x ) is better written as ____ (or avoid having to write a storage management package. 1−x Furthermore, we have been able to isolate ourselves vice versa). from most details of the particular device and charac- Second, there should be relatively few rules, ter set currently in use. For example, we let TROFF keywords, special symbols and operators, and the compute the widths of all strings of characters; we like. This keeps the language easy to learn and need know nothing about them. remember. Furthermore, there should be few excep- A third design goal is special to our environ- tions to the rules that do exist: if something works in ment. Since our program is only useful for typeset- one situation, it should work everywhere. If a vari- ting mathematics, it is necessary that it interface able can have a subscript, then a subscript can have a cleanly with the underlying typesetting language for subscript, and so on without limit. the bene®t of users who want to set intermingled Third, ``standard'' things should happen mathematics and text (the usual case). The standard automatically. Someone who types ``x=y+z+1'' mode of operation is that when a is typed, should get ``x =y +z +1''. Subscripts and superscripts mathematical expressions are input as part of the text, should automatically be printed in an appropriately but marked by user settable delimiters. The program smaller size, with no special intervention. Fraction reads this input and treats as comments those things -- --

- 3 -

which are not mathematics, simply passing them a+b over c+d+e = 1 through untouched. At the same time it converts the mathematical input into the necessary TROFF com- produces mands. The resulting ioutput is passed directly to a +b ______=1 TROFF where the comments and the mathematical c +d +e parts both become text and/or TROFF commands. Similarly, subscripts and superscripts are intro- 4. The Language duced by the keywords sub and sup: We will not try to describe the language pre- x 2+y 2=z 2 cisely here; interested readers may refer to the appen- dix for more details. Throughout this section, we will is produced by write expressions exactly as they are handed to the x sup 2 + y sup 2 = z sup 2 typesetting program (hereinafter called ``EQN''), except that we won't show the delimiters that the user The spaces after the 2's are necessary to mark the end types to mark the beginning and end of the expres- of the superscripts; similarly the keyword sup has to sion. The interface between EQN and TROFF is be marked off by spaces or some equivalent delimiter. described at the end of this section. The return to the proper is automatic. Multi- ple levels of subscripts or superscripts are of course As we said, typing x=y+z+1 should produce z allowed: ``x sup y sup z'' is x y . The construct x =y +z +1, and indeed it does. Variables are made ``something sub something sup something'' is recog- italic, operators and digits become roman, and normal nized as a special case, so ``x sub i sup 2'' is x 2 spacings between letters and operators are altered i instead of x 2. slightly to give a more pleasing appearance. i More complicated expressions can now be Input is free-form. Spaces and new lines in formed with these primitives: the input are used by EQN to separate pieces of the ∂2 2 2 input; they are not used to create in the output. ____ f =_x__ +_y__ Thus ∂x 2 a 2 b 2 x = y is produced by + z + 1 {partial sup 2 f} over {partial x sup 2} = also gives x =y +z +1. Free-form input is easier to x sup 2 over a sup 2 + y sup 2 over b sup 2 type initially; subsequent is also easier, for an Braces {} are used to group objects together; in this expression may be typed as many short lines. case they indicate unambiguously what goes over Extra white space can be forced into the output what on the left-hand side of the expression. The by several characters of various sizes. A tilde `` Ä '' language de®nes the precedence of sup to be higher gives a space equal to the normal word spacing in than that of over, so no braces are needed to get the text; a circum¯ex gives half this much, and a tab correct association on the right side. Braces can charcter spaces to the next tab stop. always be used when in doubt about precedence. Spaces (or tildes, etc.) also serve to delimit The braces convention is an example of the pieces of the input. For example, to get power of using a recursive grammar to de®ne the language. It is part of the language that if a construct = π ∫ ω f (t ) 2 sin( t )dt can appear in some context, then any expression in we write braces can also occur in that context. f(t) = 2 pi int sin ( t )dt There is a sqrt operator for making square roots of the appropriate size: ``sqrt a+b'' produces Here spaces are necessary in the input to indicate that √ a +b , and sin, pi, int, and omega are special, and potentially {− − { − }} worth special treatment. EQN looks up each such x = b + sqrt b sup 2 4ac over 2a string of characters in a table, and if appropriate gives is it a translation. In this case, pi and omega become −b ±√ b 2−4ac their greek equivalents, int becomes the integral sign x =______(which must be moved down and enlarged so it looks 2a ``right''), and sin is made roman, following conven- Since large radicals look poor on our typesetter, sqrt tional mathematical practice. Parentheses, digits and is not useful for tall expressions. operators are automatically made roman wherever Limits on summations, integrals and similar found. constructions are speci®ed with the keywords from Fractions are speci®ed with the keyword over: and to. To get ∞ Σxi →0 i =0 -- --

- 4 -

we need only type inch; this paper is set in 9 type.) sum from i=0 to inf x sub i −> 0 If necessary, an input string can be quoted in "...", which turns off grammatical signi®cance, and Σ Centering and making the big enough and the limits any font or spacing changes that might otherwise be smaller are all automatic. The from and to parts are done on it. Thus we can say both optional, and the central part (e.g., the Σ) can in fact be anything: limÄ roman "sup" Äx sub n = 0 lim from {x −> pi /2} ( tanÄx) = inf to ensure that the supremum doesn't become a super- : is lim sup xn =0 lim (tan x )=∞ x →π⁄2 Diacritical marks, long a problem in traditional Again, the braces indicate just what goes into the typesetting, are straightforward: from part. . ..  _x +xÃ+yÄ+XÃ +Y =z +Z There is a facility for making braces, , parentheses, and vertical bars of the right height, is made by typing using the keywords left and right: x dot under + x hat + y tilde left [ x+y over 2a right ]Ä=Ä1 + X hat + Y dotdot = z+Z bar

makes There are also facilities for globally changing   x +y default sizes and fonts, for example for making view- ____  = 1 graphs or for setting chemical equations. The  2a  language allows for matrices, and for lining up equa- A left need not have a corresponding right, as we tions at the same horizontal position. shall see in the next example. Any characters may Finally, there is a de®nition facility, so a user follow left and right, but generally only various can say parentheses and bars are meaningful. de®ne name "..." Big brackets, etc., are often used with another facility, called piles, which make vertical piles of at any time in the document; henceforth, any objects. For example, to get occurrence of the token ``name'' in an expression will  be expanded into whatever was inside the double  1 if x >0 quotes in its de®nition. This lets users tailor the sign (x ) ≡  0 if x =0 language to their own speci®cations, for it is quite  −1 if x <0 possible to rede®ne keywords like sup or over. Sec- tion 6 shows an example of de®nitions. we can type The EQN preprocessor reads intermixed text sign (x) Ä==Ä left { and equations, and passes its output to TROFF. Since rpile {1 above 0 above −1} TROFF uses lines beginning with a period as control ÄÄlpile {if above if above if} words (e.g., ``.ce'' means ``center the next output ÄÄlpile {x>0 above x=0 above x<0} line''), EQN uses the sequence ``.EQ'' to mark the beginning of an equation and ``.'' to mark the end. { The construction ``left '' makes a left brace big The ``.EQ'' and ``.EN'' are passed through to TROFF { } enough to enclose the ``rpile ... '', which is a right- untouched, so they can also be used by a knowledge- justi®ed pile of ``above ... above ...''. ``lpile'' makes able user to center equations, number them automati- a left-justi®ed pile. There are also centered piles. cally, etc. By default, however, ``.EQ'' and ``.EN'' Because of the recursive language de®nition, a pile are simply ignored by TROFF, so by default equations can contain any number of elements; any element of a are printed in-line. pile can of course contain piles. ``.EQ'' and ``.EN'' can be supplemented by Although EQN makes a valiant attempt to use TROFF commands as desired; for example, a centered the right sizes and fonts, there are times when the display equation can be produced with the input: default assumptions are simply not what is wanted. For instance the italic sign in the previous example .ce would conventionally be in roman. Slides and tran- .EQ sparencies often require larger characters than normal x sub i = y sub i ... text. Thus we also provide size and font changing .EN commands: ``size 12 bold {AÄxÄ=Äy}'' will produce Since it is tedious to type ``.EQ'' and ``.EN'' A x = y. Size is followed by a number represent- around very short expressions (single letters, for ing a character size in points. (One point is 1/72 -- --

- 5 -

instance), the user can also de®ne two characters to The grammar makes it obvious why there are serve as the left and right delimiters of expressions. few exceptions. For example, the observation that These characters are recognized anywhere in subse- something can be replaced by a more complicated quent text. For example if the left and right delim- something in braces is implicit in the productions: iters have both been set to ``#'', the input: eqn : box  eqn box Let #x sub i#, #y# and #alpha# be positive box : text  { eqn } produces: Anywhere a single character could be used, any legal construction can be used. Let xi , y and α be positive Clearly, our grammar is highly ambiguous. Running a preprocessor is strikingly easy on What, for instance, do we do with the input UNIX. To typeset text stored in ®le ``f '', one issues a over b over c ? the command: Is it eqn f  troff {a over b} over c The vertical bar connects the output of one process (EQN) to the input of another (TROFF). or is it a over {b over c} ? 5. Language Theory The basic structure of the language is not a To answer questions like this, the grammar is particularly original one. Equations are pictured as a supplemented with a small set of rules that describe set of ``boxes,'' pieced together in various ways. For the precedence and associativity of operators. In par- example, something with a subscript is just a box fol- ticular, we specify (more or less arbitrarily) that over lowed by another box moved downward and shrunk associates to the left, so the ®rst alternative above is by an appropriate amount. A fraction is just a box the one chosen. On the other hand, sub and sup bind centered above another box, at the right altitude, with to the right, because this is closer to standard b a line of correct length drawn between them. mathematical practice. That is, we assume x a is b The grammar for the language is shown below. x (a ), not (x a )b . For purposes of exposition, we have collapsed some The precedence rules resolve the ambiguity in productions. In the original grammar, there are about a construction like 70 productions, but many of these are simple ones used only to guarantee that some keyword is recog- a sup 2 over b nized early enough in the parsing process. Symbols We de®ne sup to have a higher precedence than over, 2 in capital letters are terminal symbols; lower case a 2 __ so this construction is parsed as ___ instead of a b . symbols are non-terminals, i.e., syntactic categories. b The vertical bar  indicates an alternative; the brack- Naturally, a user can always force a particular ets [ ] indicate optional material. A TEXT is a string parsing by placing braces around expressions. of non-blank characters or any string inside double quotes; the other terminal symbols represent literal The ambiguous grammar approach seems to be occurrences of the corresponding keyword. quite useful. The grammar we use is small enough to be easily understood, for it contains none of the pro-  eqn : box eqn box ductions that would be normally used for resolving box : text ambiguity. Instead the supplemental information  { eqn } about precedence and associativity (also small enough  box OVER box to be understood) provides the compiler-compiler with  SQRT box the information it needs to make a fast, deterministic  box SUB box  box SUP box parser for the speci®c language we want. When the  [ L  C  R ]PILE { list } language is supplemented by the disambiguating  LEFT text eqn [ RIGHT text ] rules, it is in fact LR(1) and thus easy to parse[5].  box [ FROM box ] [ TO box ] The output code is generated as the input is  SIZE text box scanned. Any time a production of the grammar is    [ROMAN BOLD ITALIC] box recognized, (potentially) some TROFF commands are      box [HAT BAR DOT DOTDOT TILDE] output. For example, when the lexical analyzer  DEFINE text text reports that it has found a TEXT (i.e., a string of con- list : eqn  list ABOVE eqn tiguous characters), we have recognized the produc- tion: text : TEXT text : TEXT -- --

- 6 -

The translation of this is simple. We generate a local ¯owers of illuminated manuscripts on esthetic name for the string, then hand the name and the grounds, either, but they have some clear economic string to TROFF, and let TROFF perform the storage advantages. management. All we save is the name of the string, Some of the de®ciencies in the output could be its height, and its baseline. cleaned up with more work on our part. For exam- As another example, the translation associated ple, we sometimes leave too much space between a with the production roman letter and an italic one. If we were willing to keep track of the fonts involved, we could do this box : box OVER box better more of the time. is: Some other weaknesses are inherent in our out- Width of output box = put device. It is hard, for instance, to draw a line of slightly more than largest input width an arbitrary length without getting a perceptible over- Height of output box = strike at one end. slightly more than sum of input heights As to ease of use, at the time of writing, the Base of output box = system has been used by two distinct groups. One slightly more than height of bottom input box user population consists of mathematicians, chemists, String describing output box = physicists, and computer scientists. Their typical move down; reaction has been something like: move right enough to center bottom box; (1) It's easy to write, although I make the follow- draw bottom box (i.e., copy string for bottom box); ing mistakes... move up; move left enough to center top box; draw top box (i.e., copy string for top box); (2) How do I do...? move down and left; draw line full width; (3) It botches the following things.... Why don't return to proper base line. you ®x them? Most of the other productions have equally simple (4) You really need the following features... semantic actions. Picturing the output as a set of properly placed boxes makes the right sequence of The learning time is short. A few minutes positioning commands quite obvious. The main gives the general ¯avor, and typing a or two of dif®culty is in ®nding the right numbers to use for a paper generally uncovers most of the misconcep- esthetically pleasing positioning. tions about how it works. With a grammar, it is usually clear how to The second user group is much larger, the extend the language. For instance, one of our users secretaries and mathematical typists who were the ori- suggested a TENSOR operator, to make constructions ginal target of the system. They tend to be enthusias- like tic converts. They ®nd the language easy to learn k j (most are largely self-taught), and have little trouble l m T producing the output they want. They are of course n i less critical of the esthetics of their output than users Grammatically, this is easy: it is suf®cient to add a trained in mathematics. After a transition period, production like most ®nd using a computer more interesting than a regular typewriter. box : TENSOR { list } The main dif®culty that users have seems to be Semantically, we need only juggle the boxes to the remembering that a blank is a delimiter; even experi- right places. enced users use blanks where they shouldn't and omit them when they are needed. A common instance is 6. Experience typing There are really three aspects of interestÐhow f(x sub i) well EQN sets mathematics, how well it satis®es its goal of being ``easy to use,'' and how easy it was to which produces build. f (xi ) The ®rst question is easily addressed. This entire paper has been set by the program. Readers instead of

can judge for themselves whether it is good enough f (xi ) for their purposes. One of our users commented that although the output is not as good as the best hand- Since the EQN language knows no mathematics, it set material, it is still better than average, and much cannot deduce that the right parenthesis is not part of better than the worst. In any case, who cares? the subscript. Printed books cannot compete with the birds and The language is somewhat prolix, but this -- --

- 7 -

doesn't seem excessive considering how much is 7. Conclusions being done, and it is certainly more compact than the We think we have shown that it is possible to corresponding TROFF commands. For example, here do acceptably good typesetting of mathematics on a is the source for the continued fraction expression in phototypesetter, with an input language that is easy to Section 1 of this paper: learn and use and that satis®es many users' demands. a sub 0 + b sub 1 over Such a package can be implemented in short order, {a sub 1 + b sub 2 over given a compiler-compiler and a decent typesetting {a sub 2 + b sub 3 over program underneath. {a sub 3 + ... }}} De®ning a language, and building a compiler This is the input for the large integral of Section 1; for it with a compiler-compiler seems like the only notice the use of de®nitions: sensible way to do business. Our experience with the use of a grammar and a compiler-compiler has been de®ne emx "{e sup mx}" uniformly favorable. If we had written everything de®ne mab "{m sqrt ab}" into code directly, we would have been locked into de®ne sa "{sqrt a}" our original design. Furthermore, we would have de®ne sb "{sqrt b}" never been sure where the exceptions and special int dx over {a emx − be sup −mx} Ä=Ä cases were. But because we have a grammar, we can left { lpile { change our minds readily and still be reasonably sure 1 over {2 mab} ÄlogÄ that if a construction works in one place it will work {sa emx − sb} over {sa emx + sb} everywhere. above 1 over mab Ä tanh sup −1 ( sa over sb emx ) Acknowledgements above We are deeply indebted to J. F. Ossanna, the −1 over mab Ä coth sup −1 ( sa over sb emx ) } author of TROFF, for his willingness to modify TROFF to make our task easier and for his continuous As to ease of construction, we have already assistance during the development of our program. mentioned that there are really only a few person- We are also grateful to A. V. Aho for help with months invested. Much of this time has gone into language theory, to S. C. Johnson for aid with the two thingsЮne-tuning (what is the most esthetically compiler-compiler, and to our early users A. V. Aho, pleasing space to use between the numerator and S. I. Feldman, S. C. Johnson, R. W. Hamming, and denominator of a fraction?), and changing things M. D. McIlroy for their constructive criticisms. found de®cient by our users (shouldn't a tilde be a delimiter?). References The program consists of a number of small, [1] A Manual of Style, 12th . University of essentially unconnected modules for code generation, Chicago Press, 1969. p 295. a simple lexical analyzer, a canned parser which we [2] Model C/A/T Phototypesetter. Graphic Sys- did not have to write, and some miscellany associated tems, Inc., Hudson, N. H. with input ®les and the macro facility. The program [3] Ritchie, D. M., and Thompson, K. L., ``The is now about 1600 lines of C [6], a high-level UNIX time-sharing system.'' Comm. ACM 17, language reminiscent of BCPL. About 20 percent of 7 (July 1974), 365-375. these lines are ``print'' statements, generating the out- put code. [4] Ossanna, J. F., TROFF User's Manual. Bell Laboratories Computing Science Technical The semantic routines that generate the actual Report 54, 1977. TROFF commands can be changed to accommodate other formatting languages and devices. For example, [5] Aho, A. V., and Johnson, S. C., ``LR Pars- in less than 24 hours, one of us changed the entire ing.'' Comp. Surv. 6, 2 (June 1974), 99-124. semantic package to drive , a variant of [6] B. W. Kernighan and D. M. Ritchie, The C TROFF, for typesetting mathematics on teletypewriter Programming Language. Prentice-Hall, Inc., devices capable of reverse line motions. Since many 1978. potential users do not have access to a typesetter, but still have to type mathematics, this provides a way to get a typed version of the ®nal output which is close enough for debugging purposes, and sometimes even for ultimate use.

-- --