两行连续之间的空白来自哪里?

两行连续之间的空白来自哪里?

我正在尝试获取一个“联系表”,其中大小相同的框彼此相邻排列,没有任何空白。由于我的目标是在生成的文档中使用它,并且项目数量事先是未知的,因此我想避免使用类似表格的方法,而要依靠本机的换行和分页机制。这也是一个了解 tex/latex 内部结构的实验,因此在此过程中,我还使用了很多关键字,例如坏处紧急拉伸马虎等等。但最终,我的追求可以用下面的代码来概括。

我下面的第一次尝试显然会在框之间产生一些“单词间”空白,因此这是不可取的。

在第二次尝试(第 2 页)时,我明确删除了这个空格,但这也删除了换行符。我真的不明白发生了什么。

在第三次尝试(第 3 页)时,我明确告诉 (la)tex 可以换行,并且我得到了我想要的水平空白结果。但是,行与行之间仍然有一些垂直空白,并且这些空白占据了空间。结果,蓝色框最终出现在第 4 页,而我希望它出现在第 3 页。

这是我的问题:如何消除两行连续文本之间的小垂直空白?

\documentclass[a4paper]{article}

\usepackage[
text={15cm,21cm},
showframe,
]
{geometry}

\usepackage{xcolor}

\setlength{\parindent}{0pt}

\begin{document}


% page 1
\textcolor{red}{\rule{5cm}{7cm}}
\textcolor{green}{\rule{5cm}{7cm}}
\textcolor{red}{\rule{5cm}{7cm}}
\textcolor{green}{\rule{5cm}{7cm}}
\textcolor{red}{\rule{5cm}{7cm}}
\textcolor{green}{\rule{5cm}{7cm}}

\newpage

% page 2

\textcolor{green}{\rule{5cm}{7cm}}%
\textcolor{red}{\rule{5cm}{7cm}}%
\textcolor{green}{\rule{5cm}{7cm}}%
\textcolor{red}{\rule{5cm}{7cm}}%

\newpage

% page 3

\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{blue}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%

\end{document}

答案1

示例 1

有空格:行尾就像空格,除非连续有两个空格(实际上这是一个善意的谎言,但对于这种情况来说已经足够了)。

示例 2

没有断点,所以 TeX 无法分割框。

示例 3

\linebreak[0]相当于\penalty0,这引入了一个可行的断点。

示例 4

每行的高度都高于基线跳过允许的空间。粗略地说,如果一行的深度加上下一行的高度加上公差(称为\lineskiplimit)超过\baselineskip,则 TeX 会\lineskip在这些行之间插入粘连。默认值为 1pt。

以下是避免占用额外空间的一种方法:

\begingroup\lineskip=0pt
\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{blue}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}\par
\endgroup

\offinterlineskip是一种不同的方法:它完全抑制了行间粘连。在这种特定情况下,它们是等效的。

答案2

您可以使用\offinterlineskip它来防止线间胶。

另外,您可以使用%来删除虚假的空格(回车符将被视为空格):

\documentclass[a4paper]{article}

\usepackage[
text={15cm,21cm},
showframe,
]
{geometry}

\usepackage{xcolor}

\setlength{\parindent}{0pt}

\begin{document}


\offinterlineskip
% page 1
\textcolor{red}{\rule{5cm}{7cm}}%
\textcolor{green}{\rule{5cm}{7cm}}%
\textcolor{red}{\rule{5cm}{7cm}}
\textcolor{green}{\rule{5cm}{7cm}}%
\textcolor{red}{\rule{5cm}{7cm}}%
\textcolor{green}{\rule{5cm}{7cm}}

\newpage

% page 2

\textcolor{green}{\rule{5cm}{7cm}}%
\textcolor{red}{\rule{5cm}{7cm}}%
\textcolor{green}{\rule{5cm}{7cm}}
\textcolor{red}{\rule{5cm}{7cm}}%

\newpage

% page 3

\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{blue}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%

\end{document}

另一个“不太激烈”的抑制行间粘连的选项是使用\nointerlineskip

\documentclass[a4paper]{article}

\usepackage[
text={15cm,21cm},
showframe,
]
{geometry}

\usepackage{xcolor}

\setlength{\parindent}{0pt}

\begin{document}


% page 1
\textcolor{red}{\rule{5cm}{7cm}}%
\textcolor{green}{\rule{5cm}{7cm}}%
\textcolor{red}{\rule{5cm}{7cm}}\par\nointerlineskip 
\textcolor{green}{\rule{5cm}{7cm}}%
\textcolor{red}{\rule{5cm}{7cm}}%
\textcolor{green}{\rule{5cm}{7cm}}

\newpage

% page 2

\textcolor{green}{\rule{5cm}{7cm}}%
\textcolor{red}{\rule{5cm}{7cm}}%
\textcolor{green}{\rule{5cm}{7cm}}\par\nointerlineskip 
\textcolor{red}{\rule{5cm}{7cm}}%

\newpage

% page 3

\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}\par\nointerlineskip %
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\par\nointerlineskip %
\textcolor{blue}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{red}{\rule{5cm}{7cm}}\linebreak[0]%
\textcolor{green}{\rule{5cm}{7cm}}%

\end{document}

相关内容