在“tikz 环境”中设置“linebreaks”等,生成为“\_tl”变量

在“tikz 环境”中设置“linebreaks”等,生成为“\_tl”变量

\NewDocumentCommand\myShowTree使用参数中定义的键值#1调用函数,该\cs_函数\rnTikZ_showTree:n又调用\cs_函数\rn_tikzStyle_FR和,这些函数为环境\rn_tikzContent_FR:n生成style和绘制二叉搜索树。在此 MWE 中,传递给的参数是一个虚拟的,并且模仿了生成和的方式- MWE 的目的仅仅是为了说明我在表示节点中的某些信息时遇到的困难,特别是。第 1 节和第 2 节将树绘制为本机图片,即手动定义环境。在节点内容中定义单个节点样式、定义换行符或设置颜色和文本样式等没有任何问题。第 3 节使用定义和部分的外部生成的变量进行环境操作。在尝试增强之前一切都很好。ERR 1:编译,但无法识别,尽管可以工作 ERR 2 到 4:。令人 惊讶的是,动态设置节点样式不会造成任何问题。contenttikzpicture#1\rnTikZ_showTree:nstylecontentlinebreakstikztikzpicturetikzpicture\_tlstylecontent
subscriptssuperscript
!Use of \@newline doesn't match its definition

\documentclass{article}
% RN. Sunday 17 December 2017
%=======================
\usepackage[check-declarations]{expl3}
\usepackage{xparse}
\usepackage{tikz}
%-----------------------
\ExplSyntaxOn
\cs_new_protected:Nn \__rn_start_tikzpicture:n 
  {\begin{tikzpicture}[#1]}
\cs_generate_variant:Nn \__rn_start_tikzpicture:n { V }

\tl_new:N \l_rn_auxOne_tl
\tl_new:N \l_rn_auxTwo_tl
\tl_new:N \l_rn_auxThree_tl

\tl_new:N \g_rn_tikzStyle_FR_tl
\cs_new:Npn \rn_tikzStyle_FR:n #1
%  PARAMETERS #1: dummy variable 
%  RETURNS \g_rn_tikzStyle_FR_tl for plugging into the tikzpicture     environment
  {
\group_begin:
    \tl_gset:Nx \g_rn_tikzStyle_FR_tl {level~distance=15mm,
    every~node/.style={rectangle,draw,inner~sep=2pt,align=left},
    level 1/.style={sibling~distance=50mm},
    level 2/.style={sibling~distance=25mm}}  
\group_end:
  }  %  \rn_tikzStyle_FR:n

\tl_new:N \g_rn_tikzContent_FR_tl
\cs_new:Npn \rn_tikzContent_FR:n #1
    %  RETURNS \g_rn_tikzContent_FR_tl for plugging into the tikzpicture environment
  {
\group_begin:
    % the nodes are visited post-order to generate tikzContent:
    % visiting node key = 6:    
    \tl_gput_right:Nx \g_rn_tikzContent_FR_tl {child[missing]}
    \tl_set:Nx \l_rn_auxOne_tl {6}
    \tl_set:Nx \l_rn_auxTwo_tl {child{node{\l_rn_auxOne_tl}}}
    \tl_gput_right:NV \g_rn_tikzContent_FR_tl {\l_rn_auxTwo_tl}
    % visiting node key = 4:
    \tl_set:Nx \l_rn_auxOne_tl {4}
% ERR 1:    \tl_set:Nx \l_rn_auxOne_tl {4:$x_3^2$}
    \tl_set:Nx \l_rn_auxTwo_tl {node{\l_rn_auxOne_tl}}
    \tl_gput_left:NV \g_rn_tikzContent_FR_tl {\l_rn_auxTwo_tl}
    \tl_set:Nx \l_rn_auxThree_tl {\g_rn_tikzContent_FR_tl}
    \tl_gset:Nx \g_rn_tikzContent_FR_tl {child{\l_rn_auxThree_tl}}
    % visiting node key = 10:
    \tl_set:Nx \l_rn_auxOne_tl {10}
% ERR 2:    \tl_set:Nx \l_rn_auxOne_tl {\color{red}10}
%    \tl_set:Nx \l_rn_auxTwo_tl {child{node{\l_rn_auxOne_tl}}}
    \tl_set:Nx \l_rn_auxTwo_tl {child{node[circle,draw,inner~sep=2pt]   {\l_rn_auxOne_tl}}}  % this works well!
    \tl_gput_right:NV \g_rn_tikzContent_FR_tl \l_rn_auxTwo_tl
    % visiting root, key = 8:
    \tl_set:Nx \l_rn_auxOne_tl {8}
% ERR 3   \tl_set:Nx \l_rn_auxOne_tl {8\\BF:+1}
% ERR 4   \tl_set:Nx \l_rn_auxOne_tl {\textbf{8}}
    \tl_set:Nx \l_rn_auxTwo_tl {\exp_not:N \node{\l_rn_auxOne_tl}}
    \tl_gput_left:NV \g_rn_tikzContent_FR_tl \l_rn_auxTwo_tl
\group_end:
  }  %  \rn_tikzContent_FR:n

\cs_new:Npn \rnTikZ_showTree:n #1  
  {
\group_begin:
  \rn_tikzContent_FR:n {#1}
  \rn_tikzStyle_FR:n {2}
  \__rn_start_tikzpicture:V \g_rn_tikzStyle_FR_tl        
    \g_rn_tikzContent_FR_tl ; 
  \end{tikzpicture}
\group_end:
  }  %  \rnTikZ_showTree:n

\NewDocumentCommand\myShowTree{O{5,3,4,7}}
  {
    \rnTikZ_showTree:n {#1}
  }

\ExplSyntaxOff
\begin{document}
\section{Native tikzpicture: Basic}
\begin{tikzpicture}
[level distance=15mm,
every node/.style={rectangle,draw,inner sep=2pt,align=left},
level 1/.style={sibling distance=50mm},
level 2/.style={sibling distance=25mm}]
\node{8}child{node{4}child[missing]child{node{6}}}child{node{10}};
\end{tikzpicture} 

\section{Native tikzpicture: Enhanced node content}
\begin{tikzpicture}
[level distance=15mm,
every node/.style={rectangle,draw,inner sep=2pt,align=left},
level 1/.style={sibling distance=50mm},
level 2/.style={sibling distance=25mm}]
\node{\textbf{8}\\BF:+1\\H:2\\W:4}child{node{4\\$x_3^2$}child[missing]child {node{6\\$\alpha_2$}}}child{node[circle,draw,inner sep=2pt]{\color{red}10}};
\end{tikzpicture} 

\section{`tikzpicture` environment generated}
\myShowTree[8,4,6,10]

\end{document}

答案1

很难理解你在做什么,因为在定义中对参数进行硬编码会浪费时间。

\exp_not:V无论如何,您在传递令牌列表时都需要\tl_set:Nx(或类似),因此提取值但不会进一步扩展。

我标记了% <-- !!更改的行,还有其他一些小的调整要做。一般来说,不应该滥用x-expansion。

\documentclass{article}
% RN. Sunday 17 December 2017
%=======================
\usepackage[check-declarations]{expl3}
\usepackage{xparse}
\usepackage{tikz}
%-----------------------
\ExplSyntaxOn
\cs_new_protected:Nn \__rn_start_tikzpicture:n 
  {\begin{tikzpicture}[#1]}
\cs_generate_variant:Nn \__rn_start_tikzpicture:n { V }

\tl_new:N \l_rn_auxOne_tl
\tl_new:N \l_rn_auxTwo_tl
\tl_new:N \l_rn_auxThree_tl

\tl_new:N \g_rn_tikzStyle_FR_tl
\cs_new_protected:Npn \rn_tikzStyle_FR:n #1 % <--- !!
%  PARAMETERS #1: dummy variable 
%  RETURNS \g_rn_tikzStyle_FR_tl for plugging into the tikzpicture     environment
  {
\group_begin:
    \tl_gset:Nn \g_rn_tikzStyle_FR_tl {level~distance=15mm, % <--- !!
    every~node/.style={rectangle,draw,inner~sep=2pt,align=left},
    level~1/.style={sibling~distance=50mm}, % <--- !!
    level~2/.style={sibling~distance=25mm}} % <--- !!
\group_end:
  }  %  \rn_tikzStyle_FR:n

\tl_new:N \g_rn_tikzContent_FR_tl
\cs_new_protected:Npn \rn_tikzContent_FR:n #1 % <--- !!
    %  RETURNS \g_rn_tikzContent_FR_tl for plugging into the tikzpicture environment
  {
\group_begin:
    % the nodes are visited post-order to generate tikzContent:
    % visiting node key = 6:    
    \tl_gput_right:Nn \g_rn_tikzContent_FR_tl {child[missing]} % <--- !!
    \tl_set:Nn \l_rn_auxOne_tl {6}
    \tl_set:Nx \l_rn_auxTwo_tl {child{node{\exp_not:V \l_rn_auxOne_tl}}} % <--- !!
    \tl_gput_right:NV \g_rn_tikzContent_FR_tl \l_rn_auxTwo_tl % <--- !!
    % visiting node key = 4:
    \tl_set:Nn \l_rn_auxOne_tl {4:$x\sb3^2$} % <--- !!
    \tl_set:Nx \l_rn_auxTwo_tl {node{\exp_not:V \l_rn_auxOne_tl}} % <--- !!
    \tl_gput_left:NV \g_rn_tikzContent_FR_tl \l_rn_auxTwo_tl % <--- !!
    \tl_set_eq:NN \l_rn_auxThree_tl \g_rn_tikzContent_FR_tl % <--- !!
    \tl_gset:Nx \g_rn_tikzContent_FR_tl {child{\exp_not:V \l_rn_auxThree_tl}} % <--- !!
    % visiting node key = 10:
    \tl_set:Nn \l_rn_auxOne_tl {\color{red}10} % <--- !!
    \tl_set:Nx \l_rn_auxTwo_tl {child{node{\exp_not:V \l_rn_auxOne_tl}}} % <--- !!
    \tl_set:Nx \l_rn_auxTwo_tl
     {
      child{node[circle,draw,inner~sep=2pt] {\exp_not:V \l_rn_auxOne_tl}}
     } % <--- !!
    \tl_gput_right:NV \g_rn_tikzContent_FR_tl \l_rn_auxTwo_tl
    % visiting root, key = 8:
    \tl_set:Nn \l_rn_auxOne_tl {8\\BF:+1} % <--- !!
    \tl_set:Nx \l_rn_auxTwo_tl {\exp_not:N \node{\exp_not:V \l_rn_auxOne_tl}} % <--- !!
    \tl_gput_left:NV \g_rn_tikzContent_FR_tl \l_rn_auxTwo_tl
\group_end:
  }  %  \rn_tikzContent_FR:n

\cs_new_protected:Npn \rnTikZ_showTree:n #1   % <--- !!
  {
\group_begin:
  \rn_tikzContent_FR:n {#1}
  \rn_tikzStyle_FR:n {2}
  \__rn_start_tikzpicture:V \g_rn_tikzStyle_FR_tl        
    \g_rn_tikzContent_FR_tl ; 
  \end{tikzpicture}
\group_end:
  }  %  \rnTikZ_showTree:n

\NewDocumentCommand\myShowTree{O{5,3,4,7}}
  {
    \rnTikZ_showTree:n {#1}
  }

\ExplSyntaxOff
\begin{document}
\section{Native tikzpicture: Basic}
\begin{tikzpicture}
[level distance=15mm,
every node/.style={rectangle,draw,inner sep=2pt,align=left},
level 1/.style={sibling distance=50mm},
level 2/.style={sibling distance=25mm}]
\node{8}child{node{4}child[missing]child{node{6}}}child{node{10}};
\end{tikzpicture} 

\section{Native tikzpicture: Enhanced node content}
\begin{tikzpicture}
[level distance=15mm,
every node/.style={rectangle,draw,inner sep=2pt,align=left},
level 1/.style={sibling distance=50mm},
level 2/.style={sibling distance=25mm}]
\node{\textbf{8}\\BF:+1\\H:2\\W:4}child{node{4\\$x_3^2$}child[missing]child {node{6\\$\alpha_2$}}}child{node[circle,draw,inner sep=2pt]{\color{red}10}};
\end{tikzpicture} 

\section{`tikzpicture` environment generated}
\myShowTree[8,4,6,10]

\end{document}

相关内容