Skip to content

Create a Tikz latex drawing feature for networkx#6238

Merged
dschult merged 38 commits intonetworkx:mainfrom
dschult:tikz_latex
Jan 4, 2023
Merged

Create a Tikz latex drawing feature for networkx#6238
dschult merged 38 commits intonetworkx:mainfrom
dschult:tikz_latex

Conversation

@dschult
Copy link
Copy Markdown
Member

@dschult dschult commented Nov 26, 2022

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 or
nx.write_latex(G, filename, as_document=True, **options) to write the string to a file.
The as_document option puts the LaTeX figure inside a document environment with preamble
that loads TikZ. When False, the code for the figure stands alone in the file ready to be \input into your LaTeX file.

Alternatives to #5639 and #5702
This makes them obsolete if merged.

LucaCappelletti94 and others added 28 commits November 26, 2022 00:48
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
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
@dschult dschult added this to the networkx-3.0 milestone Nov 26, 2022
Copy link
Copy Markdown
Member

@MridulS MridulS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@dschult
Copy link
Copy Markdown
Member Author

dschult commented Dec 20, 2022

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 draw __ to __ syntax to allow the edge styles to actually work -- as you suggested. And I also moved the edge label placement in that same construction so the labels are styled and placed correctly. Hopefully this now works better.

The line width style does seem to work with an equals sign line width=3 for both nodes (outline of the edge shape) and edges (width of the line draw). I hope I've got the tests checking for code that does actually work with Latex.

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.

Copy link
Copy Markdown
Member

@MridulS MridulS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
Screenshot 2023-01-02 at 5 22 44 PM

If we change the default radius to 50 we get
Screenshot 2023-01-02 at 5 24 00 PM

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!

@dschult
Copy link
Copy Markdown
Member Author

dschult commented Jan 2, 2023

Hmmm... None of these seem to be laid out on a circle...
I think that's why we can't hardly see any of the edges.
A linear layout of the nodes isn't a good layout to show the network.
It almost seems like what I think of as polar coordinates are being interpreted as cartesian coordinates.

I'm all for getting this in and then making good examples later.
Thanks!

@dschult dschult modified the milestones: networkx-3.1, networkx-3.0 Jan 2, 2023
Copy link
Copy Markdown
Contributor

@rossbar rossbar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@dschult dschult merged commit 59ed0cf into networkx:main Jan 4, 2023
@dschult dschult deleted the tikz_latex branch January 19, 2023 04:51
MridulS added a commit to MridulS/networkx that referenced this pull request Feb 4, 2023
* 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>
dschult added a commit to BrunoBaldissera/networkx that referenced this pull request Oct 23, 2023
* 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>
cvanelteren pushed a commit to cvanelteren/networkx that referenced this pull request Apr 22, 2024
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants