同时对齐盒子的顶部和底部

同时对齐盒子的顶部和底部

我怎样才能用 TeX 或 LaTeX 框实现这样的对齐?

所需的对齐。

事先不知道盒子的宽度和高度。(不过,如果有帮助的话,我们知道中间的盒子最高。)


作为起点,请考虑以下 MWE,它最终应产生类似这样的内容:

所需的输出。

before
\hbox{\vbox{% this gets me bottom alginment, \vtop gets me top alginment, I want "both"
    \hbox{the main text}
    \hbox{that has several lines}
    \hbox{of unknown width}
}}
after
\bye

答案1

您可以通过在第一行添加前面的文本来\phantom伪造它:\llap

\leavevmode\hbox{\phantom{before }\vbox{% this gets me bottom alginment, \vtop gets me top alginment, I want "both"
    \hbox{\llap{before }the main text}
    \hbox{that has several lines}
    \hbox{of unknown width}
}}
after
\bye

如果您接受使用expl3,您还可以使用其强大的棺材概念:

\input expl3-generic\relax
\newdimen\linewidth
\newdimen\columnwidth
\ExplSyntaxOn
\cs_new:Npn \myalign #1 #2 #3 {
  \group_begin:
  \hcoffin_set:Nn \l_tmpa_coffin {#1}
  \vcoffin_set:Nnn \l_tmpb_coffin {\vsize} {#2}
  % The *r*ight end of the before text should be *l*eft of the *t*op line:
  \coffin_join:NnnNnnnn
    \l_tmpb_coffin {l} {T}
    \l_tmpa_coffin {r} {H}
    {-3.33pt} {0pt}
  \hcoffin_set:Nn\l_tmpa_coffin{#3}
  % The *l*ight end of the after text should be *t*eft of the *b*ase line:
  \coffin_join:NnnNnnnn
    \l_tmpb_coffin{r}{B}
    \l_tmpa_coffin{l}{H}
    {3.33pt}{0pt}
  \coffin_typeset:Nnnnn
    \l_tmpb_coffin{l}{T}
    {0pt}{0pt}
  \group_end:
}
\ExplSyntaxOff
\myalign{before}{%
    \hbox{the main text}
    \hbox{that has several lines}
    \hbox{of unknown width}}%
  {after}
\bye

在此处输入图片描述

答案2

\documentclass{article}
\usepackage[usestackEOL]{stackengine}
\newcommand\threebox[3]{%
  \renewcommand\stacktype{L}%
  \strutlongstacks{T}%
  \savestack\middlebox{\Longstack[l]{#2}}%
  \raisebox{\dimexpr\ht\middleboxcontent-\ht\strutbox}{#1} \middlebox{} #3%
}
\begin{document}
\threebox{before}{the main\\text\\that has\\several lines\\of unknown\\ width }{after}
\quad\threebox{before}{with a g\\descender}{after g}\bigskip

\threebox{g before}{with a\\g descender}{after}
\end{document}

在此处输入图片描述

相关内容