Typesetting with groff Macros
We have shown above that the groff syntax for evaluating a number register with names of arbitrary length is
\n[anyname]
Similarly, the syntax for evaluating other registers is
\*[anyname] string\f[anyname] font \[anyname] special charactergroff only has scalar variables, lacking compound structures or subscripted arrays. But it is possible to combine definitions and numeric variables in such a way as to fake the effects of compound data types. Here, we will demonstrate a “pseudo-array” that may come in handy for your bag of macro tricks.
Consider the following string definitions for days of the week:
.STRING $DAYNAME1 Sunday.STRING $DAYNAME2 Monday \# etcetera .STRING $DAYNAME7 Saturday
groff provides a number register representing the current day of the week as a numeric value 1 through 7, and, of course, we alias it again to fit in with our scheme:
.ALIASNR _DOW dwNow, we can initialize a variable with the current dayname using the pseudo-array of strings we defined above as follows:
.STRING $TODAY \*[$DAYNAME\n[_DOW]]Anytime we need the name of the current day in a macro or document, we need only use the string variable $TODAY:
.<p>Thank goodness it's \*[$TODAY].Human reaction to this message will likely be most favorable when the _DOW variable evaluates to 6.
groff also has an extension that enables the use of looping constructs within macros. Together with pseudo-arrays, this feature gives you considerably more power and flexibility over traditional troff, which only has if/else branching logic. Following the example above, if you needed a macro to create a header for a list of tab-separated columns for each weekday, (Monday through Friday) you might cobble up something like the following:
.ALIAS TABSET ta.ALIAS WHILE while
.MACRO <weekdays> __END__
. NUMBER IX 1 1
. NOFILL
. TABSET T .75iC
. WHILE \\n[IX]<6 \{\
\\*[$DAYNAME\\n+[IX]]\c
. \}
. FILL
.__END__
In the example above, the TABSET command makes use of groff's “T” extension for repeating tabs, set here every 3/4 of an inch. The loop test variable, “IX”, demonstrates the auto-increment syntax with a number register (the “+” sign in the \\n+[IX] expression). This has the effect of preincrementing the variable, so the first time through the loop IX will evaluate as 2, printing “Monday” from the pseudo-array $DAYNAME. Finally, the printed line is terminated with the \c escape sequence, to continue output on the current line without inserting the new line that would otherwise be inserted in nofill mode.
groff's implementation of “while loops” includes .break and .continue statements. These give groff more of the flow-of-control facilities of a complete programming language. Although you probably won't be using groff for solving multiple regression problems, groff's while loops do make it easier to write macros for, say, printing columns of address labels on precut forms, without using an external processor.
The basic page layout model for groff is in keeping with groff's minimalism. The only dimensions groff requires are the vertical length of the page, the hard left margin and the horizontal length available for printable lines. Each of these dimensions is set with its own request that we alias below:
.ALIAS PGLENGTH pl.ALIAS PGOFFSET po .ALIAS LNLENGTH ll
Usually, though, we need a document to have other layout parameters, such as a top and bottom margin, possibly with running headers and/or footers. All these may be configured using groff's trap mechanism in combination with additional parameters and macros for the page transition that we devise.
Let's imagine that we would like a top and bottom margin of one inch for our main body text and a centered page number one-half inch from the bottom of every page. In addition, we want to define these parameters so they will work whether we are using letter, legal or A4 sized paper. The first step is to define our own set of number registers to hold all of our layout parameters:
\# parameters with default settings:.NUMBER #PAGELENGTH 11.0i .NUMBER #PAGEWIDTH 8.5i .NUMBER #LEFTMARGIN 1.0i .NUMBER #RIGHTMARGIN 1.0i .NUMBER #TOPMARGIN 1.0i .NUMBER #BOTMARGIN 1.0i .NUMBER #FOOTMARGIN 0.5i \# \# layout initialization macro: .MACRO SET_LAYOUT __END__ . PGLENGTH \\n[#PAGELENGTH]u . PGOFFSET \\n[#LEFTMARGIN]u . LNLENGTH \ \\n[#PAGEWIDTH]u-\\n[#LEFTMARGIN]u-\\n[#RIGHTMARGIN]u .__END__ \# \# initialize layout with defaults: .SET_LAYOUT
The next step is to write our page transition macros and put them into position with the trap mechanism. The following snippet demonstrates:
\# some more aliases:.ALIAS CENTER ce .ALIAS RIGHT rj .ALIAS NEWPAGE bp .ALIAS SETTRAP wh \# \# macro for header: .MACRO MYHEADER __END__ ' SKIP |\\n[#TOPMARGIN]u .__END__ \# \# macro for footer: .MACRO MYFOOTER __END__ ' SKIP |(\\n[#PAGELENGTH]u - \\n[#FOOTMARGIN]u) . CENTER \\n[_PAGE] ' NEWPAGE .__END__ \# \# position to invoke header/footer: .SETTRAP 0 MYHEADER .SETTRAP 0-\n[#BOTMARGIN]u MYFOOTERThe example shows two macros, MYHEADER and MYFOOTER, which define the actions taken at the top of the page (position 0) and at the bottom margin (-1.0i). The syntax in these macros shows the deferred break control character, the apostrophe, ', used with groff commands that would otherwise cause the output to be immediately forced out.
The page layout parameters are defined with default values, but here we will create a user-interface for changing the papersize:
.MACRO <papersize:> __END__. IFELSE "\\$1"letter" \{\
. NUMBER #PAGELENGTH 792p
. NUMBER #PAGEWIDTH 612p
. \}
. ELSE .IFELSE "\\$1"a4" \{\
. NUMBER #PAGELENGTH 842p
. NUMBER #PAGEWIDTH 595p
. \}
. ELSE .IFELSE "\\$1"legal" \{\
. NUMBER #PAGELENGTH 1008p
. NUMBER #PAGEWIDTH 612p
. \}
. ELSE \{\
. MESSAGE \
Missing or unrecognized papersize,\
file \\n[_FILE], line \\n[_LINE]
. \}
. \" re-initialize layout:
. SET_LAYOUT
.__END__
The enduser can now set the paper size of the document, which initializes the margins accordingly. Putting together the elements we have looked at so far, we have defined an interface to groff that allows the enduser to create a document that looks like this:
.<papersize:> a4.<fontsize:> 11.5p .<parskip:> 0.8v .<p> This document is typeset by groff!
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




9 hours 13 min ago
19 hours 54 min ago
1 day 1 hour ago
1 day 1 hour ago
1 day 3 hours ago
1 day 5 hours ago
1 day 12 hours ago
1 day 12 hours ago
1 day 14 hours ago
1 day 20 hours ago