很难找到两行内容之间的垂直距离

很难找到两行内容之间的垂直距离

我正在尝试确定框与紧接在后一段中的一行文本之间的垂直距离。根据顶部框的内容,我得到的差异为\lineskip

这是我不太简单的 MWE。我添加了很多彩色线条来帮助说明基线(蓝色)、支柱(橙色)、深度(紫色)和线条之间的距离(黑色)。

\documentclass{article}
\usepackage{xcolor}
%% set `\parskip` to non-trivial value
\setlength{\parskip}{2ex}
%% --- CREATING A STRUT ---
%% strut to equalize line heights of the two boxes designed to be variable
%% if `\aestrutht` is set to `0.7\baselineskip` there's a `\lineskip` discrepency
%% if `\aestrutht` is set to `0.8\baselineskip` there's no apparent discrpency
\newlength{\aestrutht}
\setlength{\aestrutht}{0.7\baselineskip}
\newlength{\aestrutdp}
\setlength{\aestrutdp}{0.3\baselineskip}
%% first rule is to prevent `\color` from screwing things up!
\newcommand{\aestrut}{%%'
  \rule{0pt}{0pt}%%'
  {%%'
    \color{orange}%%'
    \rule[-\aestrutdp]{2pt}{\dimexpr\aestrutdp+\aestrutht\relax}%%'
  }}
%% --- MAKING THE BASELINES VISIBLE ---
\newcommand{\aeshowbaseline}
  {%%'
    \bgroup
      \color{blue}%%'
      \raisebox{0pt}[0pt][0pt]{%%'
        \makebox[0pt][l]{%%'
          \rule{0.1pt}{\aestrutht}%%'
          \rule{2in}{0.1pt}%%'
        }%%'
      }%%'
    \egroup
  }
%% --- CREATING BOX FOR TOP MATTER ---
%% placed in a box so I can measure its height and depth
\newsavebox{\aebox}
\newcommand{\aeTopMatter}[1]{%%'
  \begin{lrbox}{\aebox}
    \begin{minipage}[t]{1in}
      \raggedright
      \aestrut
      \aeshowbaseline #1
      %%\par\rule{0pt}{0pt}
    \end{minipage}
  \end{lrbox}\usebox{\aebox}}%%'
%%---------------------------------------------------------------------------------
%% --- CREATING BOTTOM MATTER ---
\newcommand{\aeBottomMatter}{%%'
  \hspace*{0.95in}%%'
  \aestrut
  \aeshowbaseline
  \hspace*{1em}%%'
  More text}
%% --- MACRO TO SHOW DEPTH OF TOP BOX ---
\newcommand{\aeShowDepthOfTopMatter}{%%'
  \llap{\color{purple}%%'
    \rule[-\dp\aebox]{1in}{0.1pt}%%'
    \rule[-\dp\aebox]{0.1pt}{\dp\aebox}}}
%% --- MACRO TO SHOW DISTANCE BETWEEN TWO LINES ---
\newcommand{\aeShowDistanceBetweenLines}{%%'
  \raisebox{0pt}[0pt][0pt]{%%'
    \rule{2pt}{\dimexpr\parskip+\dp\aebox+\lineskip+\aestrutht}}}
\begin{document}

\aeTopMatter{Hello World}\aeShowDepthOfTopMatter

\aeBottomMatter\aeShowDistanceBetweenLines

\vspace{0.25cm}

\aeTopMatter{Hello My World}\aeShowDepthOfTopMatter

\aeBottomMatter\aeShowDistanceBetweenLines

\vspace{0.25cm}

\aeTopMatter{Hello World to this hardy world}\aeShowDepthOfTopMatter

\aeBottomMatter\aeShowDistanceBetweenLines

\end{document}

在此处输入图片描述

我知道两种可能的解决方法:

  1. 我可以把高度设置得\aestrutht大一点
  2. 我可以取消注释该行\par\rule{0pt}{0pt}

我不明白为什么这两种方法都有效。对于第一种情况,我遇到的困难在于,考虑到我的支柱的高度已经设置好了,它远远高于线上任何内容的高度。所以在我看来,我不需要再增加任何提升。

我想知道为什么这两个修复工作,以及为什么只有\lineskip第三种情况,而前两种情况却没有。

有一段时间我认为这可能与测量有关,\prevdepth例如使用

\par\xdef\tpd{\theprevdepth}

但这会与顶部盒子的深度混淆,而且通常看起来没什么用。

答案1

这与如何在使用 minipages (或 \parboxes) 时保持恒定的 baselineskip?answered by egreg

基本上,由于使用了minipage对齐t,在第三种情况下,盒子的深度和高度之和的值负责插入粘连\lineskip;在前两种情况下,总和的值不会强制插入粘连(有关所涉及计算的详细信息,请参阅埃格尔的精彩回答)。

让我们看看所涉及的箱子的高度和深度,然后让我们杀死\lineskip

\documentclass{article}
\usepackage{xcolor}


\newlength\BoxDp
\newlength\BoxHt
%% set `\parskip` to non-trivial value
\setlength{\parskip}{2ex}
%% --- CREATING A STRUT ---
%% strut to equalize line heights of the two boxes designed to be variable
%% if `\aestrutht` is set to `0.7\baselineskip` there's a `\lineskip` discrepency
%% if `\aestrutht` is set to `0.8\baselineskip` there's no apparent discrpency
\newlength{\aestrutht}
\setlength{\aestrutht}{0.7\baselineskip}
\newlength{\aestrutdp}
\setlength{\aestrutdp}{0.3\baselineskip}
%% first rule is to prevent `\color` from screwing things up!
\newcommand{\aestrut}{%%'
  \rule{0pt}{0pt}%%'
  {%%'
    \color{orange}%%'
    \rule[-\aestrutdp]{2pt}{\dimexpr\aestrutdp+\aestrutht\relax}%%'
  }}
%% --- MAKING THE BASELINES VISIBLE ---
\newcommand{\aeshowbaseline}
  {%%'
    \bgroup
      \color{blue}%%'
      \raisebox{0pt}[0pt][0pt]{%%'
        \makebox[0pt][l]{%%'
          \rule{0.1pt}{\aestrutht}%%'
          \rule{2in}{0.1pt}%%'
        }%%'
      }%%'
    \egroup
  }
%% --- CREATING BOX FOR TOP MATTER ---
%% placed in a box so I can measure its height and depth
\newsavebox{\aebox}
\newcommand{\aeTopMatter}[1]{%%'
  \begin{lrbox}{\aebox}
    \begin{minipage}[t]{1in}
      \raggedright
      \aestrut
      \aeshowbaseline #1
      %%\par\rule{0pt}{0pt}
    \end{minipage}
  \end{lrbox}\usebox{\aebox}%
\settoheight\BoxHt{\usebox{\aebox}}%
\settodepth\BoxDp{\usebox{\aebox}}%
Box ht: \the\BoxHt~%
Box dt: \the\BoxDp%
}%%'
%%---------------------------------------------------------------------------------
%% --- CREATING BOTTOM MATTER ---
\newcommand{\aeBottomMatter}{%%'
  \hspace*{0.95in}%%'
  \aestrut
  \aeshowbaseline
  \hspace*{1em}%%'
  More text}
%% --- MACRO TO SHOW DEPTH OF TOP BOX ---
\newcommand{\aeShowDepthOfTopMatter}{%%'
  \llap{\color{purple}%%'
    \rule[-\dp\aebox]{1in}{0.1pt}%%'
    \rule[-\dp\aebox]{0.1pt}{\dp\aebox}}}
%% --- MACRO TO SHOW DISTANCE BETWEEN TWO LINES ---
\newcommand{\aeShowDistanceBetweenLines}{%%'
  \raisebox{0pt}[0pt][0pt]{%%'
    \rule{2pt}{\dimexpr\parskip+\dp\aebox+\lineskip+\aestrutht}}}
\begin{document}

\offinterlineskip
\aeTopMatter{Hello World}\aeShowDepthOfTopMatter

\aeBottomMatter\aeShowDistanceBetweenLines

\vspace{0.25cm}

\aeTopMatter{Hello My World}\aeShowDepthOfTopMatter

\aeBottomMatter\aeShowDistanceBetweenLines

\vspace{0.25cm}

\aeTopMatter{Hello World to this hardy world}\aeShowDepthOfTopMatter

\aeBottomMatter\aeShowDistanceBetweenLines

\end{document}

在此处输入图片描述

在这个例子中,我使用\offinterlineskip全局方法来防止行间粘连,但是\nointerlineskip在适当的情况下,也可以使用它来防止行间粘连插入一次。

相关内容