Main Content

mathml

Generate MathML from symbolic expression

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 hovering your mouse on J displays a tooltip containing besselj.

besselj function

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>
     '

The preferences you set using sympref persist through your current and future MATLAB® sessions. Restore the default values by specifying the 'default' option.

sympref('default');

Input Arguments

collapse all

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

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

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.

Version History

Introduced in R2018b