1

I'm using Graph Drawing with Force algorithm to place nodes and edges, but for some reason the edges do not display the arrows when [spring layout] is used.

It works correctly when manually placing the nodes and the edges without the algorithmic layout.

My question is how to make the spring layout display the edge arrows correctly as in the second tikz picture.

Follows the MWE.

\documentclass[tikz,border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,  graphdrawing, quotes }
\usegdlibrary {force}

\tikzset{
    node distance = 20mm,
    provides/.style = {->},
    every edge quotes/.style = {fill=white,sloped},
}

\begin{document}

\begin{tikzpicture}[spring layout]
\node [draw] (a) {A};
\node [draw] (b) {B};
\draw [provides] (a) edge ["stuff"] (b);
\end{tikzpicture}

\begin{tikzpicture}
\node [draw] (a) {A};
\node [draw, below = of a] (b) {B};
\draw [provides] (a) edge ["stuff"] (b);
\end{tikzpicture}

\end{document}
1
  • Interesting, looks like the graphdrawing library only catches the options that are given directly at the edge, i.e. \path (a) edge [provides, "stuff"] (b);. (Don't use \draw with only edges.) graphdrawing only defines -- types of edges. Commented Feb 2, 2023 at 9:47

1 Answer 1

1

The Graph Drawing library doesn't seem to make the five different types of edges (--, ->, <-, <->, -!-) available to the user. Every edge will be a -- edge. (This doesn't yet have anything to do about arrow tips, this is about the direction of the edge where a GD library might interpret these differently.)

Only the options to the edge operation will be forwarded to the Graph Drawing algorithm as well when the edge is actually drawn (where -> means the arrow tip specification).

So you will need to do

\path (a) edge [provides, "stuff"] (b);

even every edge won't get considered.


Here's a hack that overloads a few styles which set both the arrow tip as well as the edge type. This hack doesn't apply to edge from parent (used with the child operation) and most likely break other things.

It would be better to use the appropriate hook that is also used by spring layout for example.

Code

\documentclass[tikz,border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, graphdrawing, quotes}
\usegdlibrary  {force}
\tikzset{
  node distance = 20mm,
  provides/.style = {->},
  every edge quotes/.style = {fill=white, sloped}}
\makeatletter
\tikzset{
  edge macro def/.code 2 args={%
    \def\tikz@gd@plain@edge@macro##1##2{%
      \pgfgdedge{\tikz@pp@name{\tikztostart}}{\tikz@pp@name{\tikztotarget}}{#1}{/tikz,#2,##1}{##2}}},
  --/.style ={arrows=-,   edge macro def= {--}{#1}},
  ->/.style ={arrows=->,  edge macro def= {->}{#1}},
  <-/.style ={arrows=<-,  edge macro def= {<-}{#1}},
  <->/.style={arrows=<->, edge macro def={<->}{#1}},
  -!-/.style={arrows=-,   edge macro def={-!-}{#1}, path only}}
\makeatother
\begin{document}

\begin{tikzpicture}[spring layout]
\node [draw] (a) {A};
\node [draw] (b) {B};
\path[provides] (a) edge ["stuff"] (b);
\end{tikzpicture}

\begin{tikzpicture}
\node [draw] (a) {A};
\node [draw, below = of a] (b) {B};
\path [provides] (a) edge ["stuff"] (b);
\end{tikzpicture}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .