# How to correctly set x,y of nodes within a custom lua layout

I am trying to understand how I can place nodes within lua at correct x and y coordinates. I have this code mainly from the pgf/tikz manual:

-- This is the file RectangleTreeLayout.lua
local declare = require "pgf.gd.interface.InterfaceToAlgorithms".declare

local RectangleTreeLayoutClass = {} -- A local variable holding the class table

declare  {
key = "rectangle tree layout",
algorithm = RectangleTreeLayoutClass
}

function RectangleTreeLayoutClass:run()
local g = self.digraph
local positions = {{x=0, y=0}, {x=1*28.3465, y=0}, {x=5*28.3465, y=0}}
for i, v in ipairs(g.vertices) do
local pos = positions[i]
v.pos.x = pos.x
v.pos.y = pos.y
end
end


The positions I have defined by myself. And this is my Latex code:

\documentclass[border=0.5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{positioning}
\begin{document}
\usegdlibrary{RectangleTreeLayout}
\begin{tikzpicture}
\graph[rectangle tree layout, no placement, nodes={draw, on grid}]
{
1 -> 2 -> 3;
};
\graph[no placement, nodes={draw, on grid, red}]
{
1 -> 2 [x=1] -> 3[x=5];
};
\draw[help lines] (0, -5) grid (5, 5);
\end{tikzpicture}
\end{document}


I would expect the red nodes from the lua layout. Changing x and y does not change anything. Using the example code from the pgf/tikz manual where they create a circle with the nodes results into the same graph they have showed. So how would I use the the x,y correctly?

This behaviour is documented in section 28.5 of the PGF manual:

Just as a graph drawing algorithm cannot know where a graph should be placed on a page, it is also often unclear which orientation it should have. Some graphs, like trees, have a natural direction in which they “grow”, but for an “arbitrary” graph the “natural orientation” is, well, arbitrary.

So generally the orientation is assumed to be irrelevant in a graph drawing algorithm and the user should specify the intended orientation using TikZ keys. E.g. you could say:

\documentclass[border=0.5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{positioning}
\begin{document}
\usegdlibrary{RectangleTreeLayout}
\begin{tikzpicture}
\graph[rectangle tree layout, no placement, nodes={draw, on grid}]
{
1 ->[orient=-] 2 -> 3;
};
\graph[no placement, nodes={draw, on grid, red}]
{
1 -> 2 [x=1] -> 3[x=5];
};
\draw[help lines] (0, -5) grid (5, 5);
\end{tikzpicture}
\end{document}


to get horizontal orientation.

If your algorithm is special and needs a specific orientation of the nodes, you can ask PGF to skip rotating you graph using the fixed postcondition:

• fixed

When set, no rotational postprocessing will be done after the algorithm has run. Usually, a graph is rotated to meet a user’s orient settings. However, when the algorithm has already “ideally” rotated the graph, set this postcondition.

This would lead to the following algorithm code:

-- This is the file RectangleTreeLayout.lua
local declare = require "pgf.gd.interface.InterfaceToAlgorithms".declare

local RectangleTreeLayoutClass = {} -- A local variable holding the class table

declare  {
key = "rectangle tree layout",
postconditions = {fixed = true},
algorithm = RectangleTreeLayoutClass
}

function RectangleTreeLayoutClass:run()
local g = self.digraph
local positions = {{x=0, y=0}, {x=1*28.3465, y=0}, {x=5*28.3465, y=0}}
for i, v in ipairs(g.vertices) do
local pos = positions[i]
v.pos.x = pos.x
v.pos.y = pos.y
end
end

• +1 for you detailed explanation and also by providing examples for both cases. – Sens4 Aug 19 '19 at 8:47