2

How can I achieve an alignment like this with TeX or LaTeX boxes?

The desired alignment.

Neither the width nor the height of the boxes is known beforehand. (It is known that the middle box is the highest, though, if that helps.)


As a starting point, consider the following MWE, that should eventually produce something like this:

The desired output.

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
2

You can fake it with \phantom and \llap by adding the before text to the first line:

\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

If you accept using expl3, you can also use their powerful coffin concept:

\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

enter image description here

1
\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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.