从 Lua 表/字符串获取 TikZ 树

从 Lua 表/字符串获取 TikZ 树

假设我有一个数据库,通过适当的段落我可以获得描述分层树的字符串。

考虑以下清单:

% !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}

命令之间的区别如下图所示

在此处输入图片描述

在第一个解决方案中,所有字符串都被视为一个节点。

如何从 Lua 字符串中获取第二个结果?

答案1

与 directlua 无关,如果您将树语法放在用\def或定义的简单宏中,您会看到相同的\newcommand结果。无论哪种方式,如果您扩展构造以便表达式解析器可以看到标记,它都会起作用。例如;

\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}

答案2

如果您的图表只需要一个字符串,您可以借助以下方法完成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}

因此该\luagraph命令仅接受str参数。

如果需要容纳更多的图表,那么是可以做到的,但需要更完整的示例。

在此处输入图片描述

相关内容