# How to draw network diagrams with lua and tikz?

I was reading this article on drawing with lua and tikz. On page 45 of the article, the author's given an example code demonstrating the way to do so. Here's his lua code:

-- File SimpleDemo.lua
local MyAlgorithmClass = {}
function MyAlgorithmClass:run()
local g = self.digraph
local alpha = (2 * math.pi) / #g.vertices
for i,vertex in ipairs(g.vertices) do
vertex.pos.x = radius * math.cos(i * alpha)
vertex.pos.y = radius * math.sin(i * alpha)
end
end

-- "Publish" the algorithm
local graph_drawing_framework = require "pgf.gd.interface.InterfaceToAlgorithms"
graph_drawing_framework.declare{
key = "simple demo layout",
algorithm = MyAlgorithmClass,
preconditions = { connected = true }
}


and on the following pages he's added four tikz{…} code to show the output. 3rd example gives me the following error:

! Package pgfkeys Error: I do not know the key '/tikz/stub' and I am going to ignore it. Perhaps you misspelled it.


Here's my tex document with the 3rd example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{graphs, graphdrawing, mindmap}
\usegdlibrary{SimpleDemo}

\begin{document}
\tikz \graph [ simple demo layout, radius=1cm]{
a --[orient=right] b -- c -- a;
d -- e;
f -- g -- h -- d --[stub,red] f;
e --[stub, red] g;
};
\end{document}


graphs, graphdrawing, mindmap libraries resolved issues for other 3 examples. Which one do I have to add to resolve the issue of 3rd example?

• Does the problem also occur when you put the extension in the require, i.e., \directlua{require("SimpleDemo.lua")}? – Marijn Jun 3 '19 at 11:04
• Could be a custom style I suppose. If you add a placeholder like \tikzset{stub/.style={{}} does it work as expected? – Torbjørn T. Jun 3 '19 at 15:51
• Sorry, brace error. Remove one opening brace after style. Or just remove stub from the code, that’s just as easy for testing. – Torbjørn T. Jun 3 '19 at 15:58
• Was just opening it. (It's not an article, but a set of slides for a presentation.) You're sort of asking the wrong question though. You've done the Lua part, which does the positioning of the nodes, that works, and isn't really relevant for the problem, I think. What you want to know is how to define a TikZ style stub which changes how a path between two nodes is drawn in a specific manner. Looks like the middle part of the path is cut out. – Torbjørn T. Jun 3 '19 at 16:11

Below is one possible definition of a stub style which does something similar to what is seen in the slides you refer to. I used one of the built-in positioning algorithms, but that shouldn't matter for the line style. Slide 46 indicates how you can change the style of the nodes.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{graphs, graphdrawing, calc} % <-- added the calc library
\usegdlibrary{circular}
\tikzset{stub/.style={
to path={%
(\tikztostart) -- ($(\tikztostart)!0.3!(\tikztotarget)$)
($(\tikztostart)!0.7!(\tikztotarget)$) -- (\tikztotarget)
}
}}
\begin{document}
\tikz \graph [ simple necklace layout, radius=1cm, nodes={circle,draw=blue!80,fill=blue!10}]{
d -- e;
f -- g -- h -- d --[stub,red] f;
e --[stub, red] g;
};
\end{document}

• That's what I've done when I was experimenting flowcharts. I've seen one doing like those /.style … on YouTube! Can we set these styles in lua? – user189833 Jun 3 '19 at 16:31
• @EmonHaque Why do you want that? Such a style contains TikZ-options, using TikZ syntax. – Torbjørn T. Jun 3 '19 at 16:34
• Someone wrote that there are other libraries like kpse, node, etc. besides tex e.g. tex.print. I thought there are something like node.draw, node.shape, etc. to help us avoid those tex syntax. Are there any actually? – user189833 Jun 3 '19 at 16:39
• @EmonHaque No idea. – Torbjørn T. Jun 3 '19 at 16:40
• I've found an example of using lua node and posted this question – user189833 Jun 5 '19 at 11:58