How do you create delimited sequences with a missing right or left delimiter?

In LaTeX, this can be done by writing one delimiter as \left. or \right.. This notation is commonly used to express derivatives at a specific point:

\[\left. \frac{\mathrm{d}}{\mathrm{d}x} x^2 \right|_3 = 6\]

I have tried:

$ lr(dif / (dif x) x^2 |_3) &= 6 $

but this does not work.
The following does seem to work:

$ lr(dif / (dif x) x^2 |)_3 &= 6 $

but I am not sure if the spacing is correct and am uneasy about the document semantics now reflecting the subscript attaching to the expression as a whole, not the right delimiter.

Additionally, the above would not work if the expression had an unclosed left parenthesis, e.g.:

$ lr(\( sum^* |) $

will always size the parenthesis and the bar together, which is not necessarily desirable.

If there is no reliable way to do this currently, might I suggest the following change:

Make lr take optional named arguments left and right, which can be content, none, or auto. The default is auto, in which case the delimiter is inferred from the text. Otherwise the provided content is affixed and sized appropriately, unless none is used. Since math mode would not interpret none as a literal, perhaps a string can be passed instead, which is either interpreted as a delimiter, e.g. "(", "|", or "none", or "auto". Perhaps, in analogy to mat, a delim parameter could be provided, too, which would take either the left delimiter with an implicit mirrored right delimiter, or a tuple.

$ lr(x^2 - sum_(i = 0)^infinity (-1)^i x!|_1, left: "none") $

Upon further thought this seems to be two only somewhat interrelated issues.

  1. lr does not interpret a delimiter with an attachment as a delimiter.
    This is what causes lr(x|_y) to not work.
  2. lr has no way of ignoring a delimiter.
    This is what makes lr(\(x|) correct only some of the time.

the simplest solution I see is to do:

$
lr(dif / (dif x) x^2 |)_3 = 6
$

as lr doesn’t necessarily need to have both sides.
you just contain the block that you want the | to size in reference to.
Hope this helps!

This is indeed the correct way to do it. As for the document semantics, it is obviously more complex to read than without lr, but it is only used here to specify that | must scale with the rest of the previous content.

About the subscript, it is not only semantics, but it is actually attached to lr.

I think this is known, see Different parsing in LR longhand vs shorthand: `lr([..])` vs `[..]` · Issue #2979 · typst/typst · GitHub.

So, is there any good way to make Typst behave as closely as possible with LaTeX?

$ lr((partial f) / (partial t) |)_(t = 0) $

will be rendered as:

The corresponding LaTeX result is:

It can be seen that the position of Typst t = 0 is obviously lower, which is inconsistent with the usual standard.

I think the lower subscript is not to do with lr, but just where typst places subscripts. Even if we manually stretch the | it places it there:

$ (partial f) / (partial t) stretch(class("closing", |), size: #0.9cm)_(t = 0) $

I am unaware of a way to adjust these attachment points.


As to the semantic of attaching it to lr: I don’t think that that matters, as none of the output formats typsts support (PDF, PNG, SVG) have any such semantics. In the PDF, afaik it just creates the glyps/letterforms in specific locations on the page, but every association as to what is attached where is lost (even the distinction that something is a “subscript” as opposed to “regular math” or “regular text” is no longer present in the PDF) only implied to the sighted reader by placement and text size.

1 Like

The choice of font has an influence on the attachment points, so it might be defined in the OTF Math specification somewhere:

#show math.equation: set text(font: "New Computer Modern Math") // Default
$ lr((partial f) / (partial t) |)_(t = 0) $
#show math.equation: set text(font: "Libertinus Math")
$ lr((partial f) / (partial t) |)_(t = 0) $
#show math.equation: set text(font: "IBM Plex Math")
$ lr((partial f) / (partial t) |)_(t = 0) $
#show math.equation: set text(font: "Fira Math")
$ lr((partial f) / (partial t) |)_(t = 0) $

But this might also be an artifact of how the | is sized…

Thanks for the demo. However, since LaTeX’s math font is typically Computer Modern Math or New Computer Modern Math, I don’t think the font itself is the main issue. I suspect Typst might have some problems with how it handles delimiter attach. My understanding is that subscripts and superscripts for delimiters (especially stretched ones, like with \big) should generally align with the delimiter’s top and bottom boundaries, with only slight overflow. But I’m not familiar with the rendering principles of Typst or LaTeX, so this is just my speculation.

Anyway, thanks for the reply. For now, I’ve used place to simulate the LaTeX result, and I think Typst’s result is acceptable, though I’m still getting used to it.

$
  lr((partial f) / (partial t) |)_#place(
    bottom,
    float: true,
    scope: "parent",
    clearance: 0em,
    dy: -0.5em,
    [$script(t = 0)$],
  )
$

edit

I wrapped this implementation in a function, and I’m pretty happy with the result, but it’s a little cumbersome:

#let lspt(body) = context {
  let subs = [$#eval("script(" + body + ")", mode: "math")$]
  place(
    bottom,
    float: true,
    scope: "parent",
    clearance: 0em,
    dy: -measure(subs).height / 2,
    subs,
  )
}

$
  lr((partial f) / (partial t) |)_#lspt("t = 0") \
  lr((partial f) / (partial t) |)_#lspt("y = 12")
$

1 Like

LaTeX font handling is very bespoke (as it predates OTF Math) so it could also be that it places the attachments according to some internal algorithm independent of the font. (The built-in Computer Modern font is in the Metafont format, not OTF. What Typst uses is a “port”, imo one with some deficiencies)

(The same might be true for Typst, I dont actually know if OTF specifies math attachment points)

1 Like

There are many differences between these two fonts. I get the same result as Typst if I use New Computer Modern Math in LaTeX:

\documentclass{article}
\usepackage{fontsetup} // New Computer Modern Math
\begin{document}
\[ \left. \frac{\partial f}{\partial t} \right|_{t=0} \]
\end{document}

So it looks like it’s just a font thing indeed.

2 Likes

Thanks for checking. My bad, it seems this is indeed a font problem.

It seems that no one has mentioned it yet? Leedehai made a package for it two years ago.

#import "@preview/physica:0.9.5": dv, evaluated, pdv

$ evaluated(dv(, x) x^2)_3 = 6. $
$ evaluated(pdv(f, t))_(t=0). $

5 Likes