https://github.com/johnyf/nx2tikz
Export NetworkX graphs to TikZ directly
https://github.com/johnyf/nx2tikz
graphs networkx tikz
Last synced: 7 months ago
JSON representation
Export NetworkX graphs to TikZ directly
- Host: GitHub
- URL: https://github.com/johnyf/nx2tikz
- Owner: johnyf
- License: other
- Created: 2014-08-21T09:51:43.000Z (about 11 years ago)
- Default Branch: main
- Last Pushed: 2024-10-23T13:49:01.000Z (12 months ago)
- Last Synced: 2025-03-18T12:03:30.211Z (7 months ago)
- Topics: graphs, networkx, tikz
- Language: Python
- Homepage:
- Size: 33.2 KB
- Stars: 30
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
About
=====`nx2tikz` is a package for exporting [`networkx`](https://networkx.org)
graphs directly to [`TikZ`](https://en.wikipedia.org/wiki/PGF/TikZ), letting
[`TikZ` itself layout the graph](https://doi.org/10.7155/jgaa.00301).
The generated TikZ code can be typeset using [LuaTeX](
https://en.wikipedia.org/wiki/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 graphThese 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` graphThe purpose of package `nx2tikz` is to avoid the:
dump DOT file ->
[`GraphViz`](https://en.wikipedia.org/wiki/PGF/TikZ) ->
[`dot2tex`](https://github.com/kjellmf/dot2tex) ->
[`dot2texi`](https://github.com/kjellmf/dot2texisty) 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
```python
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:
```shell
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:```latex
\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:
```shell
nx2tikz --input example.py --output out --format pdf
```To dump an entire LaTeX document, complete with preamble:
```shell
nx2tikz --input example.py --output out.tex --format tex
```An example for using `nx2tikz` is the Python script `examples/example.py`.
Installation
============Using [`pip`](https://pip.pypa.io):
```shell
pip install .
```The package also installs in Python's `bin/` directory a script named `nx2tikz`
as [entry point](
https://setuptools.readthedocs.io/en/latest/userguide/entry_point.html).
This script can be invoked from the command line, for example:```shell
nx2tikz -h
```References
==========[1] Jannis Pohlmann
[Configurable graph drawing algorithms for the TikZ graphics description language](
https://www.tcs.uni-luebeck.de/downloads/papers/2011/2011-configurable-graph-drawing-algorithms-jannis-pohlmann.pdf)
Diplomarbeit, Universitat zu Lubeck, 2011[2] Till Tantau
[Graph drawing in TikZ](
https://www.emis.de/journals/JGAA/accepted/2013/Tantau2013.17.4.pdf)
Journal of graph algorithms and applications
Vol.17, No.4, pp.495--513, 2013
[DOI:10.7155/jgaa.00301](
https://doi.org/10.7155/jgaa.00301)
[slides](
https://www.tcs.uni-luebeck.de/downloads/mitarbeiter/tantau/2012-gd-presentation.pdf)License
=======[BSD-3](https://opensource.org/licenses/BSD-3-Clause), see `LICENSE` file.