Leedehai / typst-physics

physica: vectors, fields, differentials, derivatives, Dirac brakets, tensors, and more. See examples in the manual PDF.
https://github.com/Leedehai/typst-physics
MIT License
357 stars 11 forks source link

Add option for derivative at a certain point #28

Open tobiasdiez opened 6 months ago

tobiasdiez commented 6 months ago

One sometimes needs to specify the point / time at which one takes the derivative, using a vertical bar.

In typst, this can be approximated using

dv(,t)| _(t=0) f(t)

which kind of works but looks ugly since the bar is too short. It would be nice if one could use something like dv(,t, t=0) or dv(,t, at=(t=0)) which automatically yields a proper rending of the bar.

Looking at the code, typst seems to use the method stretch_vertical to increase the length of lines/bars.

YDX-2147483647 commented 6 months ago

Here's a workaround.

#import "@preview/physica:0.9.3": dv, eval
$ eval(dv(,t))_(t=0) f(t) $

And we can define a quick function:

#let dval(..args, value) = math.attach(eval(dv(..args)), br: value)
$ dval(,t, t=0) f(t) $

#let edv(..args) = eval(dv(..args))
$ edv(,t)_(t=0) f(t) $
Leedehai commented 6 months ago

Thanks, the above snippet is not merely a workaround, it's actually the intended usage.

I feel it's better not to couple different semantics together, but I welcome more inputs in this to convince me - no promise:)

tobiasdiez commented 6 months ago

Not sure if this is possible with typst, but could one declare | after dv to be a shortcut for eval (active character is latex)? The eval method works and the only minor thing I don't like about it is the additional brackets that make it a bit harder to read.

Btw, this example should perhaps also be added in the "derivative" section.

YDX-2147483647 commented 6 months ago

Typst v0.8.0 changelog:

Removed support for automatic matching of fences like | and || as there were too many false positives. You can use functions like abs or norm or an explicit lr call instead. (Breaking change)

Therefore I guess the activate character is impossible without changing the typst compiler.


Besides, I've updated https://github.com/Leedehai/typst-physics/issues/28#issuecomment-2087891080 with 2 example functions. Hope they satisfy your need.

tobiasdiez commented 6 months ago

Thanks!

As another data point, the https://typst.app/universe/package/diverential package has an eval argument for derivatives and the example reads quite nice

dv(f, x, deg: 2, eval: 0)

But I will not argue further if you think the eval method is already sufficient 🦊