tikz /

Filename Size Date modified Message
49 B
1.3 KB
161 B
8.7 KB
81 B
1.8 KB
305 B

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
License:BSD License
Git Repository:
PyPI Package:

Prerequisites and Configuration


On your computer the following must be installed:

  • latex with the tikz and the amsmath packages

  • one of :
    • pdftoppm (part of the Poppler pdf library) and pnmtopng (part of the Netpbm package);
    • pdftoppm (part of the Poppler pdf library) and convert (part of the ImageMagick package);
    • ghostscript;
    • pdf2svg.

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

  • texlive and texlive-pictures (and maybe more LaTeX packages)

  • netpbm, imagemagick, ghostscript, or pdf2svg.

  • Install homebrew in the terminal by:

    /usr/bin/ruby -e "$(curl -fsSL"
  • Install poppler (pdftoppm comes with it), by:

    brew install poppler

For Windows you will need to install this two packages:

  • Xpdf package

  • NetPbm for Windows package

    If you don't want to install these packages, you can use only the files nedded.

    From Xpdf

    • pdftoppm

    From NetPbm

    • pnmtopng.exe
    • libnetpbm10.dll
    • libpng13.dll
    • rgb.txt

    Put these files in one folder and add the folder to the system path.

    Also, you need to create a new system variable RGBDEF=C:\TikzSphinx\rgb.txt assuming you copy the files to the C:\TikzSphinx folder.


If you have installed the Tikz Sphinx extension e.g. using PyPI, then you have to load the extension in the Sphinx project configuration file by:

extensions = ['sphinxcontrib.tikz']

Also in, you have to specify the LaTeX preamble in the latex_elements dictionary, adding the tikz package and any other package or library used by the tikz pictures as:

latex_elements = {
'preamble': '''

Additionally, the following configuration values are supported for the html build target:

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

    tikz_proc_suite = ‹suite›

    Note that:

    • if you want your documentation to be built on, you have to choose GhostScript;
    • all suites produce png images, excepted 'pdf2svg' which produces svg.
  • 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›


The above configuration values only apply to the html build target. If you want to use the latex target, then you have to take care to include in the preamble for the latex target:

  • The tikz_latex_preamble
  • The tikz_libraries
  • Any ‹tikz libraries› given to the libs option of the tikz directive (see :ref:`usage`)

This can be done, e.g., as:

latex_elements = {
'preamble': '''\usepackage{tikz}''' + '''
\usetikzlibrary{''' + tikz_tikzlibraries + ‹tikz libraries› + '''}'''


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

The stringsubst option enables the following string substitution in the ‹tikz code›. Before processing the ‹tikz code› the string $wd or ${wd} 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›.

Additionaly, the :include: option can be used to import an entire tikzpicture:

.. tikz::‹caption, potentially broken
   across lines›
   :libs: ‹tikz libraries›
   :include: <filename>



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);`

Example of a plot imported from a file:

An example of use of tikz_tikzlibraries and tikz_latex_preamble options (by Dominik Haumann).


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 and cannot live inside a table or another float.

If you enable :stringsubst: and you happen to have a math expression starting with wd (i.e., you would like to write $wd ... then you must insert some white space, e.g., $w d ... to prevent string substitution.