《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 找到了用武之地。

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