《Lua in ConTeXt》11:关键词与引用 register \keyword 引用 关键词和引用的统一 \keywords 结语

每一天的卡片,可能会记录一些重要的事情。任何事情,皆能概括为一个或几个简短的词汇。这些词就是关键词。

人生何其短暂,关键词能有多少?若想知其多少,就需要勤于记录,甚至为它们制作词典。

register

ConTeXt 有 register 功能,能够帮助我实现关键词的登记和展现。例如

\environment card-env
\defineregister[keyword]
\starttext
我想找回 \keyword[M]{MetaPost} 的力量。
\page
\placekeyword
\stoptext

结果如下图所示:

\MetaPost 没在「我想找回的力量」这句话里出现,是因为 \keyword[M]{MetaPost} 里的 MetaPost 并非在这句话里出现,而是要放在 \placekeyword 页面的。

倘若需要 MetaPost 出现在「我想找回的力量」这句话里,需要老老实实重复一次:

我想找回 \keyword[M]{MetaPost} MetaPost 的力量。

如果我想让 力量 成为关键词呢?只需

我想找回 \keyword[M]{MetaPost} MetaPost 的\keyword[L]{力量}力量。

结果如下图所示:

在上面 4 幅插图的第 2、4 幅里,可以看出,汉字以拼音首字母作为索引,英文单词以首字母作为索引,关键词后面的数字是页码。

\keyword

事实上,ConTeXt 在内部已经

\defineregister[index]

所以,\keyword\placekeyword 就没必要存在,用 \index\placeindex 即可。但是,我要继续用 \keyword,将它定义为

\def\keyword#1#2{\index[#1]{#2}#2}

于是与上一节最后示例等价的示例,可由以下代码构造:

\environment card-env
\def\keyword[#1]#2{\index[#1]{#2}#2}
\starttext
我想找回 \keyword[M]{MetaPost} 的\keyword[L]{力量}。
\page
\placeindex
\stoptext

引用

Register 是一种特殊形式的引用。\placeindex 所在的页面引用之前 \index 标记的内容。

更具一般性的引用,是用 \in\at 以及 \about 引用 \reference 标记的内容。例如……下面我要给出一个很长的例子,但它蕴含了 ConTeXt 诸多有用的排版命令:

\environment card-env
\starttext

\title[hi]{前言}

\indentation ... some text ...

\startitemize[n,broad]
\item ... some item ...
\item ... blab blab ...
\item[king] Long long ago, there lived a king ... ...
\stopitemize

... some text ...

\chapter[foo]{MetaPost}

我想找回 MetaPost\reference[bar]{MetaPost} 的力量。

\startuniqueMPgraphic{MetaPost-图形}
draw fullcircle scaled 2cm
     withpen pencircle scaled 1mm
     withcolor .625green
     dashed dashpattern (on 2mm off 2mm) ;
\stopuniqueMPgraphic

\placefigure[here][我是图片]{带引用标记的图片}{\uniqueMPgraphic{MetaPost-图形}}

\startbuffer[表格]
\startxtable[align={middle,broad}]
\startxrow
\startxcell \type{\in}\stopxcell
\startxcell \type{\at}\stopxcell
\startxcell \type{\about}\stopxcell
\stopxrow
\startxrow
\startxcell \in[hi]\stopxcell
\startxcell \at[hi]\stopxcell
\startxcell \about[hi]\stopxcell
\stopxrow
\startxrow
\startxcell \in[king]\stopxcell
\startxcell \at[king]\stopxcell
\startxcell \about[king]\stopxcell
\stopxrow
\startxrow
\startxcell \in[foo]\stopxcell
\startxcell \at[foo]\stopxcell
\startxcell \about[foo]\stopxcell
\stopxrow
\startxrow
\startxcell \in[我是图片]\stopxcell
\startxcell \at[我是图片]\stopxcell
\startxcell \about[我是图片]\stopxcell
\stopxrow
\startxrow
\startxcell \in[我是表格]\stopxcell
\startxcell \at[我是表格]\stopxcell
\startxcell \about[我是表格]\stopxcell
\stopxrow
\startxrow
\startxcell \in[bar]\stopxcell
\startxcell \at[bar]\stopxcell
\startxcell \about[bar]\stopxcell
\stopxrow
\stopxtable
\stopbuffer
\placetable[here][我是表格]{带引用标记的表格}{\getbuffer[表格]}

\stoptext

排版结果如下图所示:

ConTeXt 的无编号标题命令 \titlesubject\subsubject……有编号\chapter\section\subsection\subsubsection……插图命令 \placefigure,插入表格命令 \placetable 以及有编号列表等命令,皆支持给出引用标记。

一般而言,\in 可以获得带有引用标记的标题、插图、表格、列表项的编号。\at 可以获得引用标记所在页面的编号——页码。\about 获得引用标记的内容。

虽然上述排版结果已经足以表现出 ConTeXt 能够胜任内容复杂的文字排版工作,但是我依然建议,在 card-env.tex 里添加以下代码:

\setuplabeltext[en][chapter={第\kern .25em,\kern .25em 章\kern 1em}]
\setuplabeltext[en][figure=图\kern .5em]
\setuplabeltext[en][table=表\kern .5em]
\setupcaptions[table][location=top]
\setupcaptions[style=\tfx,headstyle=\normal,align={middle,borad}]

这些代码,名不见经传,我攒了好些年,一直有在用,它们的效果如下:

有所不美的是,倘若在 \startxtable ... \stopxtable 表格里使用 \reference,会造成 \in\at 以及 \about 的表现不够正常。对于常规排版而言,无伤大雅,但是对于我的时光卡片,意味着时光表格里不适合给出以及使用引用标记。

关键词和引用的统一

不能在时光表格里使用引用,我可以重新定义 \keyword 宏,将关键词和引用统一,然后在时光表格之外使用它。例如

\environment card-env
\def\keyword[#1]#2#3{\index[#1]{#3}\reference[#2]{#3}#3}
\starttext

\timestamp{2021 年 5 月 7 日}
关键词:\keyword[C]{ConTeXt}{\ConTeXt},\keyword[M]{MetaPost}{\MetaPost}

\timemachine{
  {[[在 \ConTeXt\ 里找回 \METAPOST\ 的力量。]], [[\m{\cdots}]]}
}
\page
引用示例:第 \at[MetaPost] 页\about[MetaPost],第 \at[ConTeXt] 页\about[ConTeXt]。

\title{关键词索引}
\setuptexttexts[margin][][]
\placeindex
\stoptext

结果如下图所示:

\keywords

如果有很多关键词,每输入一个,就要输入 \keyword。有没有办法定义一个 \keywords 宏,令它能批量输入关键词和引用标记?例如,如何由

\keywords{C::ConTeXt::\ConTeXt, M::MetaPost::\MetaPost, L::力量}

生成

关键词:\keyword[C]{ConTeXt}{\ConTeXt},\keyword[M]{MetaPost}{\MetaPost},\keyword[L]{力量}{力量}

呢?

现在我还不知道。等明天试试看吧!

结语

迂回了一番,终于又为 ConTeXt 里的 Lua 找到了用武之地。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章