This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

Notebooks · lib/cas/show

Printing a CAS expression as Viv and as LaTeX

An expression in the store (lib/cas/store) is printed in two steps. First it becomes a syntax tree of the shape Viv's parser builds: a + b, a - b, a * b, a / b, -a, pow(a, b) (written a ^ b), calls, names and numbers. That step decides the presentation: the terms of a sum with the highest power first, a negative term as a subtraction, the factors with negative exponents under a fraction bar, as sqrt(u).

Then the tree is written two ways:

  • cas_text(e): Viv source with only the parentheses Viv's precedence needs. Reading it back with cas gives the same node.
  • cas_tex(e): LaTeX by the rules of the compiler's printer, lang/viv/src/tex.rs: \frac for a quotient, \left( \right) only where the operators need them, \, between factors and \cdot before a digit, , \ln, \sin, \sqrt. A Rust test (cas_tex_is_tex_rs_on_cas_text in lang/viv/tests/cas.rs) prints a corpus both ways and requires cas_tex(e) to equal tex.rs's LaTeX of cas_text(e). Two differences are deliberate: the constant pi is (tex.rs prints a two-letter name upright unless a glossary names it), and an integer of a million or more is written out in full, since it is exact (tex.rs writes a double that large as ).

Used with import cas.show.

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

Shift+Enter in a cell runs it and moves to the next. Running a cell runs every cell above it too, as one program.

[1]lines 30–402