1. Christoph Reller
  2. tikz


tikz / README.rst

Description of the TikZ Sphinx Extension

This extension to Sphinx enables the use of the PGF/TikZ LaTeX package to draw nice pictures. (See CTAN or sourceforge; the manual is, e.g., here. Also have a look at contributions such as pgfplots.)

Use the extension at your own risk. Anything might change in future versions without further notice.

Author:Christoph Reller creller@ee.ethz.ch
License:BSD License
Git Repository:https://bitbucket.org/philexander/tikz
PyPI Package:http://pypi.python.org/pypi/sphinxcontrib-tikz

Prerequisites and Configuration


On your computer the following must be installed:

  • latex with the tikz and the amsmath packages
  • pdftoppm (part of the Poppler pdf library)
  • either of the following:
    • pnmcrop and pnmtopng (both part of the Netpbm package)
    • convert (part of the ImageMagick package)

(We cannot use dvipng as the pngmath Sphinx extension does because there is an issue with cropping the image if postscript specials are used.)

For Ubuntu Linux you roughly have to have the following packages installed:

  • texlive and texlive-pictures (and maybe more LaTeX packages)
  • poppler-utils
  • netpbm or imagemagick

For Mac OS X a possible way of getting this extension working is:

  • Install homebrew in the terminal by:

    /usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"
  • Install poppler (pdftoppm comes with it), by:

    brew install poppler


The Tikz Sphinx extension consists of the single file tikz.py (along with this description).

In the Sphinx project configuration file conf.py you need to:

  • add the directory where tikz.py is located to sys.path, e.g. by:

    sys.path.append(os.path.expanduser('~/‹path to directory›'))
  • load the extension by:

    extensions = ['tikz']

The following configuration values are supported:

  • Choose the image processing ‹suite›, either 'Netpbm' or 'ImageMagick' ('Netpbm' by default):

    tikz_proc_suite = ‹suite›
  • Enable/disable transparent graphics (enabled by default):

    tikz_transparent = ‹True or False›
  • Add ‹string› to the latex preamble:

    tikz_latex_preamble = ‹string›
  • Add \usetikzlibrary{‹string›} to the latex preamble:

    tikz_tikzlibraries = ‹string›

    You might want to load the tikz package and add the tikzlibraries in the latex_preamble, e.g. as:

    latex_preamble = '''
    \usetikzlibrary{''' + tikz_tikzlibraries + '''}

If you want to make use of the TikZ externalization library for the LaTeX build output, then you may want to change the line:


in /usr/share/sphinx/texinputs/Makefile to:

LATEXOPTS = "-shell-escape"


The extension adds a tikz-directive and a tikz-role. The usage is very similar to the standard math Sphinx extensions.

The tikz-directive can be used in two ways:

.. tikz:: ‹tikz code, potentially broken
   across lines›
   :libs: ‹tikz libraries›


.. tikz:: ‹caption, potentially broken
   across lines›
   :libs: ‹tikz libraries›

   ‹tikz code, potentially broken
   across lines›

The ‹caption› is optional, but if present it is printed as a picture caption below the picture.

The :libs: option expects its argument ‹tikz libraries› to be a comma separated list of tikz libraries to use. If you want to build the latex target then make sure that you add these libraries to latex_preamble in conf.py.

The stringsubst option enables the following string substitution in the ‹tikz code›. Before processing the ‹tikz code› the string %(wd)s is replaced by the project root directory. This is convenient when referring to some source file in the LaTeX code.

The ‹tikz code› is code according to the tikz latex package. It behaves as if inside a tikzpicture environment.

The tikz-role is used as follows:

:tikz:`‹tikz code›`

The ‹tikz code› is code according to the tikz latex package. It behaves as if inside a \tikz macro. TikZ options can be given at the start of the ‹tikz code›.



These examples only render in a Sphinx project with a proper configuration of the Tikz Sphinx extension.

.. tikz:: [>=latex,dotted,thick] \draw[->] (0,0) -- (1,1) -- (1,0)
   -- (2,0);
   :libs: arrows
.. tikz:: An Example Directive with Caption

   \draw[thick,rounded corners=8pt]
An example role :tikz:`[thick] \node[draw] (a) {A};
\node[draw,dotted,right of=a] {B} edge[<-] (a);`

An example role :tikz:`[thick] \node[draw] (a) {A}; \node[draw,dotted,right of=a] {B} edge[<-] (a);`


If you use the tikz directive inside of a table or a sidebar and you specify a caption then the latex target built by the sphinx builder will not compile. This is because, as soon as you specify a caption, the tikzpicture environment is set inside a figure environment and hence it is a float.

If you enable :stringsubst: then the character % cannot be used anymore for commenting LaTeX code.