<spanid="extensions"></span><h1>Sphinx extensions for embedded plots, math and more<aclass="headerlink" href="#sphinx-extensions-for-embedded-plots-math-and-more" title="Permalink to this headline">¶</a></h1>
<p>Sphinx is written in python, and supports the ability to write custom
extensions. We’ve written a few for the matplotlib documentation,
some of which are part of matplotlib itself in the
matplotlib.sphinxext module, some of which are included only in the
sphinx doc directory, and there are other extensions written by other
groups, eg numpy and ipython. We’re collecting these in this tutorial
and showing you how to install and use them for your own project.
First let’s grab the python extension files from the <codeclass="file docutils literal"><spanclass="pre">sphinxext</span></code>
directory from git (see <aclass="reference internal" href="getting_started.html#fetching-the-data"><spanclass="std std-ref">Fetching the data</span></a>), and install them in
<p>In addition to the builtin matplotlib extensions for embedding pyplot
plots and rendering math with matplotlib’s native math engine, we also
have extensions for syntax highlighting ipython sessions, making
inhertiance diagrams, and more.</p>
<p>We need to inform sphinx of our new extensions in the <codeclass="file docutils literal"><spanclass="pre">conf.py</span></code>
file by adding the following. First we tell it where to find the extensions:</p>
<divclass="highlight-default"><divclass="highlight"><pre><span></span><spanclass="c1"># If your extensions are in another directory, add it here. If the</span>
<spanclass="c1"># directory is relative to the documentation root, use</span>
<spanclass="c1"># os.path.abspath to make it absolute, like shown here.</span>
<p>And then we tell it what extensions to load:</p>
<divclass="highlight-default"><divclass="highlight"><pre><span></span><spanclass="c1"># Add any Sphinx extension module names here, as strings. They can be extensions</span>
<spanclass="c1"># coming with Sphinx (named 'sphinx.ext.*') or your custom ones.</span>
<p>Now let’s look at some of these in action. You can see the literal
source for this file at <spanclass="xref std std-ref">extensions-literal</span>.</p>
<divclass="section" id="ipython-sessions">
<spanid="ipython-highlighting"></span><h2>ipython sessions<aclass="headerlink" href="#ipython-sessions" title="Permalink to this headline">¶</a></h2>
<p>Michael Droettboom contributed a sphinx extension which does <aclass="reference external" href="http://pygments.org">pygments</a> syntax highlighting on <aclass="reference external" href="http://ipython.scipy.org">ipython</a> sessions. Just use ipython as the
language in the <codeclass="docutils literal"><spanclass="pre">sourcecode</span></code> directive:</p>
<spanid="pyplots"></span><h2>Inserting matplotlib plots<aclass="headerlink" href="#inserting-matplotlib-plots" title="Permalink to this headline">¶</a></h2>
<p>Inserting automatically-generated plots is easy. Simply put the
script to generate the plot in the <codeclass="file docutils literal"><spanclass="pre">pyplots</span></code> directory, and
refer to it using the <codeclass="docutils literal"><spanclass="pre">plot</span></code> directive. First make a
<codeclass="file docutils literal"><spanclass="pre">pyplots</span></code> directory at the top level of your project (next to
:<codeclass="docutils literal"><spanclass="pre">conf.py</span></code>) and copy the <codeclass="file docutils literal"><spanclass="pre">ellipses.py`</span></code> file into it:</p>
<areashape="rect" id="node9" title="StreamRecoder instances translate data from one encoding to another." alt="" coords="21,5,192,31"/>
</map>
<pid="extensions-literal">See the <aclass="reference internal" href="ipython_directive.html#ipython-directive"><spanclass="std std-ref">Ipython Directive</span></a> for a tutorial on embedding stateful,
matplotlib aware ipython sessions into your rest docs with multiline
and doctest support.</p>
</div>
<divclass="section" id="this-file">
<h2>This file<aclass="headerlink" href="#this-file" title="Permalink to this headline">¶</a></h2>