GitHunt
JO

johnyf/nx2tikz

Export NetworkX graphs to TikZ directly

About

nx2tikz is a package for exporting networkx
graphs directly to TikZ, letting
TikZ itself layout the graph.
The generated TikZ code can be typeset using LuaTeX.
The package can be used to convert a networkx graph to:

  • a PDF depicting the graph
  • LaTeX code for a tikzpicture, containing TikZ code describing the graph
    (to be included in a LaTeX document)
  • LaTeX code for a complete LaTeX document,
    containing TikZ code describing the graph

These are possible via two interfaces:

  • from Python by calling functions from nx2tikz, with a networkx graph
    as argument
  • from the command line, by passing the name of a Python script that contains
    a function that returns a networkx graph

The purpose of package nx2tikz is to avoid the:
dump DOT file ->
GraphViz ->
dot2tex ->
dot2texi toolchain,
because it is fragile.

Usage

The package can be:

  • imported in Python, or
  • invoked from the command line.

using nx2tikz from Python

The Python package can be imported with

import nx2tikz

Assuming that g is an instance of a networkx graph, the following are
available:

  • nx2tikz.dumps_tikz(g) returns as string the code of a tikzpicture
    environment that contains TikZ code describing the graph
  • nx2tikz.dump_tikz(g, filename) creates a file named filename that
    contains the code of a tikzpicture environment that describes the graph
  • nx2tikz.dump_pdf(g, filename) creates a PDF file named filename that
    contains a diagram of the graph. It calls lualatex to typeset the PDF file.

Read also the docstrings of these functions for more information.

Another useful function is nx2tikz.nx2tikz.dump_tex(), which creates a LaTeX
file for a document to typeset a diagram of the graph (includes preamble).

using nx2tikz from the command line

The input on the command line is a Python module that contains a function
named graph. The function graph should return a networkx graph.

To write a tikzpicture as a file:

nx2tikz --input example.py --output out.tex --format tikz

When you include such pictures in your main document, remember to import the
necessary TikZ packages in the preamble, for example:

\usepackage{tikz}
\usetikzlibrary{graphs,graphs.standard,graphdrawing,quotes,shapes}
\usegdlibrary{force}

and compile with lualatex.

To directly compile with lualatex an image as a PDF file:

nx2tikz --input example.py --output out --format pdf

To dump an entire LaTeX document, complete with preamble:

nx2tikz --input example.py --output out.tex --format tex

An example for using nx2tikz is the Python script examples/example.py.

Installation

Using pip:

pip install .

The package also installs in Python's bin/ directory a script named nx2tikz
as entry point.
This script can be invoked from the command line, for example:

nx2tikz -h

References

[1] Jannis Pohlmann
Configurable graph drawing algorithms for the TikZ graphics description language
Diplomarbeit, Universitat zu Lubeck, 2011

[2] Till Tantau
Graph drawing in TikZ
Journal of graph algorithms and applications
Vol.17, No.4, pp.495--513, 2013
DOI:10.7155/jgaa.00301
slides

License

BSD-3, see LICENSE file.

Languages

Python94.4%Makefile5.6%

Contributors

Other
Created August 21, 2014
Updated July 28, 2025
johnyf/nx2tikz | GitHunt