![如何避免(xparse)环境框破坏行间距并进入前一个环境框的内部空间?](https://linux22.com/image/397030/%E5%A6%82%E4%BD%95%E9%81%BF%E5%85%8D%EF%BC%88xparse%EF%BC%89%E7%8E%AF%E5%A2%83%E6%A1%86%E7%A0%B4%E5%9D%8F%E8%A1%8C%E9%97%B4%E8%B7%9D%E5%B9%B6%E8%BF%9B%E5%85%A5%E5%89%8D%E4%B8%80%E4%B8%AA%E7%8E%AF%E5%A2%83%E6%A1%86%E7%9A%84%E5%86%85%E9%83%A8%E7%A9%BA%E9%97%B4%EF%BC%9F.png)
第一个环境最后一行的行间距被下一个环境的第一行侵犯。在这种情况下,我该如何保持正确的行间距?
\documentclass[varwidth]{standalone}
\usepackage{xparse}
\begin{document}
\NewDocumentEnvironment{myEnv}{+b}
{ \parbox{40mm}
{ #1
}
} {}
\begin{myEnv}
This is sentence 1. This is sentence 2.
This is sentence 3.
\end{myEnv}\par
% INTERLINE SPACING IS BAD HERE
\begin{myEnv}
This is sentence 1. This is sentence 2.
This is sentence 3.
\end{myEnv}
\end{document}
答案1
由于您使用垂直居中的 parbox,它们具有很大的高度和深度,所以 tex 无法维护它们的参考点之间的基线到基线空间,所以它垂直堆叠它们,并以\lineskip
(默认为 1pt)和分隔\parskip
。
如果添加,\strut
那么堆叠的框的基线将被分隔开,\baselineskip
但是仍然有额外的基线\lineskip
,如第二个示例所示。
如果您对输入有足够的控制权,并且您知道您正在设置这些框的序列,那么您可以抑制\lineskip
with(例如)nointerlineskip
并且只依靠 来\strut
间隔行,如最后一个例子所示。
\documentclass{article}
\showoutput
\showboxdepth=3
\begin{document}
\parbox{40mm}{This is sentence 1. This is sentence 2. This is sentence 3.}
\parbox{40mm}{This is sentence 1. This is sentence 2. This is sentence 3.}
\bigskip
\parbox{40mm}{\strut This is sentence 1. This is sentence 2. This is sentence 3.\strut }
\parbox{40mm}{\strut This is sentence 1. This is sentence 2. This is sentence 3.\strut }
\bigskip
\parbox{40mm}{\strut This is sentence 1. This is sentence 2. This is sentence 3.\strut }
\nointerlineskip
\parbox{40mm}{\strut This is sentence 1. This is sentence 2. This is sentence 3.\strut }
\end{document}
三种情况的终端输出为
...\hbox(17.97223+12.97221)x345.0, glue set 216.18898fil []
...\glue(\parskip) 0.0 plus 1.0
...\glue(\lineskip) 1.0
...\hbox(17.97223+12.97221)x345.0, glue set 216.18898fil []
...\hbox(20.5+15.5)x345.0, glue set 216.18898fil []
...\glue(\parskip) 0.0 plus 1.0
...\glue(\lineskip) 1.0
...\hbox(20.5+15.5)x345.0, glue set 216.18898fil []
...\hbox(20.5+15.5)x345.0, glue set 216.18898fil []
...\glue(\parskip) 0.0 plus 1.0
...\hbox(20.5+15.5)x345.0, glue set 216.18898fil []