{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n.. redirect-from:: /tutorials/text/mathtext\n\n\n# Writing mathematical expressions\n\nMatplotlib implements a lightweight TeX expression parser and layout engine and\n*Mathtext* is the subset of Tex markup that this engine supports. Note that\nMatplotlib can also render all text directly using TeX if :rc:`text.usetex` is\n*True*; see `usetex` for more details. Mathtext support is available\nif :rc:`text.usetex` is *False*.\n\nAny string can be processed as Mathtext by placing the string inside a pair of\ndollar signs ``'$'``. Mathtext often contains many backslashes ``'\\'``; so that\nthe backslashes do not need to be escaped, Mathtext is often written using raw\nstrings. For example:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nfig = plt.figure(figsize=(3, 3), linewidth=1, edgecolor='black')\nfig.text(.2, .7, \"plain text: alpha > beta\")\nfig.text(.2, .5, \"Mathtext: $\\\\alpha > \\\\beta$\")\nfig.text(.2, .3, r\"raw string Mathtext: $\\alpha > \\beta$\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. seealso::\n\n :doc:`Mathtext example `\n\nTeX does *not* need to be installed to use Mathtext because Matplotlib ships\nwith the Mathtext parser and engine. The Mathtext layout engine is a fairly\ndirect adaptation of the layout algorithms in Donald Knuth's TeX. To render\nmathematical text using a different TeX engine, see `usetex`.\n\n
To generate html output in documentation that will exactly match the output\n generated by ``mathtext``, use the `matplotlib.sphinxext.mathmpl` Sphinx\n extension.