| 1 |
.. _using-dot2tex-as-a-module: |
| 2 |
|
| 3 |
Using dot2tex as a module |
| 4 |
========================= |
| 5 |
|
| 6 |
It is possible to load dot2tex as a module for use in other Python programs. Here is a basic example: |
| 7 |
|
| 8 |
.. sourcecode:: python |
| 9 |
|
| 10 |
import dot2tex |
| 11 |
testgraph = """ |
| 12 |
digraph G { |
| 13 |
a -> b -> c -> a; |
| 14 |
} |
| 15 |
""" |
| 16 |
texcode = dot2tex.dot2tex(testgraph, format='tikz', crop=True) |
| 17 |
|
| 18 |
The ``dot2tex`` function is the main interface: |
| 19 |
|
| 20 |
.. sourcecode:: python |
| 21 |
|
| 22 |
dot2tex(dotsource,**kwargs) |
| 23 |
|
| 24 |
It takes the following input arguments: |
| 25 |
|
| 26 |
====================== =================================================== |
| 27 |
Argument Description |
| 28 |
====================== =================================================== |
| 29 |
``dotsource`` A string containing a DOT or XDOT graph. |
| 30 |
``**kwargs`` An arbitrary number of conversion options passed as |
| 31 |
keyword arguments |
| 32 |
====================== =================================================== |
| 33 |
|
| 34 |
The function returns the resulting LaTeX code as a string. |
| 35 |
|
| 36 |
The supported options are the same as the :ref:`command line options <command-line-options>` (long version). Here are a few examples: |
| 37 |
|
| 38 |
.. sourcecode:: python |
| 39 |
|
| 40 |
import dot2tex as d2t |
| 41 |
texcode = d2t.dot2tex(testgraph, format='tikz', crop=True) |
| 42 |
texcode = d2t.dot2tex(testgraph, preproc=True,figonly=True) |
| 43 |
texcode = d2t.dot2tex(testgraph, debug=True) |
| 44 |
|
| 45 |
Option values are either strings or booleans. Note that some of the command line options are not relevant when using dot2tex as a module. |
| 46 |
|
| 47 |
To specify a template you can use the ``template`` option like this: |
| 48 |
|
| 49 |
.. sourcecode:: python |
| 50 |
|
| 51 |
import dot2tex |
| 52 |
mytemplate = "<<drawcommands>>" |
| 53 |
texcode = dot2tex.dot2tex(graph, template = mytemplate) |
| 54 |
|
| 55 |
|
| 56 |
.. _module-debugging: |
| 57 |
|
| 58 |
Debugging |
| 59 |
--------- |
| 60 |
|
| 61 |
You can set ``debug=True`` to create a detailed log useful for debugging. To retrieve the content of the log you can use the ``get_logstream`` function. It will return a ``StringIO`` instance. You can then use the ``getvalue()`` class method to get the actual text. Example: |
| 62 |
|
| 63 |
.. sourcecode:: python |
| 64 |
|
| 65 |
import dot2tex |
| 66 |
texcode = dot2tex.dot2tex(testgraph, debug=True) |
| 67 |
logstream = dot2tex.get_logstream() |
| 68 |
print logstream.getvalue() |
| 69 |
|
| 70 |
|
| 71 |
.. _positions-output-format: |
| 72 |
|
| 73 |
The ``positions`` output format |
| 74 |
------------------------------- |
| 75 |
|
| 76 |
When you use dot2tex as a module you have access to the special ``positions`` output format if you use ``format=positions``. |
| 77 |
The ``dot2tex`` function will then return dictionary with node name as key and a (x, y) tuple with the |
| 78 |
center position of the node as value: |
| 79 |
|
| 80 |
.. sourcecode:: python |
| 81 |
|
| 82 |
>>> import dot2tex |
| 83 |
>>> testgraph = """ |
| 84 |
... digraph G { |
| 85 |
... a -> b -> c -> a; |
| 86 |
... } |
| 87 |
""" |
| 88 |
>>> dot2tex.dot2tex(testgraph, format='positions') |
| 89 |
{'a': [54, 162], 'b': [27, 90], 'c': [54, 18]} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|