mathml

Generate MathML from symbolic expression

Syntax

chr = mathml(f)
chr = mathml(f,Name,Value)

Description

example

chr = mathml(f) returns the generated MathML from the symbolic expression f.

example

chr = mathml(f,Name,Value) uses additional options specified by one or more name-value pair arguments. For example, generate MathML for inline display by specifying DisplayInline as true.

Examples

collapse all

Generate MathML from a symbolic expression.

syms x
f = 1/exp(x^2);
chr = mathml(f)
chr =
    '<math xmlns='http://www.w3.org/1998/Math/MathML' display='block'>
       <msup>
         <mo>&ee;</mo>
         <mrow>
           <mo>-</mo>
           <msup>
             <mi>x</mi>
             <mn>2</mn>
           </msup>
         </mrow>
       </msup>
     </math>
     '

Generate MathML for inline display by specifying DisplayInline as true.

syms x
f = 1/exp(x^2);
chr = mathml(f,'DisplayInline',true)
chr =
    '<math xmlns='http://www.w3.org/1998/Math/MathML'>
       <msup>
         <mo>&ee;</mo>
         <mrow>
           <mo>-</mo>
           <msup>
             <mi>x</mi>
             <mn>2</mn>
           </msup>
         </mrow>
       </msup>
     </math>
     '

Use MathML tooltips for units and some special functions to provide more information. Generate tooltips by specifying Tooltips as true.

syms nu x
f = besselj(nu,x);
chr = mathml(f,'Tooltips',true)
chr =
    '<math xmlns='http://www.w3.org/1998/Math/MathML' display='block'>
       <mrow>
         <msub>
           <maction actiontype='tooltip'>
             <mo>J</mo>
             <mtext>besselj</mtext>
           </maction>
           <mi>&nu;</mi>
         </msub>
         <mrow>
           <mo form='prefix'>(</mo>
           <mi>x</mi>
           <mo form='postfix'>)</mo>
         </mrow>
       </mrow>
     </math>
     '

When you use MathML in a web page, then pausing on J displays a tooltip containing besselj.

Modify generated MathML by setting symbolic preferences using the sympref function.

Generate the MathML form of the expression π with the default symbolic preference.

sympref('default');
chr = mathml(sym('pi'))
chr =
    '<math xmlns='http://www.w3.org/1998/Math/MathML' display='block'>
       <mi>&pi;</mi>
     </math>
     '

Set the 'FloatingPointOutput' preference to true to return symbolic output in floating-point format. Generate the MathML form of π in floating-point format.

sympref('FloatingPointOutput',true);
chr = mathml(sym('pi'))
chr =
    '<math xmlns='http://www.w3.org/1998/Math/MathML' display='block'>
       <mn>3.1416</mn>
     </math>
     '

Now change the output order of a symbolic polynomial. Create a symbolic polynomial and set 'PolynomialDisplayStyle' preference to 'ascend'. Generate MathML form of the polynomial sorted in ascending order.

syms x;
poly = x^2 - 2*x + 1;
sympref('PolynomialDisplayStyle','ascend');
chr = mathml(poly)
chr =
    '<math xmlns='http://www.w3.org/1998/Math/MathML' display='block'>
       <mrow>
         <mn>1</mn>
         <mo>-</mo>
         <mrow>
           <mn>2</mn>
           <mo form='infix'>&InvisibleTimes;</mo>
           <mi>x</mi>
         </mrow>
         <mo>+</mo>
         <msup>
           <mi>x</mi>
           <mn>2</mn>
         </msup>
       </mrow>
     </math>
     '

Input Arguments

collapse all

Input, specified as a symbolic number, variable, array, function, or expression.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: mathml(f,'Tooltips',true)

Inline MathML display, specified as the comma-separated pair consisting of 'DisplayInline' and either true or false (default).

Tooltips in MathML output, specified as the comma-separated pair consisting of 'Tooltips' and either true or false (default). mathml adds tooltips for units and some special functions.

Introduced in R2018b