## `expr2text` -- convert objects into character strings

#### Introduction

`expr2text(`object`)` converts `object` into a character string.

#### Call(s)

`expr2text(object)`

#### Parameters

`object` | - | any MuPAD object

#### Returns

a `string`.

### Overloadable:

`object`

#### Related Functions

`coerce`, `fprint`, `int2text`, `tbl2text`, `text2expr`, `text2int`, `text2list`, `text2tbl`, `print`

#### Details

- `expr2text(`object`)` converts `object` into a character string. The result usually corresponds to the screen output of `object` when `PRETTYPRINT` is set to `FALSE`.

- If the function is called without arguments, then an empty character string is created. If more than one argument is given, the arguments are interpreted as an expression sequence and are converted into a single character string.

- Like most other MuPAD function, `expr2text` evaluates its arguments before the conversion.
- If strings occur in `object`, they will be quoted in the result.
- `expr2text` is a function of the system kernel.

#### Example 1

Expressions are converted into character strings:

    >> expr2text(a + b)

                                      "a + b"

`expr2text` quotes strings. Note that the quotation marks are preceded by a backslash when they are printed on the screen:

    >> expr2text(["text", 2])

                                  "[\"text\", 2]"

#### Example 2

If more than one argument is given, the arguments are treated as a single expression sequence:

    >> expr2text(a, b, c)

                                     "a, b, c"

If no argument is given, an empty string is generated:

    >> expr2text()

                                        ""

#### Example 3

`expr2text` evaluates its arguments:

    >> a := b: c := d: expr2text(a, c)

                                      "b, d"

Use `hold` to prevent evaluation:

    >> expr2text(hold(a, c));
       delete a, c:

                                      "a, c"

Here is another example:

    >> expr2text((a := b; c := d));
       delete a, c:

                                        "d"

    >> e := expr2text(hold((a := b; c := d)))

                               "(a := b; \nc := d)"

The last string contains a newline character '`\n`'. Use `print` with option `Unquoted` to expand this into a new line:

    >> print(Unquoted, e):

                                     (a := b;
                                     c := d)

#### Example 4

`expr2text` is overloadable. It uses a default output for elements of a domain if the domain has neither a `"print"` slot nor an `"`expr2text" slot:

    >> T := newDomain("T"): e := new(T, 1):
       e;
       print(e):
       expr2text(e)

                                     new(T, 1)

                                     new(T, 1)

                                    "new(T, 1)"

If a `"print"` slot exists, it will be called by `expr2text` to generate the output:

    >> T::print := proc(x) begin
         _concat("foo: ", expr2text(extop(x)))
       end_proc:
       e;
       print(e):
       expr2text(e)

                                      foo: 1

                                      foo: 1

                                     "foo: 1"

If you want `expr2text` to generate an output differing from the usual output generated by `print`, you can supply an `"`expr2text" method:

    >> T::expr2text := proc(x) begin
         _concat("bar: ", expr2text(extop(x)))
       end_proc:
       e;
       print(e):
       expr2text(e)

                                      foo: 1

                                      foo: 1

                                     "bar: 1"

#### Background

- When processing a domain element `e`, `expr2text` first tries to call the `"expr2text"` method of the corresponding domain `T`. If it exists, `T::expr2text(e)` is called and the result is returned. If no `"expr2text"` method exists, `expr2text` tries to call the `"print"` method in the same way. If no `"print"` method exists either, `expr2text` will generate a default output. Cf. example 4.

  An `"`expr2text" method or a `"print"` method may return an arbitrary MuPAD object, which will be processed recursively by `expr2text`.

  The returned object must not contain the domain element `e` as a sub-object. Otherwise, the MuPAD kernel runs into infinite recursion and emits an error message.

- For expressions, the result returned by `expr2text` always coincides with the output produced by `print`. If the 0th operand of the expression is a function environment, the result of `expr2text` is computed by the second operand of the function environment.

#### Changes

- Due to some output changes, `expr2text` sometimes produces a string that is formatted differently than in previous MuPAD versions. The new output formatting is prettier than before. E.g., `expr2text(`b-1/a`)` now returns `"b - 1/a"` instead of `"b + a^(-1)*(-1)"`.

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000