一页是叠起来的盒子,而 TeX 只排其中一个
TeX 知道的只有版心:一个 \textwidth 乘 \textheight 的矩形,双栏排版时再被 \columnsep 细分。别的一切——纸张尺寸、页眉、页脚、边注——都表示成从这个块边缘出发的一个距离。这就是为什么文档的版面是一小把长度而不是一张你可以往上画的画布,也是为什么一个版面宏包能凭五个数字重写整页。
水平方向,从纸张左边到版心左边的距离是 1in + \hoffset + \oddsidemargin(双面文档的偶数页上是 \evensidemargin)。垂直方向,页眉盒的顶部落在上边缘下方 1in + \voffset + \topmargin 处,接着是 \headheight、\headsep、版心本身,然后是 \footskip,而它不是从块的底边量起,是从最后一条文字基线量到页脚的基线。
那个一英寸项是关于 LaTeX 页面几何最被误解的事实,而它是从 TeX 的一个假设继承来的:输出是给一位在虚拟页面上工作、页面内缩一英寸的排版师准备的。这就是 \oddsidemargin=0pt 并不把文字放到纸张边缘、而是放在 25.4mm 之内的原因,也是手设边距只有先减掉一英寸才看起来正常的原因。老类文件与模板里所有靠加负值来做事的地方,都是在跟这个常数搏斗。
动手之前值得知道的一个后果:如果垂直各部分之和大于纸张,TeX 不会抱怨几何,它会逐页抱怨超出。日志消息是一条 badbox 行写着 Height too large,或者一个巨大的 Overfull \vbox——经典的那条是 115.7pt too high——而修法在几何,不在那个段落。这也正是 geometry 的 heightrounded 选项所预防的事,做法是把 \textheight 向下取整到 \baselineskip 的整数倍。
\begin{array}{rcl}\text{left edge to text} & = & \text{one inch} + \texttt{hoffset} + \texttt{oddsidemargin} \\ \text{top edge to header} & = & \text{one inch} + \texttt{voffset} + \texttt{topmargin}\end{array}
geometry:每个键实际写下什么
\usepackage[margin=2.5cm]{geometry} 就是大多数文档需要的全部接口,而且它是一台计算器而不是一个开关:你陈述你在意的那些距离,宏包解出 \textheight、\textwidth、两侧与上下的长度以及页眉页脚的间距,然后把它选定的值打印进日志,让你能读回来。
边缘类的键:margin 管四边,hmargin 与 vmargin 管一对,left/right/top/bottom 逐个给,inner/outer 在文档是 twoside 时指装订边与书口边。bindingoffset 在内侧加上空间而不挪动别的任何东西,这是为送印论文做准备的唯一正确写法。块类的键是反方向的:textwidth=/width= 与 textheight=/height= 直接陈述版心本身、让宏包去选边距,total= 陈述版心加页眉页脚。
包含类的键是困惑最集中的地方,因为它们改变 top 这类词的含义。默认边距只作用于版心:top=3cm 意思是从纸张边缘到第一行文字是 3cm,页眉被放在这段空间之内。加上 includehead,页眉算在这 3cm 之内、版心从更低处开始;includefoot 对页脚做同样的事,includeheadfoot 两者都做。所以一个在你加了 \pagestyle{headings} 之后页眉撞上正文的文档,需要的不是更大的 \headsep——而是 includehead。
第二族键按比值而不是绝对尺寸来设版心,而这正是古典版面设计做的事:hdivide={*,12.5cm,*} 与 vdivide={...} 排出具名的条带,ratio=9:16(或 hratio、vmarginratio)按比例从纸张定出版心。vcenter/centering 决定块是否垂直居中,而 invertedmargin 是一个调试玩具,它把文字与边距互换,让你一眼看见几何。
\begin{array}{lll}\text{key} & \text{writes} & \text{counts the header?} \\\hline \texttt{top=3cm} & \texttt{topmargin} & \text{no, unless includehead} \\ \texttt{textwidth=12cm} & \texttt{textwidth} & \text{irrelevant} \\ \texttt{includefoot} & \text{reuses footskip} & \text{yes, the footer}\end{array}
\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
% The one package that owns page geometry. Options at load time are the same
% as \geometry{...} in the preamble, split over lines here for legibility.
\usepackage[%
a4paper, % paper: letterpaper, b5paper, a5paper, ...
margin=2.5cm, % all four edges at once
headheight=14pt, % fancyhdr will ask for this explicitly
headsep=16pt,
footskip=28pt,
includeheadfoot, % the header/footer live INSIDE the 2.5cm
bindingoffset=8mm, % extra room on the inner side, for printing
twoside, % inner/outer instead of left/right
heightrounded, % an whole number of lines per page
showframe, % draw the box edges -- remove for the final build
]{geometry}
\begin{document}
% What the keys resolved to: look for these lines in the .log file.
\typeout{geometry gave: textwidth=\the\textwidth, textheight=\the\textheight,
topmargin=\the\topmargin, oddsidemargin=\the\oddsidemargin}
\section*{A page is a decision, not a canvas}
The text block is the only thing TeX lays out; everything else on this page
is a distance from an edge of the paper to a side of that block. Load
\texttt{geometry}, state the block once, and stop touching it: any
\texttt{\textbackslash vspace} or \texttt{\textbackslash enlargethispage} used
to repair one bad page will move two pages later, because the reflow changes
line breaking from there on.
\end{document}
只给文档一部分换一套版面,并在纸上看清它
宏包接口并不在加载那一刻冻结:导言区里再来一句 \geometry{headheight=15pt} 会增改设置,而 \newgeometry{...} / \restoregeometry 给一个区域套上它自己的版面——一页宽表、一张横向插图、一首想要窄版心的诗。\newgeometry 会替你发一条 \clearpage,所以它只能从页面边界开始:那个区域会从下一页顶端起算,指望它从页中生效的人会在这里被绊一下。
\savegeometry{wide} 与 \loadgeometry{wide} 给一套配置起名字,于是长长的附录可以在两个已知状态之间切换,而不必每次重列六个键;它们也让这个改动在 diff 里可审。这就是「版面作为一个设计决定」与「版面作为跟页面的一场争吵」之间的区别。
要看清自己有什么,有三件工具。showframe 在每个边界画一条线——版心、页眉、页脚、边注盒、纸张边缘——这是最快搞清某样东西为什么在那儿的办法。layout 宏包打印一张带标注的样页(\layout 及同族),显示块、行、边距和最后一页的深度,那就是印厂要规格时该夹在文件夹里的图。而写在导言区的 \typeout{\the\textwidth, \the\textheight} 会把真实数字送进日志,因为在你的源码说 margin=2.5cm 的那一刻,geometry 还没把它们解出来。
不该做什么,按被尝试的频率排序:在导言区之后改 \textwidth 并指望页面正确重排(浮动体、页眉线和图题宽度早就算好了);用 \oddsidemargin 设边距却忘了那一英寸;把 \enlargethispage 当间距工具用而不是当单页修补;以及在已经加载 geometry 的文档里去动类的原始长度——宏包会直接把它们覆盖掉。
纸张尺寸,以及必须与之一致的输出驱动
在标准类里纸张是一个类选项——\documentclass[a4paper]{article},同样有 letterpaper(美国安装的默认)、a5paper、b5paper、legalpaper、executivepaper,再加上旋转版心的 landscape——同时它也是一个 geometry 键,两者都给了以后者胜出。两条路都会设 \paperwidth 与 \paperheight;geometry 的键还会额外重算边距,而单独的类选项不会。
设好这两个长度并不等于告诉了输出驱动。pdfTeX 用自己的原语(\pdfpagewidth、\pdfpageheight)写 media box,走 dvips 的路径则读一条 papersize special;两边不一致时,你得到一份屏幕上看着对、打印出来被裁掉的文档,或者一个印在 letter 纸正中的 A4 版心。geometry 会把驱动那一半也管起来——它的 paper=/a4paper 键同时设长度与驱动尺寸,而且它接受 pdftex、xetex、luatex、dvips、dvipdfm 这些驱动选项,以防自动探测失败。
决定的另一半是类。印厂或学校提供的类文件已经选好版心、行距、常常还有字体,其文档会写明该传 a4paper 还是到底能不能用 \geometry;用自己的几何去覆盖一个机构类,等同于手工覆盖期刊的文献样式。若没有类文件,标准路子是:纸张尺寸作类选项,边距通过 geometry,别的都不加。
书籍还有第二个传统,比 geometry 更老:KOMA-script 的 typearea 从纸张和一个分栏数算出古典比例的版心(\usepackage[DIV=9,BCOR=15mm]{typearea},DIV 越大块越小、边距越宽,BCOR 是装订补偿),memoir 用它自己的命令做同样的事:\settypeblocksize{245mm}{145mm}{*}、\setlrmargins{*}{*}{1.4}、\setulmargins{*}{*}{1.2},然后是 \checkandfixthelayout——任何一项之后这条都是必须的,因为宏包要重新推出所有依赖它的长度。
% Two ways to get a classically proportioned page without geometry.
\documentclass[12pt,a4paper,twoside,openright]{book}
% Route A: KOMA-script's typearea computes the text block from the paper and
% a division ratio (the bigger DIV, the smaller the block).
\usepackage[DIV=9,BCOR=15mm]{typearea}
% Route B: the raw TeX lengths, which is what every layout package writes.
% Remember the one-inch assumption: \oddsidemargin is measured from 1in in.
% \setlength{\textwidth}{14.5cm}
% \setlength{\textheight}{24.5cm}
% \setlength{\oddsidemargin}{-0.6cm} % 1in - 0.6cm from the paper edge
% \setlength{\topmargin}{0.5cm} % 1in + 0.5cm down to the header
% \setlength{\headheight}{14pt}
% \setlength{\headsep}{18pt}
% \setlength{\footskip}{26pt}
% \addtolength{\textheight}{-\headheight}
% \addtolength{\textheight}{-\headsep}
\usepackage{setspace}
\usepackage{microtype}
\begin{document}
\onehalfspacing % \setstretch under the hood, applied for you
\raggedbottom % do not stretch the last page of a chapter
\chapter{Why heightrounded matters}
If \texttt{textheight} is not an exact multiple of \texttt{baselineskip}, TeX
either overflows the page by a fraction of a line -- the log calls it
\texttt{Height too large} in a \texttt{badbox} -- or leaves the last baseline
short. Rounding the height to a whole number of lines is the difference
between a stable page grid and one that shifts when you edit a paragraph.
\end{document}
行距:寄存器、倍率与宏包
基线到基线的距离是 \baselineskip,而它由当时生效的字号开关选定:标准 article 类里 10pt 的字配 12pt 的行,12pt 基准上是 12pt 配 14.5pt。人们说一份文档「太挤」时,几乎总是在描述这个比值而不是字号,所以该改的对象是行距,而且它应该被改一次、改在全局。
原始机制是 \linespread{factor}\selectfont。\linespread 只存一个乘数,新值要等下一次选字号时才被应用——这就是光写 \linespread{1.05} 不加 \selectfont 却什么都看不见的原因,也是这一对属于导言区的原因,反正类会在那里选 \normalfont。\selectfont 不是装饰;漏掉它是一次行距改动「被无视」最常见的原因。
setspace 宏包包装了同一机制,并补上裸乘数漏掉的那些部分:\setstretch{1.18} 替你把倍率应用上,\onehalfspacing 与 \doublespacing 给出文字处理器的两个惯例,spacing/doublespace 环境把改动限制在一块之内。两个具名设定有意不是 1.5 和 2.0——它们是照着类已经提供的行距校准的,这就是双倍行距的 LaTeX 看起来比文字处理器的双倍更松、却同样满足投稿指南要求的原因。setspace 还把图题和脚注留在自己更紧的行距上,正文加倍时页边的文字不会跟着加倍。
两个相邻的寄存器补完垂直这个故事。\parskip 是段落之间的缝,它属于上一课做过的那次段落样式决定,不属于行距;它里面的伸缩量(0.45\baselineskip plus 2pt minus 1pt)才是让页面能呼吸的东西。而 \lineskip 配合 \lineskiplimit 会在相邻两行高到标称基线距离分不开时接管——这就是一页里连排几个嵌套分数时可能看起来不匀、而文档其余部分正常的原因。
\begin{array}{c}\texttt{topmargin}\;(\text{from one inch down}) \\ \hline \texttt{headheight} \to \texttt{headsep} \to \underbrace{\texttt{textheight}}_{\text{the text block}} \\ \hline \texttt{footskip}\;(\text{last baseline to the footer baseline})\end{array}
双面页、运行页眉与边注
twoside 文档有两种不同的文字位置,所以类分别索要 \oddsidemargin 与 \evensidemargin,而 geometry 改用 inner 与 outer 来说话。这样分的排版学理由是装订:内侧边距是读者拇指要争的那个,所以它应该更大,而一本装订起来的文档如果左右对称,读起来就发紧。印厂要的是具体的涂胶余量而不是视觉边距时,加 bindingoffset=10mm。
运行页眉来自 \markboth{left-side text}{right-side text}(或 \markright),由 \pagestyle{headings} 供料,类会在章和节的标题处填它。双面文档里左侧页眉通常放章名、右侧放节名,这就是类在每个标题处都写两份标记的原因——也是 fancyhdr 才是改动它的诚实办法的原因,用 \fancyhead[LE,RO]{\nouppercase{\leftmark}} 这一类的槽位。它那条著名警告 headheight is too small (12.0pt) 不是烦人:它说的是页眉内容放不进几何给它留的盒子,修法是 geometry 里写 headheight=14pt,这会一并把版心往下移。
边注是一个自带三个长度的盒子:\marginparwidth、\marginparsep 与 \marginparpush,而双面页上可用宽度左不等于右,于是在正面页放得下的注在反面页上可能悬出。裸的 \marginpar 把注放在当前行,是碰撞的磁铁;marginnote 宏包让注独立于断行来定位,marginfix 则重排它们让它们不再互相压住,而 KOMA-script 提供 \marginline{...},把对齐的活儿替你做掉。
如果你的文档要文字与边注处在同一个垂直位置上、而页边里放的是真正的段落,那是另一种版面——Tufte 式——拥有它的是 tufte-latex 或 KOMA 的宽页边类。在那个世界里页面几何与 \marginpar 的行为是设计上耦合的,手调其中任何一个长度都会破坏另一个。
\documentclass[10pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[a4paper,top=3cm,bottom=3.5cm,left=2.5cm,right=2.5cm]{geometry}
\usepackage{setspace}
\usepackage{needspace}
\usepackage{marginnote}
\usepackage{fancyhdr}
\setstretch{1.18}
\setlength{\parskip}{0.45\baselineskip plus 2pt minus 1pt}
\setlength{\parindent}{0pt}
% widows and clubs: one line of a paragraph left behind is worth a page break
\widowpenalty=10000
\clubpenalty=10000
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{\small\nouppercase{\leftmark}}
\fancyfoot[C]{\thepage}
\renewcommand{\headrulewidth}{0.4pt}
\begin{document}
% \newgeometry issues a \clearpage of its own, so it can only be used after
% \begin{document} -- never in the preamble, where \geometry{...} belongs.
\newgeometry{left=4cm,right=2cm} % a wide-inner region for asides
\section{Margins are set once}
A wide-margin region for a quotation, then the document geometry back again:
\marginnote{Margin notes are a box of their own: \texttt{marginparwidth}
sets the width, \texttt{marginparsep} the gap to the text, and
\texttt{marginparpush} the minimum distance between two of them.}
\begin{quote}
Quoted material inherits the text block, not the page. If the quote wants
different margins, that is an environment with its own \leftmargin,
not a change to the geometry.
\end{quote}
\restoregeometry
\section{Space that must not collapse}
\needspace{7\baselineskip}
This heading and the six lines below it belong together; \texttt{needspace}
starts a new page rather than splitting them, which is what
\verb|\nopagebreak| and a chain of forced breaks only imitate.
\end{document}
罚值:一页怎么决定自己在哪儿结束
断页是一个优化过程,而它优化的数字是公开的。\widowpenalty 给留在下一页顶端的一行标价,\clubpenalty 给困在上一页底部的首行标价,\displaywidowpenalty 管行间公式前的那一行,\brokenpenalty 管断在连字符行之后的页,\interlinepenalty 管一个被拆开段落的每一条中间行。TeX 自己的默认值很温和;对散文的标准建议是导言区里的 \widowpenalty=10000 与 \clubpenalty=10000,这等于让两种情形都不可能发生。
罚值也是「这一块不许拆」的答案:行末的 \nopagebreak、标题前的 \needspace{6\baselineskip}、把一个段落和它的图包起来的 minipage,或者一张绝不能移动的图的 [H] 式非浮动写法。它们每一个都是关于页面的一句陈述,而不是一团胶,所以改稿之后仍然成立——这正是手工放的 \vspace 做不到的。
浮动体有自己的预算,而一份「页顶全是空白」的文档通常是浮动体放置问题而不是边距问题。要紧的五个数是 \textfraction(一页必须留给文字的最小比例)、\topfraction 与 \bottomfraction(浮动体在顶部或底部最多占多少)、\floatpagefraction(一整页纯浮动体必须有多满)以及 \totalnumber(一页最多共享几个浮动体)。把 \topfraction 抬到 0.85、\floatpagefraction 抬到 0.8 是标准的一对;在导言区用 \renewcommand 对 \topfraction 这一族来做,并且宁可用 placeins 宏包的 \FloatBarrier,也不要拿一串 \clearpage 当散弹枪。
最后是最后一个垂直决定:\raggedbottom 让短页在它自己结束的地方结束,\flushbottom 则拉伸行间胶,让最后一条基线落在一个固定高度。article 默认前者,report 与 book 默认后者,这就是论文里一章的最后一页行距比别处松的原因。若这件事烦你,开关是导言区的一行——而你不该做的,是给那一页手工加空间。
一份能为自己辩护的版面,五行写完
从需求出发,而不是从样子出发。一处研究生院要求「A4、2.5cm 边距、1.5 倍行距、15mm 装订余量」,直接翻译成 \documentclass[12pt,a4paper,twoside,openright]{report} 加 \usepackage[a4paper,inner=2.5cm,outer=2.5cm,top=3cm,bottom=3.5cm,bindingoffset=15mm,includeheadfoot,headheight=14pt,heightrounded]{geometry} 加 setspace 的 \onehalfspacing。页面上再没有别的版面决定;剩下的都是字体排印,而字体排印是另一场争论。
然后在真会读它的机器上检查结果:带 showframe 编一次,看第一页和一张有行间公式的页,再在日志里找 Overfull \vbox (…pt too high)——heightrounded 本该让它不可能出现——以及 headheight is too small 那条警告,它说的是页眉盒从来没为你塞进去的内容算过尺寸。
数一数满页的行数和满行的字符数。单栏正文字体大约 60 到 75 字符的版心宽度是好读的区间,而这个数字就是从 \textwidth 除以你所用字号下该字体的平均字符宽度得来的——这也是为什么 \typeout{\the\textwidth} 加一眼正文,比一个关于边距的观点是更好的设计工具。若版心太宽,修法是块的宽度或者双栏布局(twocolumn,或 multicol 宏包配 \columnsep 与 \columnseprule),不是更小的字。
让这一切保持可维护的那条规则:几何属于导言区,间距属于宏包,断点属于罚值,唯一的单页工具是 \enlargethispage——用在一页上、为一个理由,并写一条注释说明是什么。你对页面打的每一个其他局部补丁,都是一份「下一次编辑会弄坏两页」的承诺。
本课速查
每节抽一行关键代码:点小节名跳到对应位置,代码一键复制。
\begin{array}{rcl}\text{left edge to text} & = & \text{one inch} + \texttt{hoffset} + \texttt{oddsidemargin} \\ \text{top edge to header} & = & \text{one inch} + \texttt{voffset} + \texttt{topmargin}\end{array}
\documentclass[11pt]{article}
\documentclass[12pt,a4paper,twoside,openright]{book}
\begin{array}{c}\texttt{topmargin}\;(\text{from one inch down}) \\ \hline \texttt{headheight} \to \texttt{headsep} \to \underbrace{\texttt{textheight}}_{\text{the text block}} \\ \hline \texttt{footskip}\;(\text{last baseline to the footer baseline})\end{array}
\documentclass[10pt,a4paper]{article}
常见错误
- 设
\oddsidemargin=0pt却照样得到一英寸边距。TeX 是从离纸张边缘一英寸的那个点起量版心的,所以真实的 2.5cm 边距是-0.6cm——或者,好得多的办法:让geometry去做这道算术。 - 写了
\linespread{1.5}后面不跟\selectfont,然后断定这条命令坏了。这个倍率只在选字号时被读取;setspace会替你处理这一对。 - 给类传
a4paper却给geometry传另一种纸,或者手设\paperwidth:驱动只写一个 media box,而打印机会把页面裁掉。 - 很晚才加、或根本不加
includehead/includefoot,然后与一个坐在上边距之内的页眉吵架。没有这些键时,边距只作用于版心本身。 - 用
\\、\vspace或一个空段落去跟孤行和被拆的块打架。设\widowpenalty与\clubpenalty,或者把块包进minipage、用\needspace索要空间。 - 把
Overfull \vbox ... (badness)和headheight is too small当普通警告忽略。两句都点名了一个偏差已知的长度,而两者都是在导言区里修,不是在页面上修。 - 用
\newgeometry在文档中途改几何,却指望它从当前行起生效:它先结束这一页,因为版心不可能在一页之中改变。 - 用
\enlargethispage手工修一页然后一直留着它。它是一次单页修补;上游文字一重排,它就变成两节之后一个无法解释的大洞。
关于本课的常见问题
LaTeX 怎么改页边距?
\usepackage[margin=2.5cm]{geometry},或者逐个给 left/right/top/bottom,双面文档用 inner/outer。不要手设 \oddsidemargin 和 \topmargin,除非你是有意绕开 TeX 假设的那一英寸偏移——geometry 存在的意义就是替你做这道算术。LaTeX 里怎么设 1.5 倍或双倍行距?
\doublespacing;\onehalfspacing 给出多数投稿指南要的 1.5 行惯例,\setstretch{1.18} 接受任意倍率。doublespace 的值并不是字面上的两倍,因为类本来就已经提供行距,而宏包会让图题和脚注比正文更紧。怎么把纸张尺寸设成 A4?
\documentclass[a4paper]{article} —— 要么作为 geometry 键,\usepackage[a4paper]{geometry}。当你同时设边距时请用 geometry,因为它会一起调整纸张与版心,并把正确的尺寸传给输出驱动;单独的类选项只改长度。LaTeX 里的 textheight 和 textwidth 是什么?
\topmargin、\headheight、\headsep,下方是 \footskip,左边是 \oddsidemargin 或 \evensidemargin。一页能放多少行等于 textheight 除以 baselineskip,这就是 geometry 为什么有一个 heightrounded 选项。为什么我的 LaTeX 页面底部几乎是空的?
\textfraction),并为顶部的浮动体设了上限(\topfraction、\bottomfraction),于是一张固执的图会把文字往下推。把 \topfraction 和 \floatpagefraction 稍微抬高,或用 placeins 宏包的 \FloatBarrier 挪动那张图,就能修好这些页面而不动边距。怎么让标题跟着它后面的文字走?
\needspace{4\baselineskip} 会在四行拿不出来时另起一页。类本来通过 \@afterheading 把标题与后文粘在一起,所以一页末落单的标题,几乎总是后文是浮动体或表格、而不是纯文字的那种情形。实时演示:直接编辑 LaTeX,立刻看渲染结果
预览随输入实时更新——不妨故意写坏一次,看看报错长什么样。
继续读
同一个东西的三个入口:记法本身、讲它的那一课、以及真正用它写东西的一篇文章。