How to add a negative space between symbols in math mode?

Hi,
I would like to find an equivalent to \! used in LaTeX for negative spaces in math mode, as describe in the answer to this post.
Thanks.

Hello @DreamerPhi, the horizontal spacing works in equations too!

Hence

#show math.equation: it => {
  show "!": h(-2pt)
  it
}
${{ A, B, C }}$

${!{ A, B, C }!}$

${!!{ A, B, C }!!}$

image

Above is the Typst output, you can compare it to the LaTeX output below
image

1 Like

Thank you !
But is there a way to have ! still printing an exclamation mark, and maybe \! used for the horizontal spacing (or the opposite)?

In theory you could create a function like this: #let ! = h(-2pt) and anywhere you want a negative space you would write #!(). However, this doesn’t work.
See this forum post by xkevio for the variable naming rules in Typst.

You can use @quachpas’ solution and replace the ! in the show rule with any single character that you know won’t be found in any of your equations:

#show math.equation: it => {
  show "ü": h(-2pt)
  it
}
${{ A!, B, C }}$\
${ü{ A!, B, C }ü}$\
${ü ü{ A!, B, C }ü ü}$
Output

image

Or you could use a function.

Using a Function

It’s not as clean but you can define a function with a short name (here ns for negative space):

#let ns = h.with(-2pt)
${#ns()#ns(){ A!, B!, C! }#ns()#ns()}$

The .with() basically asks the compiler "please create a function that calls h() with these given arguments. This new function is stored with the name ns.

2 Likes

Instead of a function you can just use a variable:

#let ns = h(-2pt)
${#ns#ns{ A!, B!, C! }#ns#ns}$

Edit: you can also leave out the # symbols:

#let ns = h(-2pt)
${ns ns { A!, B!, C! } ns ns}$
3 Likes

I’m not saying I’d recommend this, but the following seems to work, using \\! as the negative space:

#show math.equation: it => {
  let target = [\\!]
  show target.func().where(children: target.children): h(-2pt)
  it
}
${{ A!, B, C }}$

${\\!{ A!, B, C }\\!}$

${\\!\\!{ A!, B, C }\\!\\!}$

\! on its own won’t work because the backslash is special in Typst in a completely different way from LaTeX, but \\ results in a regular character which with some work you can make a show rule for.

Luckily, Typst seems to parse anything suffixed by ! as a separate sequence (in math), so we can replace that whole sequence with what we want.

There may be edge cases where the sequence would not be detected, but I was not able to construct one. This manually constructed sequence would break it:

#let target = [\\!]
#let strange = target.func()((..target.children, [a]))

${\\!{ A!, B, C }strange}$

The output contains an unreplaced [\\!].

2 Likes