对 tikz 图中的不同节点使用相同的标签

对 tikz 图中的不同节点使用相同的标签

我正在使用 TikZ 手册的第 5 节“教程:图表作为简单图形”在 LaTeX 中绘制语法图。然而,我面临的一个问题是,我想对不同的节点使用相同的标签,但渲染的图像总是有指向使用该名称的第一个节点的箭头。示例代码如下:

\tikz [>=stealth', black!50, text=black, thick,
   every new ->/.style          = {shorten >=1pt},
   graphs/every graph/.style    = {edges=rounded corners},
   skip loop/.style             = {to path={-- ++(0,#1) -| (\tikztotarget)}},
   hv path/.style               = {to path={-| (\tikztotarget)}},
   vh path/.style               = {to path={|- (\tikztotarget)}},
   nonterminal/.style           = {
     rectangle, minimum size=6mm, very thick, draw=red!50!black!50, top color=white,
     bottom color=red!50!black!20, font=\itshape, text height=1.5ex,text depth=.25ex},
   terminal/.style              = {
     rounded rectangle,  minimum size=6mm, very thick, draw=black!50, top color=white,
     bottom color=black!20, font=\ttfamily, text height=1.5ex, text depth=.25ex},
   shape                        = coordinate
   ]       
\graph [grow right sep, branch down=7mm, simple] {
/ -> "def"[nonterminal] ->[vh path]
{[nodes={yshift=7mm}]
  "resource-type"[terminal] -> "name"[nonterminal], 
  "resource"[terminal]  -> "resource-type.name"[nonterminal] -> "name"[nonterminal],
  "resource-type-attribute"[nonterminal] -> "resource-type.name"[nonterminal],
  "resource-attribute"[nonterminal],
  "resource-type-relationship"[nonterminal] -> "resource-type.name 1"[nonterminal] -> "name"[nonterminal] -> "resource-type.name 2"[nonterminal],
  "resource-relationship"[nonterminal]
} 

};

答案1

您可以在手册其中解释了如何区分和node's textnode's name

"name"[nonterminal]声明一个nonterminal名为的类型节点name,并以name节点内容为节点。如果稍后重复该命令,graphs库不会创建新节点,而是使用之前创建的节点。

如果想要拥有内容相同但名称不同的不同节点,可以使用name1[as=name, nonterminal]which 声明一个名为name1但内容不同的节点name。稍后,您可以使用name2[as=name, nonterminal]which 声明一个name2具有相同内容的新节点 ( )。

我认为你想要的是:

在此处输入图片描述

获取它的代码是:

\documentclass[tikz, border=2mm]{standalone}

\usetikzlibrary{arrows,graphs,shapes}

\begin{document}

\begin{tikzpicture}[>=stealth', black!50, text=black, thick,
   every new ->/.style          = {shorten >=1pt},
   graphs/every graph/.style    = {edges=rounded corners},
   skip loop/.style             = {to path={-- ++(0,#1) -| (\tikztotarget)}},
   hv path/.style               = {to path={-| (\tikztotarget)}},
   vh path/.style               = {to path={|- (\tikztotarget)}},
   nonterminal/.style           = {
     rectangle, minimum size=6mm, very thick, draw=red!50!black!50, top color=white,
     bottom color=red!50!black!20, font=\itshape, text height=1.5ex,text depth=.25ex},
   terminal/.style              = {
     rounded rectangle,  minimum size=6mm, very thick, draw=black!50, top color=white,
     bottom color=black!20, font=\ttfamily, text height=1.5ex, text depth=.25ex},
   shape                        = coordinate
   ]       
\graph [grow right sep, branch down=7mm, simple] {
/ -> "def"[nonterminal] ->[vh path]
{[nodes={yshift=7mm}]
  "resource-type"[terminal] -> "name1"[as=name, nonterminal], 
  "resource"[terminal]  -> "resource-type.name"[nonterminal] -> "name2"[as=name,nonterminal],
  "resource-type-attribute"[nonterminal] -> "resource-type.name2"[as=resource-type.name, nonterminal],
  "resource-attribute"[nonterminal],
  "resource-type-relationship"[nonterminal] -> "resource-type.name 1"[nonterminal] -> "name3"[as=name, nonterminal] -> "resource-type.name 2"[nonterminal],
  "resource-relationship"[nonterminal]
}}; 
\end{tikzpicture}
\end{document}

答案2

一个肮脏的伎俩:添加一些无用的代码来使内容变得不同。例如

%! TeX program = lualatex
\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta, quotes, graphs, graphdrawing}
\usegdlibrary{layered}
\begin{document}
\begin{tikzpicture}[rounded corners, >=Stealth, auto]
  \graph[layered layout, nodes={draw}]{%
    "a" -> "a$ $" -> "$ $a" -> "$ $a$ $";
  };
\end{tikzpicture}
\end{document}

您将看到 4 个,a而不是 1 个a

相关内容