8.2

### 4Text block: maths and formulas

 (require text-block/math) package: text-block

The functions exported by text-block/math are prefixed with \$ to avoid name collisions with Racket’s arithmetic functions. The default prefix can be changed on require with filtered-in, for example:

Examples:
 > (require racket/require (for-syntax racket/base racket/string) (filtered-in (lambda (name) (and (char=? #\\$ (string-ref name 0)) (string-replace name "\$" "¶"))) text-block/math))
> (¶+ 'a 'b)

(tblock #:width 5 #:height 1 #:baseline 0 '("a + b"))

 procedure(\$+ t ...) → tblock? t : tblock/any (\$- t ...) → tblock? t : tblock/any (\$/ t ...) → tblock? t : tblock/any (\$* t ...) → tblock? t : tblock/any (\$sqr t) → tblock? t : tblock/any (\$cube t) → tblock? t : tblock/any (\$sqrt t) → tblock? t : tblock/any (\$expt t1 t2) → tblock? t1 : tblock/any t2 : tblock/any (\$integral below above) → tblock? below : tblock/any above : tblock/any (\$sum below above) → tblock? below : tblock/any above : tblock/any (\$product below above) → tblock? below : tblock/any above : tblock/any
Compose tblocks horizontally for arithmetic operations.

The functions \$sqr, \$cube and \$expt may add parenthesis around their first argument to avoid ambiguity.

 procedure(\$_ t t_) → tblock? t : tblock/any t_ : tblock/any (\$^ t t^) → tblock? t : tblock/any t^ : tblock/any (\$^_ t t^ t_) → tblock? t : tblock/any t^ : tblock/any t_ : tblock/any (\$_^ t t^ t_) → tblock? t : tblock/any t^ : tblock/any t_ : tblock/any
Places a tblock as a subscript, as superscript, or as both, relative to t.

> (displayln (happend (\$_ "log" 'n) "(x)"))

log (x) n
 > (displayln (\$_^ (\$square-bracket (happend "3" (\$sqrt 'x))) "x=1" (\$sqr "n")))
n² ⎡ ▁▁⎤ ⎣3╲▏x⎦ x=1

 procedure(\$left-brace t ...) → tblock? t : tblock/any (\$right-brace t ...) → tblock? t : tblock/any (\$brace t ...) → tblock? t : tblock/any (\$left-paren t ...) → tblock? t : tblock/any (\$right-paren t ...) → tblock? t : tblock/any (\$paren t ...) → tblock? t : tblock/any (\$left-square-bracket t ...) → tblock? t : tblock/any (\$right-square-bracket t ...) → tblock? t : tblock/any (\$square-bracket t ...) → tblock? t : tblock/any (\$left-ceiling t ...) → tblock? t : tblock/any (\$right-ceiling t ...) → tblock? t : tblock/any (\$ceiling t ...) → tblock? t : tblock/any (\$left-floor t ...) → tblock? t : tblock/any (\$right-floor t ...) → tblock? t : tblock/any (\$floor t ...) → tblock? t : tblock/any
Each function places a bracket of some shape to the left or right of the ts, or encloses them with both brackets.

 > (displayln (\$ceiling (happend "3x + " (\$left-brace (lines->tblock '("a if x > 0" "b if x < 0" "c if x = 0") #:baseline 1)))))
⎡ ⎧a if x > 0⎤ ⎢3x + ⎨b if x < 0⎥ ⎢ ⎩c if x = 0⎥

 procedure(\$overbrace t t^) → tblock? t : tblock/any t^ : tblock/any (\$underbrace t t_) → tblock? t : tblock/any t_ : tblock/any
Places a brace and t^ or t_ above or below the tblock t. The baseline of the returned tblock is the same as that of t.

 > (displayln (happend (\$overbrace " Mon verre est vide. " "optimiste…") (\$underbrace " C'est un message. " "…ayant soif")))
optimiste… ╭─────────┴─────────╮ Mon verre est vide. C’est un message. ╰────────┬────────╯ …ayant soif

 procedure(\$formula tree) → tblock? tree : any/c
Formats the given racket quoted math expression tree as a tblock.

Examples:
 > (displayln (\$formula '(+ (sqrt (/ (log (/ (+ x 3) (- x 2))) (- (expt x y) z))) (f a b (/ c (+ a b))))))
▁▁▁▁▁▁▁▁▁▁▁ ▏ ⎛x + 3⎞ ▏log⎜─────⎟ ▏ ⎝x - 2⎠ ⎛ c ⎞ ▏────────── + f⎜a, b, ─────⎟ ▏ y ⎝ a + b⎠ ╲▏ x - z

 > (displayln (\$/ (happend "-b ± " (\$sqrt "b² - 4ac")) (\$* 2 'a)))
▁▁▁▁▁▁▁▁▁ -b ± ╲▏b² - 4ac ─────────────── 2 a

 > (displayln (happend @sigma " = " (\$sqrt (happend (\$/ 1 'N) " " (\$sum "i=1" 'N) (\$sqr (\$- (\$_ 'x 'i) @mu))))))
▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▏ N ▏ ▁▁▁ ▏1 ╲ σ = ▏─ ╱ ⎛x - μ⎞² ▏N ▔▔▔ ⎝ i ⎠ ╲▏ i=1