了解 TeX 和 LaTeX 水平规则

了解 TeX 和 LaTeX 水平规则

我只想在文本框顶部设置一条水平线,使其与顶部边距接近。我想知道为什么这不是一个微不足道的问题。

据我了解,LaTeX\rule命令会填充框,放置在它被调用的地方,以水平模式处理。

另一方面,TeX 基元\hrule制作一个水平填充\hsize垂直模式处理的盒子(长度)。

首先,我不太确定第二个肯定句。但如果可以的话,我有两个问题:

1) 为什么在文档开头调用 TeX 时,它会在第一个 hrule 上方插入一个空格?如何避免这种情况?

2) 为什么 TeX hrule 意味着减少两个基线之间的空间? 以及如何保持它?

\documentclass{article}
\usepackage{fullpage, showframe}

\begin{document}
\hrule
One line
\hrule
Another line
\end{document}

在此处输入图片描述

答案1

\hrule是垂直列表中的黑色矩形。您可以指定所有三个尺寸:

\hrule height1cm depth4mm width8cm \relax

或者缺少某些尺寸规范。如果缺少深度,则默认值为 0pt。如果缺少高度,则默认值为 0.4 pt。如果缺少宽度,则宽度\hrule等于垂直列表中最宽的元素。它通常是 \hsize,因为您通常有一个段落输出在垂直列表中(它具有 \hsize 宽度)。但如果没有段落输出或\hsize各个段落的 发生了变化,则语句:\hrule其宽度等于\hsize不成立。

ad 1)\topskip寄存器。请参阅 egreg 的回答。

广告 2) 减少基线:\baselineskip在垂直列表中使用值应用\prevdepth。它是垂直列表中前一个框的深度。但放置\hrule会删除此\prevdepth值,因此下一行会在没有垂直空间的情况下立即添加\hrule。但您可以保留该值并在插入\prevdepth后使用它:\hrule

first line
\par % end of praragraph, we are in vertical mode
\dimen0=\prevdepth % we keep the \prevdepth value
\hrule % or \hrule width \hsize if you want to be sure that the width is \hsize
\kern-0.4pt % return to the position before \hrule is inserted
\prevdepth=\dimen0 % restoring \prevdepth
second line

我猜你不想把 放在\hrule行与行之间的不同位置。上面的解决方案不使用固定位置,但 \hrule 的位置取决于前一行的深度:它被放置在第一行下方,没有空格。

如果您想使用独立于前一行深度的固定位置,那么代码应该是:

first line
\par % end of paragraph, we are in vertical mode
\dimen0=\prevdepth % we keep the \prevdepth value
\kern-\dimen0 % now, our position is at the baseline of the previous line 
\kern3pt % we want to insert space 3pt between baseline and \hrule
\hrule
\kern-3.4pt % now, the position is at the baseline of the previous line
\kern\dimen0 % now, the position is at the same place as immediately after \par
\prevdepth=\dimen0 % restoring \prevdepth value
second line

答案2

您将得到一个\vskip数量\topskip减去规则的高度,即页面顶部的 9.6pt。

如果你添加\showoutput到你的文档中,你会看到

..\vbox(550.0+0.0)x345.0, glue set 525.65497fil
...\write-{}
...\glue(\topskip) 9.6
...\rule(0.4+0.0)x*
...\glue(\parskip) 0.0 plus 1.0
...\hbox(6.94444+0.0)x345.0, glue set 293.33327fil
....\hbox(0.0+0.0)x15.0
....\OT1/cmr/m/n/10 O
....\OT1/cmr/m/n/10 n
....\OT1/cmr/m/n/10 e
....\glue 3.33333 plus 1.66666 minus 1.11111
....\OT1/cmr/m/n/10 l
....\OT1/cmr/m/n/10 i
....\OT1/cmr/m/n/10 n
....\OT1/cmr/m/n/10 e
....\penalty 10000
....\glue(\parfillskip) 0.0 plus 1.0fil
....\glue(\rightskip) 0.0
...\rule(0.4+0.0)x*
...\glue(\parskip) 0.0 plus 1.0
...\hbox(6.94444+0.0)x345.0, glue set 275.2499fil
....\hbox(0.0+0.0)x15.0
....\OT1/cmr/m/n/10 A
....\OT1/cmr/m/n/10 n
....\OT1/cmr/m/n/10 o
....\OT1/cmr/m/n/10 t
....\OT1/cmr/m/n/10 h
....\OT1/cmr/m/n/10 e
....\OT1/cmr/m/n/10 r
....\glue 3.33333 plus 1.66666 minus 1.11111
....\OT1/cmr/m/n/10 l
....\OT1/cmr/m/n/10 i
....\OT1/cmr/m/n/10 n
....\OT1/cmr/m/n/10 e
....\penalty 10000
....\glue(\parfillskip) 0.0 plus 1.0fil
....\glue(\rightskip) 0.0
...\glue 0.0 plus 1.0fil
...\glue 0.0
...\glue 0.0 plus 0.0001fil
..\glue(\baselineskip) 30.0
..\hbox(0.0+0.0)x345.0

杀死\vspace*{-\topskip}它。你会看到

...\glue(\topskip) 10.0
...\rule(0.0+0.0)x*
...\penalty 10000
...\glue -10.0

因为现在规则的高度没有被考虑进去。

相关内容