5

Let's consider I have a database where with appropriate passages I obtain the a string that describes a layered tree.

Consider the following listing:

% !TEX encoding   = UTF-8
% !TEX program    = LuaLaTeX
% !TEX spellcheck = en_US
\documentclass{standalone}

\usepackage[T1]{fontenc}

\usepackage{tikz}
\usetikzlibrary{arrows.meta,graphs,graphdrawing}
\usetikzlibrary{calc}
\usegdlibrary{layered,circular}

\usepackage{luacode}


\begin{luacode*}
-- After long computation from a database, I obtain the following string
str = '"S 90311"->{"dd","ddd","dddd"}'
\end{luacode*}

%%%%%%%%%%%%
% Document %
%%%%%%%%%%%%
\begin{document}
\begin{tikzpicture}[scale=1.2,%
nodes={%text height = .7em,
       %text depth  = .2em,
       %text width  = 2cm,
       draw        = black!20,
       thick,
       fill        = white,
       anchor      = east},%"
       ->,
       rounded corners,
       semithick]

\graph[
% LAYERED
layered layout,
%level distance   = 2cm,
%sibling sep      = 1cm,
%sibling distance = 1cm
% CIRCULAR
%simple necklace layout,
grow'    = left,
%node sep = 1em,
]{%
\directlua{tex.print(str)};

"S 90311"->{"dd","ddd","dddd"};
};
\end{tikzpicture}
\end{document}

The difference between the command is shown in the following picture

enter image description here

In the first solution, all the string is considered a node.

How can I pbtain the second result from the Lua string?

Azoun
  • 2,317

2 Answers2

5

Not really related to directlua, you would see the same if you put the tree syntax in a simple macro defined with \def or \newcommand. Either way it works if you expand the construct so the tokens are seen by the expression parser. For example;

\documentclass{standalone}

\usepackage[T1]{fontenc}

\usepackage{tikz}
\usetikzlibrary{arrows.meta,graphs,graphdrawing}
\usetikzlibrary{calc}
\usegdlibrary{layered,circular}

\usepackage{luacode}


\begin{luacode*}
-- After long computation from a database, I obtain the following string
str = '"S 90311"->{"dd","ddd","dddd"}'
\end{luacode*}


%%%%%%%%%%%%
% Document %
%%%%%%%%%%%%
\begin{document}

\def\zz#1{%
\begin{tikzpicture}[scale=1.2,%
nodes={%text height = .7em,
       %text depth  = .2em,
       %text width  = 2cm,
       draw        = black!20,
       thick,
       fill        = white,
       anchor      = east},%"
       ->,
       rounded corners,
       semithick]
\graph[
% LAYERED
layered layout,
%level distance   = 2cm,
%sibling sep      = 1cm,
%sibling distance = 1cm
% CIRCULAR
%simple necklace layout,
grow'    = left,
%node sep = 1em,
]{%

#1;
};
\end{tikzpicture}}
\expandafter\zz\expandafter{\directlua{tex.print(str)}}


\end{document}
David Carlisle
  • 757,742
4

If your graph needs just one string, you can do it with some help from expl3:

\documentclass{standalone}

\usepackage[T1]{fontenc}

\usepackage{tikz}
\usetikzlibrary{arrows.meta,graphs,graphdrawing}
\usetikzlibrary{calc}
\usegdlibrary{layered,circular}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\luagraph}{O{}m}
 {
  \azoun_luagraph:nf { #1 } { \directlua{tex.print(#2)}; }
 }
\cs_new_protected:Nn \azoun_luagraph:nn
 {
  \graph[#1]{#2}
 }
\cs_generate_variant:Nn \azoun_luagraph:nn { nf }
\ExplSyntaxOff

\usepackage{luacode}

\begin{luacode*}
-- After long computation from a database, I obtain the following string
str = '"S 90311"->{"dd","ddd","dddd"}'
\end{luacode*}

%%%%%%%%%%%%
% Document %
%%%%%%%%%%%%
\begin{document}
\begin{tikzpicture}[scale=1.2,%
nodes={%text height = .7em,
       %text depth  = .2em,
       %text width  = 2cm,
       draw        = black!20,
       thick,
       fill        = white,
       anchor      = east},%"
       ->,
       rounded corners,
       semithick]

\luagraph[
% LAYERED
layered layout,
%level distance   = 2cm,
%sibling sep      = 1cm,
%sibling distance = 1cm
% CIRCULAR
%simple necklace layout,
grow'    = left,
%node sep = 1em,
]{str};
\end{tikzpicture}
\end{document}

So the \luagraph command just takes the str argument.

If more graphs have to be accommodated, then it's possible to do it, but a more complete example would be needed.

enter image description here

egreg
  • 1,121,712