Create a Tikz latex drawing feature for networkx#6238
Create a Tikz latex drawing feature for networkx#6238dschult merged 38 commits intonetworkx:mainfrom
Conversation
vertex -> node, vertices -> nodes PEP8 spacing and tabs pytest function instead of class (class not needed here -- no imports) Moved author info to CONTRIBUTORS.rst as our new standard converted list comprehension done for side-effects to for-loop. switch to f-strings from string-format calls update setup.py to remove conflicts
Also pep8 formatting
Check not multigraph. Add example Change variable name `file` to `latex_code` inline if/else for __init__ setup
(matches adigraph and avoids using random seeds in tests
MridulS
left a comment
There was a problem hiding this comment.
Thanks for this @dschult!
We should try to get this in soon, it's a great feature. Interestingly a bunch of the generated LaTeX doesn't compile on my machine. It has a standard MacOS LaTeX distribution from https://www.tug.org/mactex/
I have suggested the changes to make it work with this distribution. Does the LaTeX still compile for you with the changes? I also tried to follow up the format from https://tikz.dev for these changes.
Co-authored-by: Mridul Seth <mail@mriduls.com>
Co-authored-by: Mridul Seth <mail@mriduls.com>
|
Thanks very much @MridulS !! There were some style errors for the edges that this caught nicely. And the default radius for spacing was much smaller than I anticipated (and didn't check). I've changed the placement of the edge style code within the The I am also using a standard generic install of MacTex (which I think is TexLive plus some mac based GUIs that we're not using). So any errors you see, I am seeing too (just sometimes not looking the right way :). I turned off the multigraph for now as suggested. I don't think it would be hard to make it bend differently for the multiedges in a similar way to Dilara's treatment of multiedges in nx_pylab. But that can be a TODO. Please check it again when you have time. |
MridulS
left a comment
There was a problem hiding this comment.
Thanks for getting this done @dschult! Maybe we can squeeze this in the 3.0 release.
All looks good to me but even the default radius of 10 for polar coordinates doesn't seem to make the plots look appealing. Maybe we should just leave it to the user for now. We can add more better default layouts in further iterations of this.
The current subfigure test plots this out:

If we change the default radius to 50 we get

But this isn't a blocker for me and I don't know what is the right answer :)
Let's get this in and we can make changes in future PRs!
|
Hmmm... None of these seem to be laid out on a circle... I'm all for getting this in and then making good examples later. |
rossbar
left a comment
There was a problem hiding this comment.
Wow this is really nice, thanks @dschult ! I took the liberty of pushing up a few minor docstring/rst formatting nits while reading through. The code for the conversion functions is very nice and straightforward to follow, and the tests seem like just about the best that can be done without some complicated pixel-matching scheme.
I think this is a great new feature and a solid base to expand upon as users try it out.
* Implemented conversion from networkx graph to latex graph. * Removed latex cache files uploaded by error. * Resolved placeholders file import issue. * Removed dependency to numpy * Updated test * Wrapped test for numpy dependency * Updated test name * format changes to nx_latex.py and friends. vertex -> node, vertices -> nodes PEP8 spacing and tabs pytest function instead of class (class not needed here -- no imports) Moved author info to CONTRIBUTORS.rst as our new standard converted list comprehension done for side-effects to for-loop. switch to f-strings from string-format calls update setup.py to remove conflicts * Remove numpy requirement for testing latex Also pep8 formatting * reorder imports in __init__ * Attempt to debug windows/appveyor adigraph error * try again * Remove types, run pre-commit * blackify * pyupgradify * inline latex placeholders and expected_tex. Check not multigraph. Add example Change variable name `file` to `latex_code` inline if/else for __init__ setup * Change tabs to spaces * pull apart Adigraph to simplify interface * remove class Adigraph completely in favor of functions * replace default layout to be circular. (matches adigraph and avoids using random seeds in tests * fix old version of isort result * update documentation and include with rst file. Update some tests * make docs more complete, checked and add tests * Cleanup extra file changes that aren't needed * Add TikZ version of to_latex as default * improve docs * Make the networkx latex interface via TikZ and not adigraph * fix rebase * Apply suggestions from code review Co-authored-by: Mridul Seth <mail@mriduls.com> * comment out print statements in tests * Turn off multigraph for latex * Remove tests of `line width` and edge label placement * add back line width style, fix placement of style on edges. * Set default layout to have radius 10 Co-authored-by: Mridul Seth <mail@mriduls.com> * fix docstring test * Minor docstring formatting nits. Co-authored-by: Luca Cappelletti <cappelletti.luca94@gmail.com> Co-authored-by: Mridul Seth <seth.mridul@gmail.com> Co-authored-by: Mridul Seth <git@mriduls.com> Co-authored-by: Mridul Seth <mail@mriduls.com> Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
* Implemented conversion from networkx graph to latex graph. * Removed latex cache files uploaded by error. * Resolved placeholders file import issue. * Removed dependency to numpy * Updated test * Wrapped test for numpy dependency * Updated test name * format changes to nx_latex.py and friends. vertex -> node, vertices -> nodes PEP8 spacing and tabs pytest function instead of class (class not needed here -- no imports) Moved author info to CONTRIBUTORS.rst as our new standard converted list comprehension done for side-effects to for-loop. switch to f-strings from string-format calls update setup.py to remove conflicts * Remove numpy requirement for testing latex Also pep8 formatting * reorder imports in __init__ * Attempt to debug windows/appveyor adigraph error * try again * Remove types, run pre-commit * blackify * pyupgradify * inline latex placeholders and expected_tex. Check not multigraph. Add example Change variable name `file` to `latex_code` inline if/else for __init__ setup * Change tabs to spaces * pull apart Adigraph to simplify interface * remove class Adigraph completely in favor of functions * replace default layout to be circular. (matches adigraph and avoids using random seeds in tests * fix old version of isort result * update documentation and include with rst file. Update some tests * make docs more complete, checked and add tests * Cleanup extra file changes that aren't needed * Add TikZ version of to_latex as default * improve docs * Make the networkx latex interface via TikZ and not adigraph * fix rebase * Apply suggestions from code review Co-authored-by: Mridul Seth <mail@mriduls.com> * comment out print statements in tests * Turn off multigraph for latex * Remove tests of `line width` and edge label placement * add back line width style, fix placement of style on edges. * Set default layout to have radius 10 Co-authored-by: Mridul Seth <mail@mriduls.com> * fix docstring test * Minor docstring formatting nits. Co-authored-by: Luca Cappelletti <cappelletti.luca94@gmail.com> Co-authored-by: Mridul Seth <seth.mridul@gmail.com> Co-authored-by: Mridul Seth <git@mriduls.com> Co-authored-by: Mridul Seth <mail@mriduls.com> Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
* Implemented conversion from networkx graph to latex graph. * Removed latex cache files uploaded by error. * Resolved placeholders file import issue. * Removed dependency to numpy * Updated test * Wrapped test for numpy dependency * Updated test name * format changes to nx_latex.py and friends. vertex -> node, vertices -> nodes PEP8 spacing and tabs pytest function instead of class (class not needed here -- no imports) Moved author info to CONTRIBUTORS.rst as our new standard converted list comprehension done for side-effects to for-loop. switch to f-strings from string-format calls update setup.py to remove conflicts * Remove numpy requirement for testing latex Also pep8 formatting * reorder imports in __init__ * Attempt to debug windows/appveyor adigraph error * try again * Remove types, run pre-commit * blackify * pyupgradify * inline latex placeholders and expected_tex. Check not multigraph. Add example Change variable name `file` to `latex_code` inline if/else for __init__ setup * Change tabs to spaces * pull apart Adigraph to simplify interface * remove class Adigraph completely in favor of functions * replace default layout to be circular. (matches adigraph and avoids using random seeds in tests * fix old version of isort result * update documentation and include with rst file. Update some tests * make docs more complete, checked and add tests * Cleanup extra file changes that aren't needed * Add TikZ version of to_latex as default * improve docs * Make the networkx latex interface via TikZ and not adigraph * fix rebase * Apply suggestions from code review Co-authored-by: Mridul Seth <mail@mriduls.com> * comment out print statements in tests * Turn off multigraph for latex * Remove tests of `line width` and edge label placement * add back line width style, fix placement of style on edges. * Set default layout to have radius 10 Co-authored-by: Mridul Seth <mail@mriduls.com> * fix docstring test * Minor docstring formatting nits. Co-authored-by: Luca Cappelletti <cappelletti.luca94@gmail.com> Co-authored-by: Mridul Seth <seth.mridul@gmail.com> Co-authored-by: Mridul Seth <git@mriduls.com> Co-authored-by: Mridul Seth <mail@mriduls.com> Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
This PR provides a drawing feature (module and tests) from NetworkX to the TikZ library of TeX/LaTeX.
The basic interface is
nx.to_latex(G, pos, **options)to construct a string of latex code ornx.write_latex(G, filename, as_document=True, **options)to write the string to a file.The
as_documentoption puts the LaTeX figure inside a document environment with preamblethat loads TikZ. When False, the code for the figure stands alone in the file ready to be
\inputinto your LaTeX file.Alternatives to #5639 and #5702
This makes them obsolete if merged.