How do I define my own delimiters to e.g. enable automatic rendering of dirac bra - ket notation?

The physica package provides support for Dirac bra-ket notation for states, inner products etc. in e.g. quantum mechanics.
Would it be possible to define my own functionality such that, when I write:

$
|a>, <b|, <a | b>, <n | H | m>, < H >
$

Typst would translate it to:

$
bra(a), ket(b), braket(a, b), matrixelement(n, H, m), expval(H)
$

?

I could imagine that it’s possible to do this. if you look at repr($ ... $) you can see that you have all the information that you’d need available – but the code to do it likely wouldn’t be pretty. Here’s an example of a smaller syntactical feature that I did:

… and the transformation necessary for bra-ket syntax would be more complex.

I can’t implement this right now, but maybe either you’re inspired by that other post, or someone else is motivated :slight_smile:

1 Like

I think you should think of other alternatives. Typst doesn’t natively support this well, but exposes equations in a form where you can transform them a little bit.

In your case, do you want to keep using </> for less than/greater than too? The ambiguity vs other operators is a problem for this syntax.


Here is an alternative for consideration or as starting point.

This one says: We sacrifice the >> and << operators and just overwrite them with the braket angles, to maybe make a custom syntax that we like.

#show sym.lt.double: math.angle.l   // should be scoped to math.equation
#show sym.gt.double: math.angle.r
$<<a|b>> <= <<a|X|b>>$

bild

That’s one way to think about custom syntax - in this case it’s just a custom shorthand - just like bra as a function is another good shorthand. And I just replaced a symbol, so I don’t have to think about how to try to match pairs of operators.

3 Likes

Thanks for the suggestion. I think overwriting existing symbols is a good suggestion e.g. << or ≤ (alt + <).

#show symbol("≤"): math.angle.l
#show symbol("≥"): math.angle.r

As long, that the body of the bra/ket/braket is not too high, it layouts nicely.

And you are right - I did think about that the functionality requested in my original question is probably ill-advised, since this would interfere with e.g. greater than/less than or just vertical bars in isolation.

sym.lt.eq

sym.lt.eq.slant

lr works as usual also with “mismatched” delimiters, like this, to adapt the height of the brackets:

$ lr(angle.l (diff f) / (diff x)|) + angle.l x| $

bild

physica’s braket functions are probably including something like that by default.