How not to get lost in nested brackets?

Hi, I am using typst for some time now, with tinymist on vscode. I use it to pretty much everything physics related, reports, exercises and notes.

TLDR: Is there VSCode extension, or anyother way to deal with nested brackets in nice clean way, that does formating and colouring automatically?

On lectures I barerly am able to keep with the tempo to note everything down. When I have to deal with nested brackets with additional functions it gets really ugly pretty fast, and I don’t have time to check every bracket. My solution is to spam empty line as often as possible, but it is not perfect. Bracket colouring doesn’t work great too (im using VSCodium, I tried to configure, but sometimes it works, sometimes it doesn’t). So the question to all of you, how do you deal with nested brackets? Are there extensions or good practices that you found very useful?

For example:

iprod(f,g) = iprod((hat(A) - expval(A))psi, (hat(B)-expval(hat(B)))psi) = expval(hat(A)B - hat(A)expval(B) - expval(hat(A))hat(B)+expval(hat(A))expval(hat(B))) = \
  = expval(hat(A)hat(B)) - expval(hat(A))expval(hat(B)) 

Hello @typical_typst_enjoye,

Welcome to the forum!

Have you tried using the Tinymist extension?

Edit: Have you tried formatting code with:

  • Windows Alt + Shift + F
  • Mac Shift + Option + F
  • Linux Ctrl + Shift + I
1 Like

The code you posted¹ looks to be Typst in math mode², so maybe this won’t be useful to you but…

I find Typst code much more readable when things are placed on new lines. Basically if any function call is using more than 1 or 2 parameters then I break them out onto their own lines:

#table(columns: 3, table.header[A][B][C], align: center, range(3).map( v=> table.cell(v)))

versus

#table(
  columns: 3,
  table.header[A][B][C],
  align: center,
  range(3).map( 
    v=> table.cell(v)
  )
)

Indentation is critical here too.


Re-reading the question makes me realize my answer misses the “automatic” part of your question. Tinymist can auto-format code though it doesn’t by default put things on new lines.


  1. This gives an idea of the problem you are facing but it does not compile when copy/pasted into a blank file. In this case it probably isn’t necessary, but in general it lowers the barrier for trying to answer questions. See https://www.sscce.org/ for best practices
  2. If it is math mode please edit your post and wrap it in ```typm ``` so that the formatting is correct. See How to post in the Questions category

Beyond whitespace, one useful thing to avoid clutter is defining useful macros / helper variables. For example, to avoid writing hat(A) you could define a macro hA that evaluates to the same thing. We can then re-write the example as follows:

#let hA = $hat(A)$
#let hB = $hat(B)$

$
iprod(f,g) 
&= iprod(
  (hA - expval(A ))psi,
  (hB - expval(hB))psi
) \
&= expval(
  hA B - hA expval(B) - expval(A)hB + expval(hA)expval(hB)
) \
&= expval(hA hB) - expval(hA)expval(hB)
$
1 Like

@vmartel08
I am using tinymist, but after you mentioned I looked deeper in to settings and found typstyle formatter. In my case it doesn’t work automatically. But it does work with the keybind. It is actually a solution to my other issue related to formating that was very annoying so thank you for that.

@aarnent
This macro is great I love it. I used shortcuts that I was setting on the fly, but this is way better - faster and no brackets. Thank you!

@ gezepi (as a new user I can mention only two people)
Thank you for the tip and links.

#import "@preview/physica:0.9.8": *
$
  iprod(f, g) = iprod((hat(A) - expval(A))psi, (hat(B)-expval(hat(B)))psi) = expval(hat(A)B - hat(A)expval(B) - expval(hat(A))hat(B)+expval(hat(A))expval(hat(B))) = \
  = expval(hat(A)hat(B)) - expval(hat(A))expval(hat(B))
$

This code snippet should work.

As for the bracket issue per se: the best solution so far would be macros. It’s not super perfect when there are different variables. Same as my keybind solution though. Sometimes in equations like this one:

#import "@preview/physica:0.9.8": *
$
  Gamma (E, B) = vec(N, S_+) = N!/(S_+ ! (N - S_+)!) = N!/(((N - E/(N B))/2)! ((N + E/(N B))/2)!) 
$

It’s very easy to make mistake, and then entire math behind it looks completly different. Debuging this on the fly is not fast. I could upload a screenshot from vscodium right now, where it shows all the brakckets in the same colour. Only bracket from vec() is in yellow.

I actually expereience the same behavior on vscode, I guess it’s intended behavior. The only colored brackets seem to be those in function calls

I find that in general, putting adding a linebreak before every “=” helps a lot to keep equations clean, and copying the previous equation to change it is also easier as you can just press ctrl c + v. Making the changes is also easier, as the reference is right there.
Similarly, if there are things in the equation that are very similar it is also helpful to put those on their own lines, to make it easy to spot how they differ.
Lastly, to state the obvious I would also try to avoid nesting whenever possible, like by taking common factors out whenever possible (this makes the output cleaner too, imo), inventing notation to keep things clean, and using macros whenever it makes sense.

For example, I would write your equation as

$
  Gamma (E, B) 
  = vec(N, S_+) // Should this be binom instead of vec?
  = N! / (S_+ ! (N - S_+)!) 
  = N! / (
    (N/2 - E/(2 N B))! 
    (N/2 + E/(2 N B))!
  ) 
$

And while it doesn’t really make sense for such a short equation, if you know you’ll use those factorial terms a lot you could again use a macro (with the additional benefit that syntax highlighting the brackets works, lol)

$
  #let factterm(l, r, opt) = $(#l #opt #r)!$
  Gamma (E, B) 
  = vec(N, S_+) 
  = N! / (S_+ ! (N - S_+)!) 
  = N!/(
    factterm(N/2, E/(2 N B), +)
    factterm(N/2, E/(2 N B), -)
  ) 
$

You can move the macro inside or outside the math block depending on if you want it scoped throught the whole document, hardcode some parameters, etc…

Of course it is not always possible to make everything super modular and “nice”, at least in a reasonable timeframe. I don’t really have advice for this, beyond the fact that you should just write your equation and move on. Typst isn’t always a magic bullet, but then again no typesetting engine is

1 Like