On this page:
$+
$-
$/
$*
$sqr
$cube
$sqrt
$expt
$integral
$sum
$product
$_
$^
$^_
$_  ^
$left-brace
$right-brace
$brace
$left-paren
$right-paren
$paren
$left-square-bracket
$right-square-bracket
$square-bracket
$left-ceiling
$right-ceiling
$ceiling
$left-floor
$right-floor
$floor
$overbrace
$underbrace
$formula
formula-dict
8.16

4 Text 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 formula-dict) โ†’ tblock?

  tree : any/c
  formula-dict : dict?
Formats the given racket quoted math expression tree as a tblock.

The default dictionary used by $formula.

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