Skip to content

Commit

Permalink
Touch up the transpose show rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Leedehai committed Dec 21, 2023
1 parent aaf4d55 commit f8a8b92
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ This [Typst](https://typst.app) package provides handy typesetting utilities for
natural sciences, including:
* Braces,
* Vectors and vector fields,
* Matrices, including Jacobian and Hessian, interpreting `..^T` as transpose,
* Matrices, including Jacobian and Hessian, smartly interpreting `..^T` as transpose,
* Dirac braket notations,
* Common math functions,
* Differentials and derivatives, including partial derivatives of mixed orders with automatic order summation,
Expand Down
Binary file modified physica-manual.pdf
Binary file not shown.
47 changes: 36 additions & 11 deletions physica-manual.typ
Original file line number Diff line number Diff line change
Expand Up @@ -785,36 +785,61 @@ In the default font, the Typst built-in symbol `planck.reduce` $planck.reduce$ l
[$ i hbar pdv(,t) psi = -frac(hbar^2, 2m) laplacian psi $],
)

=== Matrix transpose <matrix-tranpose>
=== Matrix transpose with superscript T <matrix-tranpose>

#v(1em)

Matrix transposition can be simply written as `..^T`, where the `T` will be
formatted properly to represent transposition instead of a normal letter $T$.
This conversion is disabled if the base is integral symbol.
Matrix transposition can be simply written as `..^T`, just like handwriting,
and the only superscript `T` will be formatted properly to represent
transposition instead of a normal capital letter $T$.

To enable this feature, users need to first import this and call
This conversion is disabled if the base is either
- a `limits(...)` or `scripts(...)` element, or
- an integration symbol $integral$ or vertical bar $|$, or
- an equation or `lr(...)` element whose last child is one of the above.

If you really want to:
- print a transpose explicitly: use symbol `TT`: `A^TT` $=> A^TT$;
- print a superscript letter $T$: use `scripts(T)`: `2^scripts(T)` $=> 2^scripts(T)$.

This feature needs to be enabled explicitly through a _show rule_.
```typ
#import "...(this physica package)...": super-T-as-transpose
#show: super-T-as-transpose
(A B)^T = B^T A^T
```

If you only want to enable it within a content block's scope, you may do
```typ
#[
#show: super-T-as-transpose // Enabled from here till the end of block.
(A B)^T = B^T A^T
]
```

#align(center, [*Examples*])

#show: super-T-as-transpose // Necessary!

#grid(
columns: (50%, 50%),
columns: (auto, auto),
row-gutter: 1em,
column-gutter: 2em,

[
*(1)* #hl(`(A_n B_n)^T = B_n^T A_n^T`) \
$ (A_n B_n)^T = B_n^T A_n^T $
*(1)* #hl(`(U V_n W')^T = W'^T V_n^T U^T`) \
$ (U V_n W')^T = W'^T V_n^T U^T $
],
[
*(2)* #hl(`vec(a, b)^T, mat(a, b; c, d)^T`) \
$ vec(a, b)^T, mat(a, b; c, d)^T $
],
[
*(3)* #hl(`limits(sum)^T, scripts(e)^T`) \
$ limits(sum)^T, scripts(e)^T $
],
[
*(2)* #hl(`integral_0^T A^T f(x) dif x`) \
$ integral_0^T A^T f(x) dif x $
*(4)* #hl(`integral_0^T, abs(a)^T, eval(F(t))^T_0`) \
$ integral_0^T, abs(a)^T, eval(F(t))^T_0 $
],
)

Expand Down
28 changes: 26 additions & 2 deletions physica.typ
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,11 @@

#let vecrow(..content) = $lr(( #content.pos().join([,]) ))$

// Prefer using super-T-as-transpose()
// Prefer using super-T-as-transpose() found below.
//
// Note Unicode U+1D40 (#str.from-unicode(7488)) is kinda ugly, and that
// glyph is in the superscript position already so users could not write
// the habitual "A^TT".
#let TT = $sans(upright(T))$

#let __vector(a, accent, be_bold) = {
Expand Down Expand Up @@ -635,9 +639,29 @@
// Credit: Enivex in https://github.com/typst/typst/issues/355 was very helpful.
#let hbar = (sym.wj, move(dy: -0.08em, strike(offset: -0.55em, extent: -0.05em, sym.planck)), sym.wj).join()

// A show rule, should be called like:
// #show: super-T-as-transpose
// (A B)^T = B^T A^T
// or in scope:
// #[
// #show: super-T-as-transpose
// (A B)^T = B^T A^T
// ]
#let super-T-as-transpose(document) = {
show math.attach: elem => {
if elem.base != [∫] and elem.at("t", default: none) == [T] {
let __eligible(e) = {
if e.func() == math.limits or e.func() == math.scripts { return false }
if e.func() == math.lr {
let last = e.at("body").at("children").at(-1)
return __eligible(last)
}
if e.func() == math.equation {
return __eligible(e.at("body"))
}
(e != [∫]) and (e != [|]) and (e != sym.bar.v.double)
}

if __eligible(elem.base) and elem.at("t", default: none) == [T] {
$attach(elem.base, t: TT, b: elem.at("b", default: #none))$
} else {
elem
Expand Down

0 comments on commit f8a8b92

Please sign in to comment.