关于 Unix,我最喜欢的故事之一是它如何演变成一个文本处理系统。Brian Kernighan 在他的书 Unix: A History and a Memoir (第 3 章) 中讲述了这个故事,但概括来说:贝尔实验室的 Unix 团队在 PDP-7 计算机上运行原始的 Unix,但这是一个小型系统,没有足够的资源来支持新的工作。因此,Ken Thompson 和其他人游说购买一台新的 PDP-11 计算机。管理层拒绝了这一请求。大约在同一时间,专利部门计划购买一个新的计算机平台,使用专有的文档格式化软件来生成专利申请。Unix 团队提议,专利部门可以购买一台新的 PDP-11 给 Unix 团队,而 Unix 团队将为他们创建格式化软件。
这个新的格式化系统被称为 nroff
,是 "New Roff" 的缩写,它是 20 世纪 60 年代计算机系统中的一个名为 Roff 的文本格式化程序的更新版本。Roff 这个名字来源于旧的表达方式 "I'll run off a document."(我会打印一份文档)。
使用 nroff 进行基本格式化
默认情况下,nroff
收集单词并填充段落。当 nroff
遇到空行时,它会开始一个新的段落。例如,从本文的引言开始,它只有几个段落长
$ cat intro
Old-school technical writing with groff
Jim Hall
One of my favorite stories about Unix is how it turned
into a text processing system. Brian Kernighan tells the
story in his book Unix: A History and a Memoir (chapter 3)
but to summarize:
The Unix team at Bell Labs ran the original Unix on
a PDP-7 computer, but it was a tiny system and didn't
have sufficient resources to support new work. So Ken
Thompson and others lobbied to purchase a new PDP-11
computer. Management denied the request. Around the same
time, the Patents department planned to buy a new computer
platform to produce patent applications using proprietary
document formatting software. The Unix group proposed
that the Patents department instead buy a new PDP-11 for
the Unix team, and the Unix team would create formatting
software for them.
That new formatting system was called nroff, short for
"New Roff," an updated version of a text formatting program
called Roff from a 1960s computer system. The name Roff
came from the old expression, "I'll run off a document."
如果您使用 nroff
处理此文件,则行会被 "粘合" 在一起,因此输出是完全对齐的段落。如果这有助于平衡文本中的行,使用 nroff
也会对单词进行断字
$ nroff intro | head
Old‐school technical writing with groff Jim Hall
One of my favorite stories about Unix is how it turned into a
text processing system. Brian Kernighan tells the story in his
book Unix: A History and a Memoir (chapter 3) but to summarize:
The Unix team at Bell Labs ran the original Unix on a PDP‐7 com‐
puter, but it was a tiny system and didn’t have sufficient re‐
sources to support new work. So Ken Thompson and others lobbied
to purchase a new PDP‐11 computer. Management denied the request.
Around the same time, the Patents department planned to buy a new
原始的 Unix 系统使用打字机式打印机,在美国 Letter 纸张上使用 66 行 80 列,而 nroff
也做了相同的假设。它还添加了空行,因此每页输出为每页 66 行,但我使用了 head
命令来仅显示输出的前几行,因为我的示例文本不是很长。
换行和居中文本
前两行旨在成为单独的文本行。您可以插入格式化指令来告诉 nroff
添加换行符。所有 nroff
指令都以点开头,后跟简短的命令。要添加换行符,请在第一行和第二行之间使用 .br
指令
Old-school technical writing with groff
.br
Jim Hall
当您处理这个新文件时,nroff
会将标题和作者打印在单独的行上
$ nroff intro | head
Old‐school technical writing with groff
Jim Hall
One of my favorite stories about Unix is how it turned into a
text processing system. Brian Kernighan tells the story in his
book Unix: A History and a Memoir (chapter 3) but to summarize:
The Unix team at Bell Labs ran the original Unix on a PDP‐7 com‐
puter, but it was a tiny system and didn’t have sufficient re‐
sources to support new work. So Ken Thompson and others lobbied
to purchase a new PDP‐11 computer. Management denied the request.
您可以添加其他格式,使此文档看起来更好。要使顶部两行居中,请使用 .ce
格式化请求。这需要一个数字参数,以指示 nroff
应居中多少行。在这里,您可以使用 .ce 2
请求使顶部两行输出居中
.ce 2
Old-school technical writing with groff
.br
Jim Hall
通过添加此指令,nroff
正确地使前两行居中
$ nroff intro | head
Old‐school technical writing with groff
Jim Hall
One of my favorite stories about Unix is how it turned into a
text processing system. Brian Kernighan tells the story in his
book Unix: A History and a Memoir (chapter 3) but to summarize:
The Unix team at Bell Labs ran the original Unix on a PDP‐7 com‐
puter, but it was a tiny system and didn’t have sufficient re‐
sources to support new work. So Ken Thompson and others lobbied
to purchase a new PDP‐11 computer. Management denied the request.
添加页面边距
将其打印到打印机上会导致文本从页面的第一行开始,并靠在左边缘。要从页面顶部添加几行额外的空间,请使用 .sp
请求,并指定要添加的空行数
.sp 5
.ce 2
Old-school technical writing with groff
.br
Jim Hall
默认情况下,nroff
格式化输出,使每行 65 列宽。打印到 80 列的美国 Letter 纸张会留下 15 列空白。在左侧添加 7 个空格可以整齐地平衡输出,使左右页面边距相等。您可以使用 .po 7
请求创建此页面偏移
.po 7
.sp 5
.ce 2
Old-school technical writing with groff
.br
Jim Hall
使用 nroff
处理新文件会生成一个纯文本页面,该页面已准备好打印
$ nroff intro | head
Old‐school technical writing with groff
Jim Hall
One of my favorite stories about Unix is how it turned into a
text processing system. Brian Kernighan tells the story in his
打印到激光打印机
后来,贝尔实验室的 Unix 团队获得了一台照相排版机,能够生成类似于激光打印机的印刷文本。为了支持排版机的新功能,Unix 团队更新了 nroff
,使其成为特定于排版机的 troff
程序,并在几年后再次更新,使其成为 ditroff
,即 troff
的设备无关版本。
Linux 系统使用 GNU groff
程序提供现代版本的 nroff
和 troff
。您仍然可以使用旧的 nroff
程序名称来生成纯文本输出,或使用 troff
来生成 ditroff
兼容的输出。使用 groff
程序,您还可以为其他类型的输出文件(例如 Postscript)准备文档。
您可以使用 groff
处理相同的输入文件,通过使用 -T
选项选择合适的输出类型(例如 -Tps
生成 Postscript 文件)来在 Postscript 兼容的激光打印机上打印。例如,我可以使用 lpr
命令 和 HP_LaserJet_CP1525nw
设备打印到打印机,因为这是我的 Linux 系统 识别我的激光打印机 的方式
$ groff -Tps intro | lpr -P "HP_LaserJet_CP1525nw"
生成其他类型的输出
如果您改为希望将输出保存为 PDF 文件,则可以使用 ps2pdf
工具转换 Postscript
$ groff -Tps intro | ps2pdf - > intro.pdf
要从同一文件生成网页,请使用 -Thtml
将输出类型设置为 HTML
$ groff -Thtml intro > index.html
groff
命令支持许多其他内置格式化请求,以提供其他类型的文档格式化。如果您想了解 GNU groff
实现的 nroff
和 troff
中可用的其他默认格式化请求,请参阅 《GNU Troff 手册》中的第 5 章。
使用这些内置命令格式化文档需要花费大量精力来保持所有内容看起来相同。使用 groff
的技术作家改为使用一组称为宏的格式化请求,这些宏提供自己的命令来生成节标题、段落、块引用、脚注、列表和其他有用的文档格式。要了解有关一种流行的宏包的更多信息,请阅读 Opensource.com 上的 如何使用 groff -me 在 Linux 上格式化学术论文。
2 条评论