10

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.

11
  • 3
    @Andrestand That's a pretty old reference. TikZ now has a fullblown graphdrawing library.
    – percusse
    Sep 29, 2014 at 8: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, 2014 at 8: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, 2014 at 8:41
  • 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, 2014 at 8:49
  • 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, 2014 at 9:06

1 Answer 1

14

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.

9
  • 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. Sep 29, 2014 at 8: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, 2014 at 9: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, 2014 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, 2014 at 10:54
  • @domotorp Specifically you need TikZ/PGF v3.00. Probably you need to update your distribution too.
    – percusse
    Sep 29, 2014 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.