troff (执行排版功能并格式化文档)

rose1 发表于 2020-07-31 14:31
浏览次数:
在手机上阅读

在类似Unix的操作系统上,troff命令执行排版功能并格式化文档。 它是AT&T为Unix开发的文档处理系统的主要组件。 本文档介绍了troff的GNU / Linux版本。

查看英文版

目录

1 troff 运行系统环境

2 troff 语法

3 troff 说明

4 troff 例子

troff 运行系统环境

Linux

troff 语法

troff [-abcivzCERU] [-d cs] [-f fam] [-F dir] [-I dir] [-m name] [-M dir] 
      [-n num] [-o list] [-r cn] [-T name] [-w name] [-W name] [file ...]
troff [-abcivzCERU] [-d cs] [-f fam] [-F dir] [-I dir] [-m name] [-M dir] 
      [-n num] [-o list] [-r cn] [-T name] [-w name] [-W name] [file ...]

查看英文版

查看中文版

troff 说明

troff格式化打印机和字符终端的文本,并且是Unix历史上最重要的软件之一。

roff,nroff和groff一样,troff源自Jerome Saltzer为1960年代中期针对MIT的CTSS操作系统编写的名为RUNOFF的文本格式程序。名称RUNOFF来自短语“要运行文档”,意思是定稿或准备发布或展示。

RUNOFF的后续迭代称为rf,然后称为roff,最终在Unix上的PDP-7上编写了一个版本的roff。最终,它演变为nroff(“较新的roff”),然后演变为troff(“排字机”)。传说中的Brian Kernighan最终重写了troff,使其语法更加灵活,并将其设计为与设备无关。

尽管多年来诸如LaTeX之类的其他排版软件包已经获得了广泛的使用,但是troff仍在Unix中广泛使用,并且是man的默认类型格式化程序。

本文档适用于troff命令的GNU版本,它是groff软件包的一部分,通常在Linux上使用。

选件

-a 生成排版输出的ASCII近似值。
-b 显示每个警告或错误消息的回溯。此回溯应该有助于跟踪错误原因。回溯中给出的行号可能并不总是正确的,因为troff的行号概念会被asam请求混淆。
-c 禁用颜色输出(在兼容模式下始终禁用)。
-C 启用兼容模式。
-dcs, -dname=s 将c或name定义为字符串s; c必须是一个字母的名称。
-E 禁止troff的所有错误消息。请注意,这不会影响使用tmtm1请求的宏程序包将消息输出为标准错误。
-ffam 使用fam作为默认字体系列。
-Fdir 在目录(或目录路径)的dir中搜索子目录devname(其中name是设备的名称)以及DESC文件和字体文件。在所有其他字体目录之前扫描目录。
-i 处理所有命名的输入文件后,读取标准输入。
-Idir 此选项可用于将目录添加到文件(在命令行上的文件和在.psbb请求中命名的文件)的搜索路径。搜索路径用当前目录初始化。可以多次指定此选项。然后按照指定的顺序搜索目录(但在当前目录之前)。如果要使当前目录在其他目录之前被读取,请添加-I。在适当的地方。

不对具有绝对文件名的文件执行目录搜索。
-mname 读入文件“ name.tmac”。如果找不到,则尝试使用“ tmac.name”。它将首先在以下位置搜索:
  1. -M命令行选项给定的目录,
  2. 然后在GROFF_TMAC_PATH环境变量中给定的目录中,
  3. 然后在当前目录中(仅在“不安全”模式下),
  4. 主目录,
  5. /usr/lib/groff/site-tmac,
  6. /usr/share/groff/site-tmac,
  7. 最后在/usr/share/groff/1.22.2/tmac中。
-Mdir 搜索宏文件的目录(或目录路径)dir。将在所有其他宏目录之前对其进行扫描。
-nnum 为第一页编号。
-olist 仅输出列表中的页面,列表是页面范围的逗号分隔列表; n表示打印页面n,m-n表示打印m到n之间的每页,-n表示打印每页直到n,n-表示从n开始打印每页。打印列表中的最后一页后,troff将退出。
-rcn -rname=n 将数字寄存器c或名称设置为n; c必须是一个字符名称; n可以是任何troff数值表达式。
-R 不加载troffrctroffrc-end
-Tname 为设备名称而不是缺省ps准备输出。
-U 不安全模式。这将启用以下请求:openopenapsosypi。出于安全原因,否则将禁用这些潜在危险的请求。还将当前目录添加到宏搜索路径。
-v 打印版本号。
-wname 启用警告名称。可用的警告在下面的“警告”部分中进行了描述。例如,要启用所有警告,请使用-w all。允许使用多个-w选项。
-Wname 禁止警告名称。允许使用多个-W选项。
-z 禁止格式化输出。

警告事项

troff可以给出的警告分为以下几类。与每个警告关联的名称由-w-W选项使用。该数目由警告请求和.warn寄存器使用;允许按位组合始终为2的幂(可以将任何两个代码加在一起组合以创建唯一的数字)。

警告,按位列出:

bit code warning
0 1 char
1 2 number
2 4 break
3 8 delim
4 16 el
5 32 scale
6 64 range
7 128 syntax
8 256 di
9 512 mac
10 1024 reg
11 2048 tab
12 4096 right-brace
13 8192 missing
14 16384 input
15 32768 escape
16 65536 space
17 131072 font
18 262144 ig
19 524288 color
20 1048576 file

说明:

警告 代码 说明
break 4 在填充模式下,无法折断的线的长度小于线的长度。默认情况下启用。
char 1 不存在的字符。默认情况下启用。
color 524288 与颜色相关的警告。
delim 8 缺少或不匹配的结束定界符。
di 256 在没有当前转移的情况下使用不带参数的di或da。
el 16 使用el请求,没有匹配的请求。
escape 32768 无法识别的转义序列。当遇到无法识别的转义序列时,转义字符将被忽略。
file 1048576 指示缺少mso请求的文件。默认启用。
font 131072 不存在的字体。默认情况下启用。
ig 262144 请求忽略的文本中的无效转义符。当这些条件没有出现在忽略的文本中时,这些条件就是错误。
input 16384 无效的输入字符。
mac 512 使用未定义的字符串,宏和转换。当使用未定义的字符串,宏或转换时,该字符串将自动定义为空。因此,在大多数情况下,每个名称最多给出一个警告。
missing 8192 缺少非可选参数的请求。
number 2 无效的数字表达式。默认情况下启用。
range 64 超出范围的参数。
reg 1024 使用未定义的数字寄存器。当使用未定义的数字寄存器时,该寄存器将被自动定义为值为0。因此,在大多数情况下,对于使用特定名称,最多将给出一个警告。
right-brace 4096 使用\}(应为数字)。
scale 32 无意义的缩放指示器。
space 65536 请求或宏与其参数之间缺少空格。当遇到长度超过两个字符的未定义名称,并且该名称的前两个字符构成已定义名称时,将发出此警告。该请求或宏将不会被调用。发出此警告时,不会自动定义宏。默认情况下启用。在兼容模式下将永远不会出现此警告。
syntax 128 数字表达式中的可疑语法。
tab 2048 不当使用制表符。在期望数字的位置使用制表符,或在未加引号的宏参数中使用制表符。

还有一些名称可用于引用警告组:

all dimacreg外的所有警告。旨在涵盖适用于传统宏程序包的所有警告。
w 所有警告。

环境

troff使用以下环境变量:

GROFF_TMAC_PATH 用冒号分隔的目录列表,可在其中搜索宏文件。 troff将先扫描-M选项中指定的目录以及标准目录(如果处于不安全模式,则为当前目录,主目录,/ usr / lib / groff / site-tmac,/ usr / share / groff / site-tmac, /usr/share/groff/1.22.2/tmac)之后。
GROFF_TYPESETTER 默认设备。
GROFF_FONT_PATH 用冒号分隔的目录列表,可在其中搜索devname目录。 troff将先扫描-F选项中给出的目录,然后再扫描标准目录(/usr/share/groff/site-font、/usr/share/groff/1.22.2/font、/usr/lib/font)中的目录这些。

档案

/usr/share/groff/1.22.2/tmac/troffrc 初始化文件(在任何其他宏程序包之前调用)。
/usr/share/groff/1.22.2/tmac/troffrc-end 初始化文件(在任何其他宏程序包之后调用)。
/usr/share/groff/1.22.2/tmac/name.tmac, /usr/share/groff/1.22.2/tmac/tmac.name 宏文件
/usr/share/groff/1.22.2/font/devname/DESC 设备名称的设备描述文件。
/usr/share/groff/1.22.2/font/devname/F 设备名称的字体F的字体文件。

请注意,出于安全考虑,默认情况下,既不在当前目录中也不在主目录中搜索troffrctroffrc-end(即使已指定-U选项)。如有必要,请使用-M命令行选项或GROFF_TMAC_PATH环境变量将这些目录添加到搜索路径。

troff formats text for printers and character terminals, and is one of the most important pieces of software in Unix history.

Like roff, nroff and grofftroff is descended from a text formatting program called RUNOFF written by Jerome Saltzer for MIT's CTSS operating system in the mid-1960s. The name RUNOFF comes from the phrase "to run off a document," meaning to finalize a document, or prepare it for publication or presentation.

Later iterations of RUNOFF were called rf and then roff, and a version of roff was eventually written for Unix on a PDP-7. Eventually, it evolved in to nroff ("newer roff") and then troff ("typesetter roff"). The legendary Brian Kernighan eventually rewrote troff, giving it a more flexible syntaxand designing it to be device-independent.

Although other typesetting software packages such as LaTeX have gained prominent usage throughout the years, troff is still used extensively in Unix, and is the default type formatter for man.

This documentation is for GNU version of the troff command, which is part of the groff package and commonly used on Linux.

Options

-a Generate an ASCII approximation of the typeset output.
-b Print a backtrace with each warning or error message. This backtrace should help track down the cause of the error. The line numbers given in the backtrace may not always be correct, for troff's idea of line numbers gets confused by as or am requests.
-c Disable color output (always disabled in compatibility mode).
-C Enable compatibility mode.
-dcs, -dname=s Define c or name to be a string s; c must be a one-letter name.
-E Inhibit all error messages of troff. Note that this doesn't affect messages output to standard error by macro packages using the tm or tm1 requests.
-ffam Use fam as the default font family.
-Fdir Search in directory (or directory path) dir for subdirectories devname (where name is the name of the device), and for the DESC file and font files. The dir is scanned before all other font directories.
-i Read the standard input after all the named input files have been processed.
-Idir This option may be used to add a directory to the search path for files (both those on the command line and those named in .psbb requests). The search path is initialized with the current directory. This option may be specified more than once; the directories are then searched in the order specified (but before the current directory). If you want to make the current directory be read before other directories, add -I. at the appropriate place.

No directory search is performed for files with an absolute file name.
-mname Read in the file "name.tmac". If it isn't found, "tmac.name" is tried instead. It will be first searched for in:
  1. directories given with the -M command line option,
  2. then in directories given in the GROFF_TMAC_PATH environment variable,
  3. then in the current directory (only if in "unsafe" mode),
  4. the home directory,
  5. /usr/lib/groff/site-tmac,
  6. /usr/share/groff/site-tmac,
  7. and finally in /usr/share/groff/1.22.2/tmac.
-Mdir Search directory (or directory path) dir for macro files. This is scanned before all other macro directories.
-nnum Number the first page num.
-olist Output only pages in list, which is a comma-separated list of page ranges; n means print page n, m-n means print every page between m and n, -n means print every page up to n, n- means print every page from n. troff will exit after printing the last page in the list.
-rcn -rname=n Set number register c or name to n; c must be a one character name; n can be any troff numeric expression.
-R Don't load troffrc and troffrc-end.
-Tname Prepare output for device name, rather than the default ps.
-U Unsafe mode. This will enable the following requests: openopenapsosy, and pi. For security reasons, these potentially dangerous requests are disabled otherwise. It will also add the current directory to the macro search path.
-v Print the version number.
-wname Enable warning name. Available warnings are described in the section WARNINGS below. For example, to enable all warnings, use -w all. Multiple -w options are allowed.
-Wname Inhibit warning name. Multiple -W options are allowed.
-z Suppress formatted output.

Warnings

The warnings that can be given by troff are divided into the following categories. The name associated with each warning is used by the -w and -W options; the number is used by the warn request, and by the .warn register; it is always a power of 2 to allow bitwise composition(any two codes can be combined additively to create a unique number).

Warnings, listed by bit:

bit code warning
0 1 char
1 2 number
2 4 break
3 8 delim
4 16 el
5 32 scale
6 64 range
7 128 syntax
8 256 di
9 512 mac
10 1024 reg
11 2048 tab
12 4096 right-brace
13 8192 missing
14 16384 input
15 32768 escape
16 65536 space
17 131072 font
18 262144 ig
19 524288 color
20 1048576 file

Descriptions:

warning code description
break 4 In fill mode, lines which could not be broken so that their length was less than the line length. This is enabled by default.
char 1 Non-existent characters. This is enabled by default.
color 524288 Color related warnings.
delim 8 Missing or mismatched closing delimiters.
di 256 Use of di or da without an argument when there is no current diversion.
el 16 Use of the el request with no matching ie request.
escape 32768 Unrecognized escape sequences. When an unrecognized escape sequence is encountered, the escape character is ignored.
file 1048576 Indicates a missing file for the mso request. Enabled by default.
font 131072 Non-existent fonts. This is enabled by default.
ig 262144 Invalid escapes in text ignored with the ig request. These are conditions that are errors when they do not occur in ignored text.
input 16384 Invalid input characters.
mac 512 Use of undefined strings, macros and diversions. When an undefined string, macro or diversion is used, that string is automatically defined as empty. So, in most cases, at most one warning will be given for each name.
missing 8192 Requests that are missing non-optional arguments.
number 2 Invalid numeric expressions. This is enabled by default.
range 64 Out of range arguments.
reg 1024 Use of undefined number registers. When an undefined number register is used, that register is automatically defined to have a value of 0. So, in most cases, at most one warning will be given for use of a particular name.
right-brace 4096 Use of \} where a number was expected.
scale 32 Meaningless scaling indicators.
space 65536 Missing space between a request or macro and its argument. This warning will be given when an undefined name longer than two characters is encountered, and the first two characters of the name make a defined name. The request or macro will not be invoked. When this warning is given, no macro is automatically defined. This is enabled by default. This warning will never occur in compatibility mode.
syntax 128 Dubious syntax in numeric expressions.
tab 2048 Inappropriate use of a tab character. Either use of a tab character where a number was expected, or use of tab character in an unquoted macro argument.

There are also names that can be used to refer to groups of warnings:

all All warnings except dimac, and reg. It is intended that this covers all warnings that are useful with traditional macro packages.
w All warnings.

Environment

troff makes use of the following environment variables:

GROFF_TMAC_PATH A colon-separated list of directories in which to search for macro files. troff will scan directories given in the -M option before these, and in standard directories (current directory if in unsafe mode, home directory, /usr/lib/groff/site-tmac/usr/share/groff/site-tmac/usr/share/groff/1.22.2/tmac) after these.
GROFF_TYPESETTER Default device.
GROFF_FONT_PATH A colon separated list of directories in which to search for the devname directory. troff will scan directories given in the -F option before these, and in standard directories (/usr/share/groff/site-font/usr/share/groff/1.22.2/font/usr/lib/font) after these.

Files

/usr/share/groff/1.22.2/tmac/troffrc Initialization file (called before any other macro package).
/usr/share/groff/1.22.2/tmac/troffrc-end Initialization file (called after any other macro package).
/usr/share/groff/1.22.2/tmac/name.tmac/usr/share/groff/1.22.2/tmac/tmac.name Macro files
/usr/share/groff/1.22.2/font/devname/DESC Device description file for device name.
/usr/share/groff/1.22.2/font/devname/F Font file for font F of device name.

Note that troffrc and troffrc-end are neither searched in the current nor in the home directory by default for security reasons (even if the -U option is given). Use the -M command line option or the GROFF_TMAC_PATH environment variable to add these directories to the search path if necessary.

查看英文版

查看中文版

troff 例子

troff myfile

处理文件myfile。

troff myfile

Process file myfile.

查看英文版

查看中文版

其他命令行

tabs | tac | talk | tail | tcopy | tty | tar | tbl | tcpdump | time | tee | timex | telinit | telnet | test | top | touch | tput | tr | traceroute |

如此好文,分享给朋友