Producing PDFswith groff and momDeri JamesandPeter SchaffterThis file is part of groff.Groff is free software. You can redistribute it and/ormodify it under the terms of the GNU General PublicLicense as published by the Free Software Foundation,either version 3 of the License, or (at your option) anylater version.You should have received a copy of the GNU GeneralPublic License along with this program. If not, see:http://www.gnu.org/licenses/©2015, 2017 Free Software Foundation
Contents1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12. Using groff to generate PDF files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13. Creating PDF links with mom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13.1. Creating destination points at headings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23.2. Creating destination points at arbitrary locations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23.3. Creating internal links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23.4. Creating external links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23.5. Assigning a colour to links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34. The PDF Outline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34.1. Opening and closing levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34.2. Suspending/disabling collection of outline entries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44.3. The PDF window title . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45. Tables of Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45.1. Generating a Table of Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45.2. Positioning the Table of Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46. pdfmom: Simplifying PDF output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56.1. The problem of forward references . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56.2. pdfmom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56.3. Setting papersize within a source file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56.4. Differences between pdfmom and pdfroff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57. Comparison of -Tps/-mpdfmark with -Tpdf/-mom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6- i -
Producing PDFswith groff and momDeri JamesandPeter Schaffter1. IntroductionPDF documents are intended to be “electronic paper,and, as such, take advantage of the digitalmedium in ways that PostScript documents do not. Chief amongst these are clickable links thatpoint to named destinations, either within the documents themselves (internal links) or to remoteweb pages (external links), and the generation of a clickable document outline that appears in theContents panel of most PDF viewers.Using groff and mom to produce PDF documents results in the automatic generation of clickabledocument outlines (discussed below, “4. The PDF Outline”), and, if the TOCmacro is included inthe source file, entries in the printable table of contents can be clicked on as well when the docu-ment is viewed at the screen (see “5. Tables of Contents”). 2. Using groff to generate PDF filesGroff provides more than one way to generate PDF documents from files formatted with the mommacros. One is to call groff directly, either withgroff [-Tps] -mom -m pdfmark doc.mom | ps2pdf - doc.pdfwhich pipes output from the grops PostScript driver through ps2pdf, orgroff -Tpdf -mom doc.mom > doc.pdfwhich uses the native PDF driver, gropdf. Alternatively, one may call the wrapperpdfroff -mom -mpdfmark --no-toc doc.mom > doc.pdfA fourth, preferred method is to use pdfmom, which is strongly recommended since it imple-ments the full range of PDF features available in mom.pdfmom doc.mom > doc.pdfOne reason to prefer using the native PDF driver (via pdfmom or -Tpdf) is that papersizes setwithin mom source files (see paper and page setup macros) do not require a corresponding -P-p<papersize> flag on the command line.There are other minor differences between the methods, discussed here. 3. Creating PDF links with momOften, but not always, links in the body of a PDF document point to headings elsewhere in thesame document. Creating these links is a simple process. First, identify the places to link to (“des-tinations”), then link to them from any place in the document.-1-
James, SchaffterPRODUCING PDFS WITH GROFF AND MOM 3.1. Creating destination points at headingsThe first step in creating links to a heading is to give the heading a unique destination name. Withmom, this is done by addingNAMED <id>to the HEADING macro, where<id>is a unique identi-fier for the heading. For example,.HEADING 1 NAMED intro "Introduction"would, in addition to printing the head in the body of the document, identify the introduction by theunique id, “intro”. This id, or name, can then be used to create links to the introduction from anypart of the document.Furthermore,NAMED <id>stores the text of the heading for use later on when linking to it (see“3.3. Creating internal links”). If headings are being numbered, the heading number is prepended. 3.2. Creating destination points at arbitrary locationsAny part of a document can be a link destination, not just headings. For example, say you createa table that needs to be referred to from other parts of the document. You’d identify the location ofthe table by placing.PDF_TARGET <id> "<text>"just above the table in the source file. As withHEADING,<id>is any unique name.<text>isoptional.<id>can now be linked to from anywhere in the document.3.3. Creating internal linksInternal links are clickable text areas that allow you to jump to named destinations within a docu-ment. (See here for a description of external links.)Internal links are created with the macroPDF_LINK, which takes the form.PDF_LINK <id> [PREFIX <text>] [SUFFIX <text>] "<hotlink text>"where<id>is a named destination point elsewhere in the document (see “3.1. Creating destina-tion points at headings”and “3.2. Creating destination points at arbitrary locations”). PREFIX <text>andSUFFIX <text>, both or either of which are optional, are printed aroundthe clickable area but do not form part of the link itself.<hotlink text>is the text that should be clickable, identifiable in the PDF document by thecolour assigned to links (see “3.5. Assigning a colour to links”).If the hotlink text ends in "*", the asterisk is replaced by the text of the destination point, assum-ing it’s a heading. If the hotlink text ends in"+", the replacement text is surrounded by quotes.Using our HEADING example, above, the following invocation of PDF_LINKwould produce a click-able link to the introduction:.PDF_LINK intro PREFIX ( SUFFIX ). "see: +"In the text, the link would look like this: (see: “1. Introduction”). 3.4. Creating external linksExternal links are clickable text areas whose destination is a URL. Clicking on them causes abrowser window to pop up with the destination address.The format of the macro to create external links is similar to the one for creating internal links:.PDF_WWW_LINK <url> [PREFIX <text>] [SUFFIX <text>] ["<hotlink text>"]-2-
James, SchaffterPRODUCING PDFS WITH GROFF AND MOM <url>is any valid URL, usually a web address;PREFIX <text>andSUFFIX <text>haveexactly the same meaning, as does<hotlink text>, which furthermore accepts the sameexpandos,"+"and"*".If no hotlink text is given, then<url>is used as the text. If hotlink text is given and ends in"*", theasterisk is replaced by the URL. If it ends in"+", the URL is surrounded by quotes. As an example,.PDF_WWW_LINK https://www.schaffter.ca/mom/momdoc/toc.htmlwould open mom’s online documentation at https://www.schaffter.ca/mom/momdoc/toc.html. Thesame, with "here"supplied as hotlink text, lets you click here instead.3.5. Assigning a colour to linksThe colour of links is set with.PDF_LINK_COLOR <xcolor> | <newcolor> | <r g b> | <#rrggbb>where<xcolor>or<newcolor>are the names of colours already initialized with XCOLORorNEWCOLOR. If you prefer to define a new colour (using the RGB colour scheme), enter it eitheras 3 numbers between 0.0 1.0 or as a 6 character hex string. Thus.PDF_LINK_COLOR #ff0000and.PDF_LINK_COLOR 1.0 0 0both lead to mom using red links.The default colour can be restored by callingPDF_LINK_COLORwith no parameter.Note: The decimal scheme for creating colours must be used if a file is to be processed withgroff -Tps -mpdfmark’, ‘pdfroff’, or ‘pdfmom -Tps’.4. The PDF OutlineMost PDF viewers provide a panel that displays a document’s outline, similar to a table of con-tents. Clicking on an entry navigates directly to the appropriate place in the document.Mom generates PDF outlines the same way she populates her own table of contents: by inter-cepting calls to theHEADINGmacro, as well as to the various title and chapter macros used innamimg documents, and allocating each a hierarchic level.Covers, titles/chapters, and the table of contents are all assigned to level 1. Subsequent headingsare assigned to n+1, where n is the level given toHEADING.The PDF outline can sensibly recover from skipped or omitted heading levels; the printed table of con-tents cannot. Users are therefore advised to use headings in logical order, not for typographic effects.4.1. Opening and closing levelsA level is said to be open if one or more levels beneath it is visible in the PDF outline. Closedlevels have at least one level beneath them that is not visible unless the closed link is clicked. It iscommon for only the first two levels to be open so the outline doesn’t look cluttered.To establish which levels should be open by default when a document loads, use.PDF_BOOKMARKS_OPEN nwherenis a number specifying at which level all subsequent ones should be closed.If, at any point in the document, you specify.PDF_BOOKMARKS_OPEN NO \" or any other text argumentthen all subsequent bookmarks will be closed untilPDF_BOOKMARKS_OPENopens them again.-3-
James, SchaffterPRODUCING PDFS WITH GROFF AND MOM 4.2. Suspending/disabling collection of outline entriesSuspending the collection of entries for the PDF outline is accomplished with.PDF_BOOKMARKS OFFMom’s default is to collect entries, so if the command is placed at the start of a document, itdisables entry collection completely. Elsewhere, it suspends collection until you re-enable it with.PDF_BOOKMARKS \" i.e. with no parameter4.3. The PDF window titleWhile not strictly part of the PDF outline, the title of a document can be displayed as the docu-ment viewer’s window title. The macro to accomplish this is.PDF_TITLE "<window title>"It can take any text, so the viewer window title need not be the same as the document’s title.Note: The macro,DOC_TITLE, always invokesPDF_TITLE. If this is not what you want, you canremove the window title by issuing.PDF_TITLE "" \" ie. with a blank argument5. Tables of Contents5.1. Generating a Table of ContentsTo generate a printable Table of Contents for any document, simply insert the macro,TOC, as thelast line of the source file. (Formatting of the printable Table of Contents is discussed in detail in themom documentation). When the file is processed and loaded in a viewer, entries in the Table ofContents will be clickable links.Whichever link colour is active at the end of the document, prior to TOC, will be used for the Tableof Contents links.5.2. Positioning the Table of ContentsIf groff’s PostScript device (-Tps) is used to process a mom file, the Table of Contents is printedat the end of the document. When this is not desirable, the PostScript output from groff must beprocessed with psselect in order to place the TOC in the preferred location.When using mom and groff’s native pdf device (via pdfmom or groff -Tpdf), positioning of theTable of Contents can be done within the source file.The command to control the placement of the TOC is.AUTO_RELOCATE_TOC [<position>]where the optional<position>can be one of these keywords:TOP*(ie. at the very start of the document)BEFORE_DOCCOVERAFTER_DOCCOVERBEFORE_COVERAFTER_COVERIt is normally not necessary to supply a keyword, sinceAUTO_RELOCATE_TOCplaces the TOCafter the DOC_COVER, if there is one, or the first COVER when no DOC_COVER is present. Inrare instances where it is desirable to place the TOC somewhere else in the document, there are *Note: Documents without a COVER or DOC_COVER require theTOPargument.-4-
James, SchaffterPRODUCING PDFS WITH GROFF AND MOM two low-level commands,.TOC_BEFORE_HEREand.TOC_AFTER_HEREwhich place the TOCeither before or after the current page.These last two commands have a small catch: although the TOC will appear where specified, the“Contents” entry in the PDF outline, which observes a hierarchy of levels, will assign the TOC tolevel 1, possibly disrupting the visual ordering of levels in the outline.6. pdfmom: Simplifying PDF outputAs explained in the section 2. Using groff to generate PDF files, there are two established methodsfor creating PDF files with groff: the original method, ie. passing the -Tps and -mpdfmarkoptions to groff (or using pdfroff, which does this for you); or the newer -Tpdf, which producesPDF files natively.6.1. The problem of forward referencesBoth methods encounter difficulties when dealing with forward references; that is, when a linkearlier in a document refers to a destination later in the document and the link text terminateswith one of the expandos,"*"or"+"(explained here). Mom doesn’t know what text to put in theexpando because it has not yet been defined. This means that groff must be run multiple times tofind the unknown text.The program pdfroff exists to handle these multiple runs, but it imposes some limitations onthe PDF features available with mom.6.2. pdfmompdfmom performs the same function as pdfroff, and is the preferred, trouble-free way to gener-ate PDF documents from a mom source file. Like pdfroff, it is a frontend to groff and accepts allthe same options (see man groff).Called as-is, pdfmom accepts all the same options as groff, and requires no additional flags.PDF generation is performed by gropdf, groffs native PDF driver:pdfmom doc.mom [groff opts] > doc.pdfIf a -Tps option is supplied, pdfmom hands control over to pdfroff, and both groff and pdfroffoptions may given. The resulting PDF is produced from PostScript output fed into ghostscript.pdfmom -Tps [pdfroff opts [groff opts]] doc.mom > doc.pdfFor either invocation, it is not necessary to add -mom or -mpdfmark, as these are implied.If EncapsulatedPostScriptorplainPostScriptimageshavebeenembeddedinadocumentwithPSPIC, the -Tps option must be used. In most other cases, pdfmom with no -T flag is preferable.6.3. Setting papersize within a source fileA significant convenience afforded by using pdfmom (or groff with the -Tpdf flag) is that paper-sizes or page dimensions set within mom source files (see paper and page setup macros) do notrequire a corresponding -P-p<papersize> option on the command line. It is even possible to cre-ate documents with unequal-sized pages.6.4. Differences between pdfmom and pdfroffSeveral features described in this manual are not available when the -Tps option is given topdfmom, nor when using pdfroff or groff -Tps -mpdfmark:Relocation of the Table of Contents is not supported. The TOC appears at the end of thedocument; psselect must be used to re-order pages.If a link crosses a page boundary, it will stop being a clickable hotspot on subsequent-5-
James, SchaffterPRODUCING PDFS WITH GROFF AND MOM pages.When establishing whether PDF outline levels are open or closed, only the numericalparameter to PDF_BOOKMARKS_OPENhas any effect.PDF_LINK_COLOR only accepts colour definitions in decimal notation.7. Comparison of -Tps/-mpdfmark with -Tpdf/-mom-Tps/-mpdfmarkdoes not support all the features described hereaccepts images and graphics embedded with PSPIC-Tpdf/-momfacilitates embedding fonts directly in the PDF file (if the -P-e flag is given on thecommand line)sets papersize from within the source file, circumventing the need for the papersizeflag (-P-p<papersize>) on the command lineis not compatible with PRINTSTYLE TYPEWRITE underlining (e.g., of italics)generally produces larger files; these can be reduced by piping the output throughps2pdfNote: Owing to a known bug, PDF files piped through ps2pdf lose some of theirmetadata, notably the window title set withPDF_TITLE.-6-