How to preserve spacing when inserting `dif x` through a variable?

My article has a section about Differential Forms. For convenience, I defined

#let dx = $dif x$

However when I do f dx the spacing is normal, but in f(x) dx^1 the d just sticks to the paren.

I guess this is due to when I do dx^1, it takes dx as an operator, no longer a sequence that has a spacing. If there is not a workaround I would stick to dif x.

But when doing dx wedge dy (or dif x wedge dif y), where wedge is just and, dy seems closer to the wedge than dx. When doing dx^1 wedge dy^2, the spacing is normal but, the spacing in the front disappears.

Is there currently a good and graceful way (to deal with the spacing) in Typst to solve this? If not, can I add a space as convenient as LaTeX’s \, or \;?

Examples you’ve mentioned

#let dx = $dif x$

$
  f dx \
  f dx^1 \
  f dif x^1 \
  dif x and dif y \
  dif x^1 and dif y^2
$

My answer

Sorry I don’t if there’s a better way to deal with grouping or magic spacing…

You can create a shorthand of mathematical spacing with quick-maths – Typst Universe.

#import "@preview/quick-maths:0.2.1": shorthands
#show: shorthands.with(
  ($,,$, $thin$),
)

$
  f dif x^1 \
  f thin dx^1 \
  f ,, dx^1 \
$

1 Like

Yes, there are predefined spacing constants like: thin, med, thick, quad and more. By default the definition of dif uses a thin space but in a weak version.


You could use the physica package which has one solution to this: a different way of calling and using differentials, like this:

#import "@preview/physica:0.9.5": dd

$ integral f(x) dd(x) $
$ integral f(x) dd(x^1, y^1) $
$ integral f(x) dd(x^1, y^1, p: and) $
$ integral f(x) dd(x, 3)  $

See the physica manual’s examples for dd for more.


I read a little of physica’s implementation, and it turns out this works, but I’m not sure why anymore. (They use op too).

#let dx = $op(dif x)$
$ integral f(x) dx^1 $

bild

1 Like

Thanks! This indeed works but, when you type a equation like

$ a dx + b dy $

The + sticks to the b, this is probably because dx being an op makes + read as a unitary operator, not a binary one.

Oh I see, yes that’s a small problem with physica’s dd that I didn’t know about

Thanks! But I wonder if there is a way to replace dx I input directly with dif x, just like you Find & Replace in the editor?

Testing the following cases, seen below, I think this is the best we can do so far. With a custom adjustment to attach to “move” the thin weak space from inside of the attach to before it.

#let thinweak = h(math.thin.amount, weak: true)
#let dx = $dif x$  // dif is the same as thinweak upright(d)
#let starts-with(body, item) = {
  if body.func() == math.equation { return starts-with(body.body, item) }
  body.func() == [].func() and body.children.at(0) == item
}
#show math.attach: it => {
  if starts-with(it.base, math.dif) {
    thinweak + it
  } else {
    it
  }
}

$f dx + g dx$\
$f dx^1 + g dx^1$\
$A dx and dx$\
$B dx^1 and dx^2$\
$1 + dx$\
$2(dx + 1)$\
$display(integral dx integral dx^1 integral f dx^1 and g dx^1)$\

We could ask why the space in dif and here is weak at all - the answer is the case with (dx) where we need the space in front to collapse, which it does if it is weak(?)