Docxpresso allows you to insert mathematical equations into your documents.

In order to do that you must provide the math method all mathematical formulas either in MathML 1.0 or StarMath 5.0 format (you may find detailed tutorial about their respective syntaxes in the provided links).

Beware that currently there is not support for MathML 2.x so if you use mathML you have to sitck to v1.0 conventions.

The core method responsible of inserting a math equation into the document is the math method which public API is given by:

Signature

public math ($equation [, $options])

Parameters

  • $equation (type: string). The equation in MathML 1.0 or StarMath 5.0 format (if the later is choosen one should set the type option to StarMath).
  • $options (type: array). This array has the following available keys and values:
    • math-settings (type: string).
      This array has the following available keys and values:
      • base-font-value (type: integer, default: 12). The base font size (in points) used in the equation.
      • rel-text-size (type: integer, default: 100). Relative text size.
      • rel-indexes-size (type: integer, default: 60). Relative text size.
      • rel-function-size (type: integer, default: 100). Relative function size.
      • rel-operators-size (type: integer, default: 100). Relative operator size.
      • rel-limits-size (type: integer, default: 60). Relative limits size.
      • variables-font (type: string, default: Times New Roman). Variable font family.
      • functions-font (type: string, default: Times New Roman). Functions font family.
      • number-font (type: string, default: Times New Roman). Number font family.
      • text-font (type: string, default: Times New Roman). Text font family.
      • custom-serif-font (type: string, default: Times New Roman). Custom serif font family.
      • custom-sans-serif-font (type: string, default: Arial). Custom sans-serif font family.
      • custom-fixed-font (type: string, default: Courier New). Custom fixed font family.
    • type (type: string, default: MathML). It can take the following values: MathML or StarMath.
    • style (type: string). A string of styles in CSS format.

The relevant CSS style type styles that one may apply to a math formula include.

  • Display:
    • float: it also admits center besides the standard left and right values.
    • width: the total math formula width (not recommended unless you want to force a specific size).
    • height: the total math formula height (not recommended unless you want to force a specific size).
  • Borders:
    • border: shorthand for border properties.
    • border-color: sets the border color.
    • border-style: sets the border line style.
    • border-width: sets the border width.
  • Margins and paddings:
    • margin: specifies all margin widths in on shot.
    • padding: specifies all padding widths in on shot.

In order to style the math display we strongly recommend to wrap the math formula within a paragraph or text-box so one may have higher control on position , borders, margins, etcetera.

Let us work out an actual example with a math equation written in MathML 1.0:

<?php
/**
 * This sample script inserts math equations
 */
require_once 'pathToDocxpresso/CreateDocument.inc';
$doc = new Docxpresso\CreateDocument();
$format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf
//insert a paragraph:
$doc->paragraph(array('text' => 'This is a sample math equation:'));
//insert a math equation in MathML 1.0
$eq='<math xmlns="http://www.w3.org/1998/Math/MathML">
        <mrow>
            <mrow>
                <mstyle mathvariant="bold">
                    <mrow>
                        <mi>A</mi>
                    </mrow>
                </mstyle>
                <mo stretchy="false">=</mo>
                <mfenced open="[" close="]">
                    <mrow>
                        <mtable>
                            <mtr>
                                <mtd>
                                    <mrow>
                                        <mi>a</mi>
                                    </mrow>
                                </mtd>
                                <mtd>
                                    <mrow>
                                        <mi>b</mi>
                                    </mrow>
                                </mtd>
                            </mtr>
                            <mtr>
                                <mtd>
                                    <mrow>
                                        <mi>c</mi>
                                    </mrow>
                                </mtd>
                                <mtd>
                                    <mrow>
                                        <mi>d</mi>
                                </mrow>
                                </mtd>
                            </mtr>
                        </mtable>
                    </mrow>
                </mfenced>
            </mrow>
        </mrow>
    </math>';

$doc->paragraph(array('style' => 'text-align: center;'))
		->math($eq, array('math-settings' => array('base-font-size' => 16)));
//and some additional inline math
$eq2 = '<math xmlns="http://www.w3.org/1998/Math/MathML">
            <mrow>
                <mn>2</mn><mi>π</mi><mi>r</mi>
            </mrow>
        </math>';
$doc->paragraph()
                ->text(array('text' => 'The circunference length is given by '))
		->math($eq2)
                ->text(array('text' => ' .'));
//include in the render method the path where you want your document to be saved
$doc->render('simple_math' . $format); 
//echo a link to the generated document
echo 'You may download the generated document from the link below:<br/>';
echo '<a href="' . 'simple_math' . $format . '">Download document</a>';

DOWNLOAD:download pdfdownload docdownload docxdownload odtdownload rtf

There are currently some limitations in the formatting of math formulas in Word (.doc and .docx).

Let us run the same example with StarMath 5.0 syntax:

<?php
/**
 * This sample script inserts math equations in StarMath 5.0 format
 */
require_once 'pathToDocxpresso/CreateDocument.inc';
$doc = new Docxpresso\CreateDocument();
$format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf
//insert a paragraph:
$doc->paragraph(array('text' => 'This is a sample math equation:'));
//insert a paragraph with a math formula
$eq='A = left [ matrix { a # b ## c # d } right ]';
$doc->paragraph(array('style' => 'text-align: center; vertical-align: middle'))
		->math($eq, array('math-settings' => array('base-font-size' => 16), 'type' => 'StarMath'));
//and some additional inline math
$eq2 = '2 %pi r';
$doc->paragraph()
                ->text(array('text' => 'The circunference length is given by '))
		->math($eq2, array('type' => 'StarMath'))
                ->text(array('text' => ' .'));
//include in the render method the path where you want your document to be saved
$doc->render('starmath' . $format); 
//echo a link to the generated document
echo 'You may download the generated document from the link below:<br/>';
echo '<a href="' . 'starmath' . $format . '">Download document</a>';

DOWNLOAD:download pdfdownload docdownload docxdownload odtdownload rtf

Word formats do not support StarMath syntax.