{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Mathematical expressions\n\nSelected features of Matplotlib's math rendering engine.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import re\nimport subprocess\nimport sys\n\nimport matplotlib.pyplot as plt\n\n# Selection of features following \"Writing mathematical expressions\" tutorial,\n# with randomly picked examples.\nmathtext_demos = {\n    \"Header demo\":\n        r\"$W^{3\\beta}_{\\delta_1 \\rho_1 \\sigma_2} = \"\n        r\"U^{3\\beta}_{\\delta_1 \\rho_1} + \\frac{1}{8 \\pi 2} \"\n        r\"\\int^{\\alpha_2}_{\\alpha_2} d \\alpha^\\prime_2 \\left[\\frac{ \"\n        r\"U^{2\\beta}_{\\delta_1 \\rho_1} - \\alpha^\\prime_2U^{1\\beta}_\"\n        r\"{\\rho_1 \\sigma_2} }{U^{0\\beta}_{\\rho_1 \\sigma_2}}\\right]$\",\n\n    \"Subscripts and superscripts\":\n        r\"$\\alpha_i > \\beta_i,\\ \"\n        r\"\\alpha_{i+1}^j = {\\rm sin}(2\\pi f_j t_i) e^{-5 t_i/\\tau},\\ \"\n        r\"\\ldots$\",\n\n    \"Fractions, binomials and stacked numbers\":\n        r\"$\\frac{3}{4},\\ \\binom{3}{4},\\ \\genfrac{}{}{0}{}{3}{4},\\ \"\n        r\"\\left(\\frac{5 - \\frac{1}{x}}{4}\\right),\\ \\ldots$\",\n\n    \"Radicals\":\n        r\"$\\sqrt{2},\\ \\sqrt[3]{x},\\ \\ldots$\",\n\n    \"Fonts\":\n        r\"$\\mathrm{Roman}\\ , \\ \\mathit{Italic}\\ , \\ \\mathtt{Typewriter} \\ \"\n        r\"\\mathrm{or}\\ \\mathcal{CALLIGRAPHY}$\",\n\n    \"Accents\":\n        r\"$\\acute a,\\ \\bar a,\\ \\breve a,\\ \\dot a,\\ \\ddot a, \\ \\grave a, \\ \"\n        r\"\\hat a,\\ \\tilde a,\\ \\vec a,\\ \\widehat{xyz},\\ \\widetilde{xyz},\\ \"\n        r\"\\ldots$\",\n\n    \"Greek, Hebrew\":\n        r\"$\\alpha,\\ \\beta,\\ \\chi,\\ \\delta,\\ \\lambda,\\ \\mu,\\ \"\n        r\"\\Delta,\\ \\Gamma,\\ \\Omega,\\ \\Phi,\\ \\Pi,\\ \\Upsilon,\\ \\nabla,\\ \"\n        r\"\\aleph,\\ \\beth,\\ \\daleth,\\ \\gimel,\\ \\ldots$\",\n\n    \"Delimiters, functions and Symbols\":\n        r\"$\\coprod,\\ \\int,\\ \\oint,\\ \\prod,\\ \\sum,\\ \"\n        r\"\\log,\\ \\sin,\\ \\approx,\\ \\oplus,\\ \\star,\\ \\varpropto,\\ \"\n        r\"\\infty,\\ \\partial,\\ \\Re,\\ \\leftrightsquigarrow, \\ \\ldots$\",\n}\nn_lines = len(mathtext_demos)\n\n\ndef doall():\n    # Colors used in Matplotlib online documentation.\n    mpl_grey_rgb = (51 / 255, 51 / 255, 51 / 255)\n\n    # Creating figure and axis.\n    fig = plt.figure(figsize=(7, 7))\n    ax = fig.add_axes((0.01, 0.01, 0.98, 0.90),\n                      facecolor=\"white\", frameon=True)\n    ax.set_xlim(0, 1)\n    ax.set_ylim(0, 1)\n    ax.set_title(\"Matplotlib's math rendering engine\",\n                 color=mpl_grey_rgb, fontsize=14, weight='bold')\n    ax.set_xticks([])\n    ax.set_yticks([])\n\n    # Gap between lines in axes coords\n    line_axesfrac = 1 / n_lines\n\n    # Plot header demonstration formula.\n    full_demo = mathtext_demos['Header demo']\n    ax.annotate(full_demo,\n                xy=(0.5, 1. - 0.59 * line_axesfrac),\n                color='tab:orange', ha='center', fontsize=20)\n\n    # Plot feature demonstration formulae.\n    for i_line, (title, demo) in enumerate(mathtext_demos.items()):\n        print(i_line, demo)\n        if i_line == 0:\n            continue\n\n        baseline = 1 - i_line * line_axesfrac\n        baseline_next = baseline - line_axesfrac\n        fill_color = ['white', 'tab:blue'][i_line % 2]\n        ax.axhspan(baseline, baseline_next, color=fill_color, alpha=0.2)\n        ax.annotate(f'{title}:',\n                    xy=(0.06, baseline - 0.3 * line_axesfrac),\n                    color=mpl_grey_rgb, weight='bold')\n        ax.annotate(demo,\n                    xy=(0.04, baseline - 0.75 * line_axesfrac),\n                    color=mpl_grey_rgb, fontsize=16)\n\n    plt.show()\n\n\nif '--latex' in sys.argv:\n    # Run: python mathtext_examples.py --latex\n    # Need amsmath and amssymb packages.\n    with open(\"mathtext_examples.ltx\", \"w\") as fd:\n        fd.write(\"\\\\documentclass{article}\\n\")\n        fd.write(\"\\\\usepackage{amsmath, amssymb}\\n\")\n        fd.write(\"\\\\begin{document}\\n\")\n        fd.write(\"\\\\begin{enumerate}\\n\")\n\n        for s in mathtext_demos.values():\n            s = re.sub(r\"(?<!\\\\)\\$\", \"$$\", s)\n            fd.write(\"\\\\item %s\\n\" % s)\n\n        fd.write(\"\\\\end{enumerate}\\n\")\n        fd.write(\"\\\\end{document}\\n\")\n\n    subprocess.call([\"pdflatex\", \"mathtext_examples.ltx\"])\nelse:\n    doall()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}