Hacker News new | ask | show | jobs
by neutrono 1405 days ago
NB: I'm new to HN, so please excuse any formatting errors in this comment.

Well, I've been using groff on and off for a few years, I can give it a shot. Most "macros" as in TeX, are invoked by having the first character of a line be a `.` and then as in TeX, refer to a name. Now, macros in groff are weird. Macros are not delimited by `{}` delimiters as in TeX, but either by having an environment block like so:

    .TS
    .TE
Incidentally, `.TS` and `.TE` is the environment for tbl(1), the pre-processor for tables. Or by invoking another macro, as in this example for headings and paragraphs in the -ms macro set. (The -ms macro set is called as such because that's how you get it into your document, you supply the -ms flag at build time to groff to get the -ms macros). See groff_ms(7) for more information than I will supply here.

    .NH 1
    Some heading here
    .PP
    Start of a paragraph.
Sometimes you want to start a line of input with a period. To do that you just use the character '\&' before the period. The `\&` character is a non printing character, so use it anywhere you want, really. I imagine you want to play with math typography, in that case there is EQN. EQN works by having an environment block, `.EQ` and `.EN` and in that block you type in the EQN mini-language.

    .EQ
    f ( x ) ~=~ x sup {2} - 1
    .EN
EQN does not care about precedence or anything, so use `{}` to delimit expressions. I think there is some kind of EQN document rescued from Bell Labs that tells you about how the language works. Check troff.org for it, or email the mailing list for groff, which is also pretty good for newbie questions, it is not really all that high traffic.

Groff also does pictures, so you can for example create a box with some text in it (it is capable of more, like strokes, macros, color, and I believe rotation[not sure]). To start a picture, `.PS` and `.PE` will do so, and again, a small language as input in that block.

Now, to print anything you should know the design of the original troff system was heavily based on the UNIX philosophy, which means the output you get from running the groff program prints the output to stdout. Just redirect the output to a file to get a PDF or a PS document.

Obviously, there is much I have not covered in this comment, such as macros, registers, and other exciting topics such as refer(1), but this should give you a quick start.

1 comments

This is helpful! Thank you for taking the time to write it up.

I didn't know about '\&' as a way to "short circuit" groff from interpreting something as a macro that should be literal (such as a line-initial period). That's great to know.

I've found a couple of resources that have been helpful. Mostly, I've been referring to this[0] page on gnu.org. I've also learned from this[1] article on Linux journal and Luke Smith's YouTube playlist[2] on groff.

To practice, I'm trying to re-typeset a paper I wrote in school that follows the Chicago Manual of Style[3]. I formatted the original in MS Word using a template my school provided, so I have something to visually compare against. I'm using the -ms macro set, mostly because that's what Luke Smith demos in his videos so that's what I got started with.

I've already got a reasonable imitation of the original document, which is fun and a bit rewarding. But there's a couple of very small details that I'm finding difficult to crack. Are you very well-versed with -ms? In particular, right now I'm trying to figure out how to add one blank line of space in footnotes AFTER the horizontal line, but BEFORE the first footnote begins. I suspect I'll need to redefine the -ms macro that controls the trap for footnotes...? But I haven't figured out which macro that is just yet.

[0] "The GNU Troff Manual", https://www.gnu.org/software/groff/manual/groff.html

[1] "Typesetting with groff Macros", https://www.linuxjournal.com/article/4375

[2] "groff/troff for Minimalist Document Complication", https://www.youtube.com/playlist?list=PL-p5XmQHB_JRe2YeaMjPT...

[3] "The Chicago Manual of Style Online", https://www.chicagomanualofstyle.org/home.html

Sadly I don’t know how to futz about with traps, I just haven’t gotten to that point yet in my use of groff.

First I would point you to the manpage, but, we all know that manpages are a mess, so it might not be there. I will however point you, perhaps not /super/ helpful, but alas, I know not of any better solution. If you go to /usr/share/groff/current/tmac/s.tmac and search for «module fn», that should be the code for the footnotes. Aside, the -ms name comes from this, -m means macro, and whatever letter comes after it is the macro set, hence why -man will be called an.tmac in groff source code, or in this case, s.tmac.

I am not sure where documentation is on traps in groff, but I assume it’s buried withing some manpage or info document somewhere. Software is crap, eh? :D

Of course, you could ask in the groff mailing list[1], they are usually pretty helpful in these matters. Which course of action you chose is of course up to you, I will investigate for myself as well to educate myself more on traps.

[1]: mailto:groff@gnu.org