7

I'm trying out the algorithmic graph drawing capabilities of TikZ/PGF and initially I was very exited. However, now I am stuck.

My example is below. I think the graph would look better if node "J" was moved down and left and node "G" was moved inside the cycle A-I-J-F-A.

So far I have tried different random seeds and different random orders of the lines in the TeX-file. I have also tried to use absolute node positioning that the force directed drawing would fine-tune, but it never produced anything.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{force}
\begin{document}
\tikz \graph [spring layout, random seed=1] {
  A -- F;
  A -- G;
  A -- I;
  B -- F;
  C -- D;
  C -- F;
  D -- E;
  D -- H;
  D -- I;
  E -- I;
  F -- J;
  G -- I;
  G -- J;
  I -- J;
};
\end{document}

Simple graph

  • Welcome to TeX.SX! Dummy question: if you know how your graph should look like, could manual positionning be an acceptable solution to you? – ebosi Nov 7 '16 at 11:14
  • @ebo: Yes, manual positioning is how I currently do it. It has it's pros and cons. Perhaps a good solution would be to give an initial configuration manually and then let the computer smooth it out. I think the Tikz manual says you can do it, but I can't make it work. – Mankka Nov 7 '16 at 12:16
8

I see that you're using spring layout but it seems to me that the resulting graphic is more like distributed charges (electric charges) making me think that the spring electrical layout is more suiting. Just by using it you already see the difference in the final graph. Furthermore, to fine tune the graphic you can use the nudge=<coordinate> key (or the variants nudge up, down, left and right), although to mee it seems a little rudimentar, but if the drawing needs 'simple' nudges, than it's ok:

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{force}
\begin{document}
\tikz \graph [spring electrical layout, random seed=1] {
  A -- F;
  A -- I;
  B -- F;
  C -- D;
  C -- F;
  D -- E;
  D -- H;
  D -- I;
  E -- I;
  F -- J[nudge=(210:10mm)];
  I -- J;
  G[nudge=(260:11mm)] -- J;
  G -- A;
  G -- I;
};
\end{document}

enter image description here

You can alse try to set electric charges to the nodes, fine tunning the intensity they repell (positive values) or atract (negative values) the connected nodes. Though with this way the result is sometimes quite unpredictable.

Second Approach

If you look at the previous drawing seems that node J has an invisible connection with nodes C and D. The same could be said for the node G with F. My thought was, if I could make this invisible connection then the resulting graph would probably be what the OP wants without any nudges and with more reliable graph. To do so we must give the line-to operator (--) the option draw=none when making the Connections (I also took the liberty of shortening the code a little):

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{force}
\begin{document}
\tikz \graph [spring electrical layout] {
  A -- F -- C -- D -- E -- I -- A;
  D -- I;
  B -- F;
  H -- D;
  I -- J -- F;
  I -- G -- A;
  G -- J;
  % invisible connections
  D --[draw=none] J --[draw=none] C;
  G --[draw=none] F;
};
\end{document}

The result is nearly the same as before, but should give no Errors this time:

enter image description here

  • For me, this example fails with ! Undefined control sequence. \sa@placebox ->\newpage \global \pdfpagewidth<br> =\wd \sa@box \global \pdfpageh... l.20 };. I have LuaTeX, Version 0.95.0 (TeX Live 2016). – Mankka Nov 7 '16 at 20:38
  • Well, that's just weird. Worked in Overleaf as well as in my PC with Windows 10/TL 2015. I have no idea if something changed from 2015 to 16, maybe it's a bug. – Guilherme Zanotelli Nov 7 '16 at 20:42
  • I had also tried spring electrical layout and spring electrical layout'. The results I got required more than a few nudges. – Mankka Nov 7 '16 at 20:50
  • 1
    Hm, that's bad... But look, with TikZ's graphdrawing library you're supposed to not care so much about the position of everything, then a few nudges will always manage it. If you're requiring a lot of fine tuning maybe it's because you want a strict drawing and therefore are better off with manually positioned graph... Also, did you try setting electric charge values for the nodes? Maybe that also helps. – Guilherme Zanotelli Nov 7 '16 at 21:05
  • @Mankka, please test the second approach. I believe it's more elegant and should give no errors. :D – Guilherme Zanotelli Nov 8 '16 at 10:23

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.