\chapter{沙盘游戏}

先不要急于学习使用 \ConTeXt\ 写论文,出专著,虽然它极为擅长这类任务,但是我还是希望先将它当成一个可爱的朋友,慢慢去熟悉它,而不是功利主义视角下的工具。

\section{新手村}

在第 \in[Installation] 章中,为了验证 \ConTeXt\ LMTX 是否已成功安装,我使用三条 \type{echo} 命令构造了一份简单的 \ConTeXt\ 源文件 foo.tex。事实上,可以使用任何一个文本编辑器来做此事。foo.tex 文件内容如下:

\starttyping[option=TEX]
\startTEXpage[offset=1cm]
Hello \ConTeXt!
\stopTEXpage
\stoptyping

\noindent 执行以下命令\footnote{从本章开始,一直使用 Linux 风格的命令提示符 \type{$}。},调用 \type{context} 程序,可将 foo.tex 编译成 PDF 文件 foo.pdf:

\starttyping
$ context foo.tex
\stoptyping

\noindent 在使用 \type{context} 命令编译 ConTeXt 源文件时,可省略文件扩展名「\type{.tex}」,因此下面的命令与上述命令等效:

\starttyping
$ context foo.tex
\stoptyping

现在,想必你已经敏锐地觉察到了,凡是开头为反斜线「\type{\}」开头的英文单词,都是排版命令,的确如此。

排版命令 \type{\startTEXpage...\stopTEXpage} 称为 \type{TEXpage} 环境,我将其称为新手村。这一对排版命令所包含的内容,例如「\type{Hello \ConTeXt!}」会被排版于一个恰好能包含它的矩形排版空间。\type{frame} 参数用于控制边框是否开启,其值为 \type{off} 或省略对它的设定时,关闭边框。\type{offset} 参数可用于对排版空间进行扩大或缩小,如示例 \in[starting-area] 所示,排版空间从中心向四周被扩大 2.5 mm。

\startsample
\startTEXpage[frame=on,offset=2.5mm]
Hello \ConTeXt!
\stopTEXpage
\stopsample
\sample[option=TEX][starting-area]{新手村}{\externalfigure[02/starting-area.pdf]}

之所以称 \type{TEXpage} 环境为新手村,是因它足够简单,将其用于观察大多数排版命令的效果时无需关心版面的天头、地脚、订口、翻口、版心、页码等元素的设定。

\section{伪文字}

\ConTeXt\ 有一个可视化模块,提供了伪造单词的排版命令,使用该命令可以生成一些黑色的长短随机的矩形块,可将它们当成文字,从而在沙盘上可以更加随心所欲一些。

如示例 \in[true-words] 所示,排版两行文字,每行由 5 个单词组成。示例 \in[true-words] 给出了伪文字的效果。同样是两行文字,每一行由 3~5 个单词构成。

\startsample
\usemodule[visual] % 加载可视化模块
\startTEXpage[frame=on,offset=2.5mm]
This is the first line.\\ % 强制换行
This is the second line.
\stopTEXpage
\stopsample
\sample[option=TEX][true-words]{两行真实文字}{\externalfigure[02/true-words.pdf]}

\startsample
\usemodule[visual]
\startTEXpage[frame=on,offset=2.5mm]
\fakewords{3}{5}\\
\fakewords{3}{5}
\stopTEXpage
\stopsample
\sample[option=TEX][fakewords]{两行伪文字}{\externalfigure[02/fakewords.pdf]}

\section{注释}

在示例 \in[true-words] 的源代码中,「\type{%}」及其后面的同一行文字,是 \TeX\ 注释文本,它们会被 \type{context} 程序忽略,故而不会出现在排版结果中。注释是为了便于人类阅读 \ConTeXt\ 源代码而存在的。

\section{换行符}

在示例 \in[true-words] 和 \in[fakewords] 的源代码中,「\type{\\}」是强制换行命令,倘若将其删除,即使在源代码中将文字分为两行,例如

\starttyping[option=TEX]
This is the first line.
This is the second line.
\stoptyping

\noindent 排版结果依然是一行,而且换行符会被 \type{context} 程序视为一个空格,不妨亲自动手试验一下。

在使用换行符的情况下,即使两行文字在源代码中处于同一行,例如

\starttyping[option=TEX]
This is the first line.\\ This is the second line.
\stoptyping

\noindent 排版结果依然是两行。

\type{\crlf} 也能用于对文字强制进行断行。倘若不希望对每一行都输入断行命令,也可以考虑使用 \type{lines} 环境,请参考示例 \in[lines]。

\startsample
\usemodule[visual]
\startTEXpage[frame=on,offset=2.5mm]
\startlines
\fakewords{3}{5}
\fakewords{4}{7}
\fakewords{5}{9}
\stoplines
\stopTEXpage
\stopsample
\sample[option=TEX][lines]{排版多行文本}{\externalfigure[02/lines.pdf]}

\section{分段}

观察示例 \in[pars],虽然排版结果依然是两行,但实际上它是两段。\type{\par} 是分段命令。

\startsample
\usemodule[visual]
\startTEXpage[frame=on,offset=2.5mm]
\startlines
\fakewords{3}{5}\par
\fakewords{3}{5}
\stoplines
\stopTEXpage
\stopsample
\sample[option=TEX][pars]{分段}{\externalfigure[02/pars.pdf]}

通常很少使用分段符对文本进行分段,因为在 \ConTeXt\ 源文档中,只需在两段文字之间空一行便可实现分段。示例 \in[pars-2] 使用空行进行分段,并通过限定「新手村」的宽度为 6 cm,从而在促狭的空间里展示了多行伪文字构成的段落。

\startsample
\usemodule[visual]
\startTEXpage[frame=on,offset=2.5mm,width=6cm]
\fakewords{9}{15}

\fakewords{9}{15}
\stopTEXpage
\stopsample
\sample[option=TEX][pars-2]{多行文本构成的段落}{\externalfigure[02/pars-2.pdf]}

段落可以设置首行缩进。中文排版的惯例是,段落首行需缩进 2 个汉字的宽度,示例 \in[pars-3] 实现了该需求,排版结果中段落开头的蓝色矩形区域表示文字缩进后产生的空白区域。尺寸 2em 即英文字母 \type{M} 的宽度的 2 倍,刚好与两个汉字的宽度相同。还有一个常用的尺寸单位 ex,它是英文字母 \type{x} 的高度。

\startsample
\usemodule[visual]
\startTEXpage[frame=on,offset=2.5mm,width=6cm]
\setupindenting[first,always,2em]
\fakewords{9}{15}

\fakewords{9}{15}
\stopTEXpage
\stopsample
\sample[option=TEX][pars-3]{设置段落首行缩进}{\externalfigure[02/pars-3.pdf]}

根据文档「Faking text and more」\cite[faking-text]的提示,可将段落缩进区域的颜色定义为白色,从而可避免蓝色矩形块对视觉的干扰,见示例 \in[parindentcolor]。

\startsample
\usemodule[visual]
\startTEXpage[frame=on,offset=2.5mm,width=6cm]
\setupindenting[first,always,2em]
% 将缩进区域的颜色由默认的蓝色改为白色
\definecolor[fakeparindentcolor][white]
\fakewords{9}{15}

\fakewords{9}{15}
\stopTEXpage
\stopsample
\sample[option=TEX][parindentcolor]{消除段落缩进色块}{\externalfigure[02/parindentcolor.pdf]}

在设定段落首行缩进后,若不希望某个段落的首行被缩进,可在段落开头放置命令 \type{\noindent}。示例 \in[noindent],第二段消除了第二段文字的首行缩进。

\startsample
\usemodule[visual]
\startTEXpage[frame=on,offset=2.5mm,width=6cm]
\setupindenting[first,always,2em]
\definecolor[fakeparindentcolor][white]
\fakewords{9}{15}

\noindent\fakewords{9}{15}
\stopTEXpage
\stopsample
\sample[option=TEX][noindent]{消除第二段的首行缩进}{\externalfigure[02/pars-4.pdf]}

\section[interlinespace]{行间距}

\ConTeXt\ 默认的段落内各行文字的间距是 2.8 ex,约等于 \ConTeXt\ 默认的正文字体大小 12 pt。可使用 \type{\setupinterlinespace}  对行间距进行调整。如示例 \in[pars-5] 将行间距设为 1.75 倍默认行距。

\startsample
\usemodule[visual]
\startTEXpage[frame=on,offset=2.5mm,width=6cm]
\setupindenting[first,always,2em]
\definecolor[fakeparindentcolor][white]
\setupinterlinespace[1.75]
\fakewords{9}{15}

\fakewords{9}{15}
\stopTEXpage
\stopsample
\sample[option=TEX][pars-5]{多行文本构成的段落}{\externalfigure[02/pars-5.pdf]}

不过,我发现示例 \in[pars-5] 的行间距设定语句若放在 \type{\startTEXpage} 语句之前则无效\footnote{在 \in[context-world] 节中会介绍 \type{\starttext ... \stoptext},直接以默认行距倍数的方式设置行间距,若设定语句位于 \type{\starttext} 之前则同样无效。},也许是当前的 \ConTeXt\ LMTX 版本存在 bug。

还有一种行间距设定方法。待你掌握第 \in[chinese-fonts] 章中讲述的 \ConTeXt\ 字体设定方法,并清楚正文字体的大小时,可使用 \type{line} 参数设定最大行高。例如,若正文字体大小为 11 pt,以下代码可设定行间距为 1.75 倍:

\starttyping[option=TEX]
\setupinterlinespace[line=19.25pt]
\stoptyping

\noindent 之所以是 19.25 pt,是因它等于 $1.75\times 11$ pt。最大行高即行间距,因为在 \CONTEXT\ 中,行间距是相邻两行文字的基线距离,恰好等于一行文字的最大高度。本文档在后文示中皆使用该方式设定段落内行间距。

\section{对齐}

有时,希望将一行文字居左、居中或居右放置,可分别使用 \type{\leftaligned},\type{\midaligned} 和\type{\rightaligned} 进行排版,请参考示例 \in[aligned-1]。

\startsample
\usemodule[visual]
\startTEXpage
 [frame=on,offset=2.5mm,width=6cm]
\leftaligned{\fakewords{1}{2}}
\midaligned{\fakewords{1}{2}}
\rightaligned{\fakewords{1}{2}}
\stopTEXpage
\stopsample
\sample[option=TEX][aligned-1]{多行文本构成的段落}{\externalfigure[02/aligned-1.pdf]}

如果是一段文字需要居左、居中或居右排版,可使用 \type{alignment} 环境,通过该环境的参数控制对齐形式。示例 \in[aligned-2] 展示了段落的三种对齐形式。

\startsample
\usemodule[visual]
\startTEXpage[frame=on,offset=2.5mm,width=6cm]
\startalignment[flushleft] % 左对齐
1. \fakewords{5}{15}
\stopalignment
\startalignment[middle] % 居中对齐
2. \fakewords{5}{15}
\stopalignment
\startalignment[flushright] % 右对齐
3. \fakewords{5}{15}
\stopalignment
\stopTEXpage
\stopsample
\sample[option=TEX][aligned-2]{段落对齐}{\externalfigure[02/aligned-2.pdf]}

\section{写一封谁也看不懂的信}

现在,我们已经有能力用伪文字排版一封谁也看不懂内容的书信了,见示例 \in[letter]。也许现在你很想用汉字给朋友写封信,并告诉他,这封信的排版是你亲手用 \ConTeXt\ 制作的。我劝你暂时最好放弃这个想法,写英文书信是完全可以的。

\startsample
\usemodule[visual]
\startTEXpage
 [frame=on,offset=1cm,width=10cm]
\definecolor[fakeparindentcolor][white]
\setupindenting[first,always,2em]
\noindent\fakewords{1}{1}:\par
\fakewords{1}{2}\par
\fakewords{20}{50}\par
\fakewords{20}{50}\par
\fakewords{20}{50}\par
\fakewords{1}{1}\par
\noindent\fakewords{1}{1}\par
\rightaligned{\fakewords{1}{1}}\par
\rightaligned{\fakewords{1}{1}}
\stopTEXpage
\stopsample
\sample[option=TEX][letter]{一封谁也看不懂的信}{\externalfigure[02/letter.pdf][width=.425\textwidth]}

\section{小结}

恭喜你,走出了新手村。在下一章学会安装和使用汉字字体,便可以闯荡 \ConTeXt\ 世界了。