# How to add options to a graph from within a Lua layout algorithm

I'm working on a graph layout algorithm for use with the graphdrawing library in TikZ, and part of what I'd like to do is use the length of an edge label in calculating edge lengths. If the label is excessively long, which for the use case it may be, I should like to add a "text width = xx"---where xx is a size---option to the edge from within the Lua algorithm. This will allow me to calculate the bounding box for the resized label and use it in edge routing and node positioning.

Currently I am able to access an edge label added as such: q1 ->[edge label={foolabel}] q2 within the scope

\tikz{
\graph[foo layout]{
q1,q2;
q1 ->[edge label = {foo label}] q2;
};
}


by accessing the edge label option using the relevant arc's options() function. In order to do this I must first define an edge label key in the Lua algorithm as follows:

declare {
key = "edge label",
type = "string",

summary = [["
The label of an arc.
"]],
examples = [["
q1 ->[edge label = {foo label}] q2
"]]
}


which is exactly the key used by default for edge labels in graphs (when not using the quotes TikZ library). That is, without declaring the key in the lua file the label will be correctly applied but cannot be accessed from the lua algorithm through the options() function.

Once I have this label's text, I can guesstimate how long it'll be in the typical application at 11pt, and decide whether or not to wrap it. Text in labels can be wrapped by adding text width = xx to the options in the edge, as such: q1 ->[edge label = {foobarboofar}, text width = 3em] q2.

I would like to add this text width option myself, in the Lua algorithm. I've tried adding it to the generated_options field of the arc like this (a is the relevant arc):

a.generated_options = {{key="text width",value="3em"}}


but this has no effect on the graph output. I suspect that generated_options are not rendered as normal options by the Display Layer. In the example in the manual and code (SimpleHuffman.lua), the generated_options are only acted on when their keys are declared in the \tikz scope of the .tex file using \pgfkeys{...}. As the option I'm adding already exists in the keys, I would expect it to work. It doesn't, and re-adding it to every .tex file using my layout would defeat the point of my algorithm.

Furthermore, should I accept that I cannot add options to the graph (surely this is a point of generated_options?), I would need to access the text width option now set by the user in order to calculate the label bounding box. If I attempt to do this in the same way as I accessed the label itself, by declaring the key in the lua algorithm, it allows me to read the text width but prevents the option from being applied to the graph. This does not occur with edge label. I find this extremely confusing.

MWE

\documentclass[11pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{graphs}
\usetikzlibrary{graphdrawing}
\usegdlibrary{layered}

\begin{document}

\tikz{
\graph[layered layout] {
q1, q2;

q1 ->[edge label = {long label which should be wrapped by algorithm}] q2;
};
}
\end{document}


TL;DR

How can I add existing options (such as text width or color) to a graph/vertex or preferably edge from within my lua layout algorithm in such a way that they are applied when the graph is drawn?

Thank you for any help.

• It would be helpful if you composed a fully compilable MWE including \documentclass and the appropriate packages that sets up the problem. While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem. – AML Jun 20 '18 at 17:08
• Thanks for the reminder AML. MWE compiles with lualatex and TexLive 2017 – joseph Jun 20 '18 at 19:22