@@ -44,45 +44,6 @@ The input of `ce.box()` can be:
4444
4545The result is an instance of a ` BoxedExpression ` .
4646
47- ### String Representation
48-
49- The ` expr.toString() ` method returns a [ AsciiMath] ( https://asciimath.org/ ) string representation of the expression.
50-
51- ``` live
52- let expr = ce.parse("3x^2+\\sqrt{2}");
53- console.log(expr.toString());
54- ```
55-
56- When used in a context where a string is expected, the ` expr.toString() ` method
57- is called automatically.
58-
59- ``` live
60- let expr = ce.parse("3x^2+\\sqrt{2}");
61- console.log(expr);
62- ```
63-
64- ** To output an AsciiMath representation of the expressio to the console** use
65- ` expr.print() ` .
66-
67- ``` live
68- let expr = ce.parse("3x^2+\\sqrt{2}");
69- expr.print();
70- ```
71-
72- ** To obtain a LaTeX representation of the expression** use ` expr.latex ` or
73- ` expr.toLatex() ` for additional formatting options.
74-
75- ``` live
76- let expr = ce.parse("3x^2+\\sqrt{2}");
77- console.log(expr.latex);
78- ```
79-
80- ### Canonical Expressions
81-
82- By default, ` ce.box() ` returns a canonical expression. See
83- [ Canonical Expressions] ( #canonical ) for more info.
84-
85-
8647``` js
8748let expr = ce .box (1.729e3 );
8849console .log (expr .re );
@@ -113,9 +74,6 @@ console.log(expr.json);
11374// ➔ ["Add", 3, "x", "y"]
11475```
11576
116- By default, ` ce.parse() ` returns a canonical expression. See
117- [ Canonical Expressions] ( #canonical-expressions ) for more info.
118-
11977** To get a Boxed Expression representing the content of a mathfield**
12078use the ` mf.expression ` property:
12179
@@ -127,42 +85,6 @@ console.log(expr.evaluate());
12785// ➔ 2
12886```
12987
130- ## Unboxing
131-
132- ** To access the MathJSON expression of a boxed expression as plain JSON** , use
133- the ` expr.json ` property. This property is an "unboxed" version of the
134- expression.
135-
136- ``` js
137- const expr = ce .box ([" Add" , 3 , " x" ]);
138- console .log (expr .json );
139- // ➔ ["Add", 3, "x"]
140- ```
141-
142- ** To customize the format of the MathJSON expression returned by ` expr.json ` **
143- use the ` ce.toMathJson() ` method.
144-
145- Use this option to control:
146-
147- - which metadata, if any, should be included
148- - whether to use shorthand notation
149- - to exclude some functions.
150-
151- See [ JsonSerializationOptions] ( /compute-engine/api#jsonserializationoptions )
152- for more info about the formatting options available.
153-
154- ``` live
155- const expr = ce.parse("2 + \\frac{q}{p}");
156- console.log("expr.json:", expr.json);
157-
158- console.log("expr.toMathJson():", expr.toMathJson({
159- exclude: ["Divide"], // Don't use `Divide` functions,
160- // use `Multiply`/`Power` instead
161- shorthands: [], // Don't use any shorthands
162- }));
163- ```
164-
165-
16688## Canonical Expressions
16789
16890The ** canonical form** of an expression is a conventional way of writing an
@@ -216,15 +138,20 @@ By default, `ce.box()` and `ce.parse()` produce a canonical expression.
216138** To get a non-canonical expression instead** , use
217139` ce.box(expr, {canonical: false}) ` or ` ce.parse(latex, {canonical: false}) ` .
218140
219- The non-canonical form sticks closer to the original LaTeX input.
141+ When using ` ce.parse() ` , the non-canonical form sticks closer to the original
142+ LaTeX input. When using ` ce.box() ` , the non-canonical form matches the
143+ input MathJSON.
220144
221145``` js
222- const expr = " \\ frac{30}{-50}" ;
146+ const latex = " \\ frac{30}{-50}" ;
223147
224- ce .parse (expr );
148+ ce .parse (latex );
225149// canonical form ➔ ["Rational", -3, 5]
226150
227- ce .parse (expr, { canonical: false });
151+ ce .parse (latex, { canonical: false });
152+ // non-canonical form ➔ ["Divide", 30, -50]
153+
154+ ce .box ([" Divide" , 30 , - 50 ], { canonical: false });
228155// non-canonical form ➔ ["Divide", 30, -50]
229156```
230157
@@ -249,6 +176,85 @@ only possible syntax errors, but also semantic errors (incorrect number or
249176type of arguments, etc...).
250177
251178
179+
180+
181+ ## String Representation
182+
183+ The ` expr.toString() ` method returns a [ AsciiMath] ( https://asciimath.org/ ) string representation of the expression.
184+
185+ ``` live
186+ let expr = ce.parse("3x^2+\\sqrt{2}");
187+ console.log(expr.toString());
188+ ```
189+
190+ When used in a context where a string is expected, the ` expr.toString() ` method
191+ is called automatically.
192+
193+ ``` live
194+ let expr = ce.parse("3x^2+\\sqrt{2}");
195+ console.log(expr);
196+ ```
197+
198+ ** To output an AsciiMath representation of the expression to the console** use
199+ ` expr.print() ` .
200+
201+ ``` live
202+ let expr = ce.parse("\\frac{1+\\sqrt{5}}{2}");
203+ expr.print();
204+ ```
205+
206+ ** To obtain a LaTeX representation of the expression** use ` expr.latex ` or
207+ ` expr.toLatex() ` for additional formatting options.
208+
209+ ``` live
210+ let expr = ce.parse("3x^2+\\sqrt{2}");
211+ console.log(expr.latex);
212+ ```
213+
214+
215+
216+
217+
218+
219+
220+ ## Unboxing
221+
222+ ** To access the MathJSON expression of a boxed expression as plain JSON** , use
223+ the ` expr.json ` property. This property is an "unboxed" version of the
224+ expression.
225+
226+ ``` js
227+ const expr = ce .box ([" Add" , 3 , " x" ]);
228+ console .log (expr .json );
229+ // ➔ ["Add", 3, "x"]
230+ ```
231+
232+ ** To customize the format of the MathJSON expression returned by ` expr.json ` **
233+ use the ` ce.toMathJson() ` method.
234+
235+ Use this option to control:
236+
237+ - which metadata, if any, should be included
238+ - whether to use shorthand notation
239+ - to exclude some functions.
240+
241+ See [ JsonSerializationOptions] ( /compute-engine/api#jsonserializationoptions )
242+ for more info about the formatting options available.
243+
244+ ``` live
245+ const expr = ce.parse("2 + \\frac{q}{p}");
246+ console.log("expr.json:", expr.json);
247+
248+ console.log("expr.toMathJson():", expr.toMathJson({
249+ exclude: ["Divide"], // Don't use `Divide` functions,
250+ // use `Multiply`/`Power` instead
251+ shorthands: [], // Don't use any shorthands
252+ }));
253+ ```
254+
255+
256+
257+
252258## Mutability
253259
254260Unless otherwise specified, expressions are immutable.
0 commit comments