11

Is there a simple package for which I can input an abstract graph and it returns a drawing?

A similar question was asked in 2010, it did not receive any answers relevant to my question. I want no complicated code, just give the edge-list of the graph and get any drawing. (Of course a few optional parameters are welcome.) This is already implemented in many programming languages, so I suppose it should also be available in LaTeX.

domotorp
  • 389
  • 3
    @Andrestand That's a pretty old reference. TikZ now has a fullblown graphdrawing library. – percusse Sep 29 '14 at 08:17
  • 3
    @percusse I don't know very well the SE rules, but wouldn't it be more appropriate then to reactivate the original question? – Andrestand Sep 29 '14 at 08:21
  • 1
    @Andrestand If you feel like it yes please do. But it's not strictly required in this case. We rather work on guidance/community agreement bases. Reviewers Don't you read any meta questions? – percusse Sep 29 '14 at 08:41
  • @percusse I don't understand what you mean. Wouldn't be more efficient (in order to make easily-accessible answers to common questions, etc) to merge the modern answer to the current post into the duplicate? What meta question do you have in mind? – T. Verron Sep 29 '14 at 08:44
  • 1
    @Andrestand I don't mind merging my question with the old one, please do so, if you find it best, I don't know the policy. (I am not sure why you had to announce possible duplicate when I've stated so in my original question.) – domotorp Sep 29 '14 at 08:49
  • @domotorp The comment is added automatically by the system when the duplicate vote is registered. – yo' Sep 29 '14 at 09:00
  • @tohecz That makes it even more mysterious why Andrestand also added his comment. – domotorp Sep 29 '14 at 09:01
  • @domotorp You mean because he has not enough reputation? Probably a flag vote was used. – yo' Sep 29 '14 at 09:03
  • 1
    @T.Verron If updated yes, otherwise there is no point merging with an old question. So it's not fair to close a question as a duplicate before the general one is updated. Especially when a question mentions an old answer. – percusse Sep 29 '14 at 09:06
  • @percusse Ah, indeed. I spent too much time reading the comments and skipped that link in the question, my bad. – T. Verron Sep 29 '14 at 09:10

1 Answers1

15

There are at least two ways:

  1. the most flexible (and the one I recommend) is using PGF3/TikZ's automatic layout facilities available if you have a recent version of the package (version 3.0.0 and above) and if you use luatex/lualatex to compile. See the PGF manual, there's a chapter dedicated to this. The advantage is that you can use the full power of TikZ to style your nodes/edges and it would blend seamlessly with the rest of the document. You can use (almost) arbitrary TeX code for labels/node contents and TikZ styles. The disadvantage could be performance, but you could alleviate this by using the external library (see the manual).

    An example from the manual:

    \tikz [>=spaced stealth’]
    \graph [layered layout, components go right top aligned, nodes=draw, edges=rounded corners]
    {
        first root -> {1 -> {2, 3, 7} -> {4, 5}, 6 }, 4 -- 5;
        second root -> x -> {a -> {u,v}, b, c -> d -> {w,z} };
        third root -> child -> grandchild -> youngster -> third root;
    };
    

    example

  2. use graphviz to generate the layout and then use dot2tex. The advantage is that you still get the same fonts as your main document, you can use math and you can compile the graph once and avoid doing the layout at every compilation of the document.

    There are ways to automate the latter using the LaTeX package dot2texi. Remember to run pdflatex with the -shell-escape option to make this work: dot2texi will need to invoke the external dot2tex tool which needs to be installed (depends on python) along with graphviz. With recent versions you have the option to generate TikZ code and specify custom TikZ styles for nodes/edges. A quote from the docs:

    The output from Graphviz and dot2tex is not perfect. Manual adjustments are sometimes necessary to get the right results for use in a publication. For final and cosmetic adjustments, it is often easier to edit the generated code than to hack the dot source. This is especially true when using the tikz output format.

Bordaigorl
  • 15,135
  • 1
    I'm not completely convinced by the last sentence. If you're using TikZ, you have plenty of possibilities to customize graphviz nodes. See Drawing relationships between elements of a database. – Claudio Fiandrino Sep 29 '14 at 08:40
  • Ah, actually that is based on experiments done a long time ago, I see now that dot2tex evolved considerably...I might add that to the answer. Thanks! – Bordaigorl Sep 29 '14 at 09:00
  • Thank you. Could I also ask you to tell me what packages to load? I've tried for an hour, have been reading the manual, but I cannot make your example translate. Can the problem be that I use MiKTeX? If so, are there any solutions that work with MiKTeX? – domotorp Sep 29 '14 at 10:47
  • which option are you using? with 1 you will need tikz, \usetikzlibrary{graphdrawing} and \usegdlibrary{layered} + compiling using lualatex. See chapter IV of manual... – Bordaigorl Sep 29 '14 at 10:54
  • @domotorp Specifically you need TikZ/PGF v3.00. Probably you need to update your distribution too. – percusse Sep 29 '14 at 11:05
  • Well, actually I wanted a simpler solution than installing a new compiler... Please let me know if you have a solution that works with MiKTeX. – domotorp Sep 29 '14 at 11:08
  • @domotorp Lua(La)TeX is part of MikTeX too, there's nothing stopping you from using it there. – Paul Gessler Sep 29 '14 at 11:23
  • @Paul Oh, cool, then I suppose I only have to figure out how to install the missing library... – domotorp Sep 29 '14 at 11:59
  • @domotorp just use the MikTeX Package Manager to update the pgf package to the current version. – Paul Gessler Sep 29 '14 at 12:02
  • Where does this example go? Inside \beging{tikzpicture}? – mathtick Jun 12 '23 at 08:54
  • 1
    @mathtick the example is taken from https://tikz.dev/gd-overview#sec-27.2 there's a whole section dedicated just to graph drawing – Bordaigorl Jun 13 '23 at 14:53