Novellas with Groff
Looks like the baseline of the last line of output is just outside the
以下正文同步自 Hacker News,版权归原站所有,已转换为易读排版。
Looks like the baseline of the last line of output is just outside the page. Not sure what's going on here -- it's related to setting the page size to A5. In other words, I probably missed another spot where this has to be set. It'll be fixed implicitly by the following.
The whole page number thing consists of three parts: First, how large is the footer (the area where the page numbers live)? I defined this in a register (which is not really necessary but I find the "code" easier to read this way) -- the name is arbitary:
.nr _FooterSize 5v
We than plant a trap at the bottom of the page:
.wh -\n[_FooterSize]u _FooterTrap
\n[_FooterSize] "gets" the value of the register called _FooterSize. There's a minus sign in front of it, it's negative and relative to the bottom of the page. The documentation says:
A general-purpose register (one created or updated with the nr request) is implicitly dimensionless, or reckoned in basic units if interpreted in a measurement context.
I'm not 100% sure why I need to add the u. I haven't read every page of the manual, yet. Needless to say, it doesn't work without it.
Okay, so the trap gets installed at 5v before the end of the page and it'll call the _FooterTrap macro. It looks like this:
.de _FooterTrap
." Push current environment on stack and switch to _FooterEnv.
." Parent env will keep partially collected lines.
. ev _FooterEnv
." Copy settings from default environment.
. evc 0
." Don't do this for pages 1 and 2, they're title pages in my actual
." "book".
. if \n[%]>2 {
." Set vertical position, explained in the text below.
. sp |\n[.p]u-2v
." Print % register centered.
. ce
\fR\n[%]\fP
}
." Go back to previous environment.
. ev
." Request page break.
' bp
..
It's much easier to read without the inline comments:
.de _FooterTrap
. ev _FooterEnv
. evc 0
. if \n[%]>2 {
. sp |\n[.p]u-2v
. ce
\fR\n[%]\fP
}
. ev
' bp
..
Why is it 'bp instead of .bp? We'll get to that.
What I'm unsure about: I set the vertical position to |\n[.p]u-2v. That's relative to the top of the page, it's the page length minus two vee. I would have expected |-2v-1v (including the "minus one vee" mentioned in the docs) to also work, but that leads to an infinite loop (probably triggers the same trap again and again). Why?
For symmetry, I also want some empty space at the top of the page:
.nr _HeaderSize 5v .de _HeaderTrap ' sp |\n[_HeaderSize]u .. .wh 0v _HeaderTrap
And another TODO for me: This isn't 100% symmetrical. The length of the header is a bit larger than the footer. An issue with the page size again?
Let's have a closer look at traps. I'm not 100% sure on this -- this is to the best of my understanding.
Actually, let's take a step back from the example above. Let's start with this:
This is a little excerpt from Mary Shelly's Frankenstein.
These visions faded when I perused, for the first time, those poets whose effusions entranced my soul and lifted it to heaven. I also became a poet and for one year lived in a paradise of my own creation; I imagined that I also might obtain a niche in the temple where the names of Homer and Shakespeare are consecrated. You are well acquainted with my failure and how heavily I bore the disappointment. But just at that time I inherited the fortune of my cousin, and my thoughts were turned into the channel of their earlier bent.
Six years have passed since I resolved on my present undertaking. I can, even now, remember the hour from which I dedicated myself to this great enterprise. I commenced by inuring my body to hardship. I accompanied the whale-fishers on several expeditions to the North Sea; I voluntarily endured cold, famine, thirst, and want of sleep; I often worked harder than the common sailors during the day and devoted my nights to the study of mathematics, the theory of medicine, and those branches of physical science from which a naval adventurer might derive the greatest practical advantage. Twice I actually hired myself as an under-mate in a Greenland whaler, and acquitted myself to admiration. I must own I felt a little proud when my captain offered me the second dignity in the vessel and entreated me to remain with the greatest earnestness, so valuable did he consider my services.
Save it as test.groff and make a PDF:
groff -Tpdf test.groff >book.pdf
Looks like this:
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。