How to recreate LaTeX \smash and \vphantom in Typst math?

In LaTeX, I often use \smash, \vphantom, etc. to define commands like these:

% add an underbrace and \text{#2} while keeping the orginal bounding box of #1
\newcommand{\notebelow}[2]{\vphantom{#1}\smash{\underbrace{#1}_{\text{#2}}}}
% matched scaling parentheses with a linebreak
\newcommand{\splitparen}[2]{\left(#1\vphantom{#2}\right.\\\left.\vphantom{#1}#2\right)}

These may be used in cases like these:

$$ f(a, b) = \int_{a}^{b} \left(a - \notebelow{\frac{a+b}{a-b}}{note} + b \right) dx $$


\begin{multline}
    \int_{a}^{b} \splitparen{ \text{some incredibly lengthy expression } \frac{a+b}{a-b}}
        {+ f(x) - \text{some other equally lengthy expression }} dx = F(a, b)
\end{multline}

However, when I try to put a bit of math in a #box, it starts jittering about and I don’t get the baseline well aligned. I expect think this mechanism could be used, but I don’t understand what goes on well enough to recreate my bits of LaTeX:

This package also shows it should be possible to put ‘tall’ math in a box while keeping it nicely aligned on the baseline:

However, the source code it too complicated for me to understand right now…

These looks related as well, but on closer inspection, these deal with single-line contents. That doesn’t make it very noticeable, but the vertical placement is however a very little bit off and will not do for my question.

1 Like

Oh, I found \vphantom here, but I did not manage to recreate the second equation in Typst yet…
https://www.reddit.com/r/typst/comments/1cvhfh9/vhphantom_of_latex_equivalent/#t1_l4pwxcf-post-rtjson-content

For your second equation do you need \vphantom to scale the parentheses correctly, or am I misunderstanding? If all you need is to scale the parentheses, typst does this automatically:

$
integral_a^b (
  &"some incredibly lengthy expression" (a + b)/(a - b) \
  &+ f(x) - "some other equally longthy expression"
) d x = F(a, b)
$

The first equation is a bit tricky because the underbrace itself takes up vertical space in typst. The best option is probably to scale the left/right parentheses explicitly instead of smashing the underbrace:

#let autolr(eq) = context {
  let wh = none
  {
    show math.underbrace: it => it.body
    wh = measure(eq)
  }
  math.lr(size: wh.height, $(eq)$)
}

$
f(a, b) = integral_a^b autolr(a - underbrace((a + b)/(a - b), "note") + b) d x
$

3 Likes

:exploding_head: I was trying to solve a problem that does not exist!
In LaTeX a line break cannot be part of a set of scaling parenthesis, but in Typst it can…

This is what I got now:

// maxheight has to be large enough to accommodate the depth below the baseline
#let math-smash(body, maxheight:2em, style:math.display) = box(height:0pt, box(stroke:red, height:maxheight, move(dy:-maxheight, [#box(width:0em, height:maxheight)$style(body)$])))

#let math-vphantom(body, style:math.display) = hide(context{
  let size = measure($style(body)$)
  body
  h(-size.width)
})

#let notebelow(body, note) = {
  $#math-vphantom(body)#math-smash($underbrace(body, note)$, maxheight:.7em)$}

$ f(a, b) = integral_a^b (a - #notebelow($(a + b) / (a - b)$, "note") + b) d x $

For completeness, the multline with equation number is a bit tricky in Typst, but I consider this ‘good enough’ for now:

#set math.equation(numbering: "(1)", number-align: right + bottom)   

$
integral_a^b (
  "some incredibly lengthy expression" (a + b)/(a - b) \
  & #h(-4cm) + f(x) - "some other equally longthy expression"
) d x = F(a, b)
$

image

1 Like