If and when someone answers this question in a way that demonstrates lua programming for TikZ graph algorithms, I will guarantee a 500-point bounty to that person.
According to the PGF 3.0 manual, Chapter 30, one of the key steps in the layered layout
graph-drawing routine is to re-order the nodes within each layer so as to minimize edge crossing. I find that the order in which I specify the nodes is better, for my purposes, than the order created by the default algorithm.
How can I force TikZ not to employ this algorithm but instead do the "stupid" thing, just using the nodes in the same order I give them? I'm aware that this is likely to require some amount of Lua programming, but given the simplicity of the algorithm I'm asking for, it might be feasible to do as an answer to a stack-exchange question.
Note that this question differs from Why does PGF/TikZ 3.0 draw my simple layered graph as non-planar by default? primarily in that I am requesting a specific solution (implement the alternate, naive algorithm).
A "minimal" working example:
% !TEX TS-program = lualatex
\documentclass[tikz,convert,margin=10pt]{standalone}
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{layered}
\begin{document}
\tikz[rounded corners] \graph [layered layout]
{
{[same layer] \foreach \i in {1, 2, ..., 16} {
pt\i/$P^0_{\i}$,
}};
{[same layer] \foreach \i in {1, 2, ..., 17} {
l\i/$P^1_{\i}$,
}};
\foreach \i [evaluate=\i as \j using int(\i+1)] in {1,2,...,16} {
pt\i -- l\i,
pt\i -- l\j,
};
Q/$P^1 \times P^1$ [gray];
\foreach \i [evaluate=\i using int(2*\i)] in {1,2,...,7} {
l\i -- [gray, edge node = {node [fill=white] {h}}] Q,
};
pt1 -- [gray] Q;
};
\end{document}
The result:
I would like the nodes in each layer to be numbered in order, regardless of what TikZ thinks would minimize crossings. Note that making the first two layers into a subgraph is not an acceptable solution here, because then the edge from the first layer to the third layer will not avoid the nodes in between.
Best Answer
Coding a new layout algorithm for tikz's graph library is a daunting task, even in Lua, even as "simple" as the one you want. The reason is the great amount of "boilerplate code" needed.
Instead, it is very easy to "cheat" and use one of the already provided algorithms, but changing one of its parts. In this case, we can take the
layered layout
and remove the part which does the node reordering in each layer.But even this is not so simple, because we don't want to modify any of the lua "system files". We want all our changes to be confined to the document folder.
I've found a way to do it. I do not claim it is the cleanest way (nor even the easiest way perhaps some phases of the algorithm can be changed via pgf keys). But it works (see update 1 for a cleaner and nicer way).
What follows shows how it can be done.
The result
The LaTeX document
The remaining files in the document folder
layered-no-reorder.lua
This one is based in
texmf-dist/tex/generic/pgf/graphdrawing/lua/pgf/gd/layered/library.lua
. Only comments were removed, and one line changed (marked below):no_crossing_minimization.lua
This one is based on
texmf-dist/tex/generic/pgf/graphdrawing/lua/pgf/gd/layered/crossing_minimization.lua
. All comments were stripped out for brevity. The only interesting change is the marked line, which originally wasrequire "pgf.gd.layered.CrossingMinimizationGansnerKNV1993"
.noCrossing.lua
This one is based on
texmf-dist/tex/generic/pgf/graphdrawing/lua/pgf/gd/layered/CrossingMinimizationGansnerKNV1993.lua
, but heavily stripped down. Only therun()
function remains, and basically does nothing.Update 1
There is a cleaner way to integrate the new "no reorder" algorithm into the general
layered layout
, without replacing the library. This way the author still has control to decide if he wants some graphs using the standardsweep crossing minimization
(which is the default in Tikz) while others use the newno crossing minimization
.This is how:
layered-no-reorder.lua
used in previous solution. It is no longer required, since standardlayered
library will be used instead.no_crossing_minimization.lua
. We will rewrite it (with a different name) in a better way.noCrossing.lua
as in the previous solution. This is the file which implements the new "algorigthm" for the reordering phase.Now, write the following in a file called
more-crossing-algorightms.lua
:Note the
key
property. It is the name of a key which can be used later in the TikZ diagram to select this algorithm. In principle, we could declare more crossing algorithms in this same file, giving a different key to each one. This will allow to select one or other from tikz, using the appropiate key. Note also thephase_default
property, which is set tofalse
. This means that this algorithm will not be used by default, but only when it is explicitly selected with the keyno crossing algorithm
.Now the main file is:
Note the
\tikz
line. It has the optionno crossing minimization
, which selects the new algorithm. If we omit that option, then the defaultsweep crossing algorithm
will be selected (because it is marked as the default algorithm in TikZ libraries).To sum up, all the files required in this new solution are
test.tex
(main document),more-crossing-algorithms.lua
andnoCrossing.lua
. Standard TikZ libraries are not replaced, but extended.