我正在尝试调试我的宏,最后我将其简化为最简单的形式:它接受一个参数,用“set-bold”、“reset-bold”字体请求将其包装起来,仅此而已。但问题是我想将多行作为 1 个参数传递给它。我尝试嵌入“ " 在字符串内部,但没有成功(我仍然不知道是否可能)。所以,我改用了一个转移:
.\" Start a diversion to capture text
.di t1
aaa
bbb
.di
.\" Define a macro to print the diversion content in bold
.de MMM
.ft B
\\$1
.
.ft R
..
.\" Use the macro to process and print the diversion (I am
.\" not sure about asciify too)
.asciify t1
.MMM \*[t1]
然后打印出来
啊啊
bb
即“bbb”不是粗体!强制它打印“bbb”是一个问题,而不仅仅是“aaa”(我在行之间尝试了
.br
,但似乎只有空行可以正常工作)。在此之后,我无法将所有行加粗(在这种情况下它们只有 2 行)。
那么如何强制它使“bbb”加粗,我不知道。我什至不明白为什么会这样。通常这样的技巧有效,但不适用于多行参数。
如何修复这个简单的宏?
您不能将多行作为宏调用的参数。
当您调用
MMM
时,实际上只有第一行作为参数给出。要显示这一点:
.\" Start a diversion to capture text
.di t1
aaa
bbb
.di
.\" Define a macro to print the diversion content in bold
.de MMM
.ft B
\\$1
.
.ft R
.br
MMM end
..
.\" Use the macro to process and print the diversion (I am
.\" not sure about asciify too)
.asciify t1
.MMM \*[t1]
所以宏
bbb
永远看不到MMM
。 groff
的实际输入是
.MMM aaa
bbb
有两种方法可以解决这个问题。第一:使用已知的转移名称。如果您总是使用
t1
作为转移名称,只需这样做
.de MMM2
.ft B
\*[t1]
.ft R
..
或者使用转移名称作为参数:
.de MMM3
.ft B
\\*[\\$1]
.ft R
..
关于转移的一些解释:转移用于将格式化文本转移到指定的存储区域。您需要的是未格式化的文本。
.asciify
基本上再次取消格式化转移。
将所有内容放在一个演示中,您将得到:
.di t1
aaa
bbb
.di
.di t2
ccc
ddd
.di
.\" Define a macro to print the diversion content in bold
.de MMM
.ft B
\\$1
.
.ft R
.br
MMM end
..
.\" Use the macro to process and print the diversion (I am
.\" not sure about asciify too)
.asciify t1
.asciify t2
.MMM \*[t1]
.de MMM2
.ft B
\*[t1]
.ft R
..
.MMM2
.de MMM3
.ft B
\\*[\\$1]
.ft R
..
.MMM3 t1
.MMM3 t2
.MMM3 t1