标记树形图中的节点

标记树形图中的节点

我正在尝试标记下图中的节点,这样我只需定义红色(作为 x)和蓝色(作为 y),并根据其路径命名子节点(例如 $-x^{2}y$ 是从中心沿红色方向向左移动两次,然后沿 y 方向向上移动一次。)我不知道在 tikz 中是否可行。我还想将其旋转 45 度。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{trees,snakes}
\begin{document}
\pagestyle{empty}
\xdef\mycolor{black}
\xdef\ColorList{{"red","blue"}}
\tikzset{level 1/.style={level distance= 32mm,sibling angle=90},
level 2/.style={level distance= 16mm,sibling angle=90},
level 3/.style={level distance = 8mm,sibling angle=90},
every node/.append style={fill},
set color/.code={\pgfmathtruncatemacro{\index}{mod(#1,2)}
\pgfmathparse{\ColorList[\index]}
\xdef\mycolor{\pgfmathresult}}}
\begin{tikzpicture}[grow cyclic, shape=circle,very thick,level 
distance=13mm,
                cap=round]
\node {} child [set color=\A,color=\mycolor] foreach \A in {0,1,2,3}
{ node {} child [set color=\A+\B+1,color=\mycolor] foreach \B in  {0,1,2}
    { node {} child [set color=\A+\B+\C+2,color=\mycolor] foreach \C in  {0,1,2}
        { node {} }
    }
};
\end{tikzpicture}


\end{document}  

在此处输入图片描述

答案1

是的,有可能。第二次编辑:我忽略了减号在指数之前x而不是指数中的事实。非常感谢 Vinzza 指出这一点!这里有一个修复了这个问题的版本,但它非常令人困惑,因为您无法-在不丢失信息的情况下将其分解出来。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{trees,snakes}
\begin{document}
\pagestyle{empty}
\xdef\mycolor{black}
\xdef\ColorList{{"red","blue"}}
\tikzset{level 1/.style={level distance=32mm,sibling angle=90},
level 2/.style={level distance=16mm,sibling angle=90},
level 3/.style={level distance =8mm,sibling angle=90},
every node/.append style={fill},
my color/.code={\pgfmathparse{\ColorList[mod(#1,2)]}
\pgfkeysalso{/tikz/color/.expanded=\pgfmathresult}},
set label/.code n args={3}{% x
\pgfmathtruncatemacro{\myexpXA}{cos(180+#1*90)
+ifthenelse(#2==-1,0,cos(90+#1*90+#2*90))
+ifthenelse(#3==-1,0,cos(#1*90+#2*90+#3*90))}
\pgfmathtruncatemacro{\mySignXA}{sign(\myexpXA)}
\ifnum\mySignXA<0
\def\Xsign{-}
\else
\def\Xsign{}
\fi
\pgfmathtruncatemacro{\myexpX}{abs(\myexpXA)}
\ifcase\myexpX
\def\myX{}
\or
\def\myX{(\Xsign x)}
\or
\def\myX{(\Xsign x^2)}
\or
\def\myX{(\Xsign x^3)}
\fi
% y
\pgfmathtruncatemacro{\myexpYA}{sin(180+#1*90)
+ifthenelse(#2==-1,0,sin(90+#1*90+#2*90))
+ifthenelse(#3==-1,0,sin(#1*90+#2*90+#3*90))
}
\pgfmathtruncatemacro{\mySignYA}{sign(\myexpYA)}
\ifnum\mySignYA<0
\def\Ysign{-}
\else
\def\Ysign{}
\fi
\pgfmathtruncatemacro{\myexpY}{abs(\myexpYA)}
\ifcase\myexpY
\def\myY{}
\or
\def\myY{(\Ysign y)}
\or
\def\myY{(\Ysign y^2)}
\or
\def\myY{(\Ysign y^3)}
\fi
\xdef\mylabel{\myX \myY}}}
\begin{tikzpicture}[grow cyclic,rotate=-45,shape=circle,very thick,level 
distance=13mm,cap=round]
\node {} child [my color=\A]  foreach \A in {0,1,2,3}
{ node[set label={\A}{-1}{-1},label=45:{$\mylabel$}] {} 
child [my color=\A+\B+1] foreach \B in  {0,1,2}
    { node[set label={\A}{\B}{-1},label={[font=\small,xshift=2pt,yshift=9pt]-135:$\mylabel$}] {} child [my color=\A+\B+\C+2] foreach \C in  {0,1,2}
        { node[set label={\A}{\B}{\C},
        label={[font=\tiny,yshift={-sin((\A+\B+\C)*90)*11pt},
        xshift={-cos((\A+\B+\C)*90)*2.5pt}]{(\A+\B+\C)*90}:$\mylabel\vphantom{x^2}$}] {} }
    }
};
\end{tikzpicture}
\end{document}  

在此处输入图片描述

第一次编辑:我错过了Vinzza 的评论当我写这个答案时。我使用了那里建议的旋转键(尽管符号不同),并且也同意标签不是唯一的。另一方面,我无法看到那里模数算法的意义。

至于问题,我需要您对标签的意见。如果我使用您提供的尺寸,它们会重叠(如果以正常尺寸打印)。在这里,我通过缩小标签字体来解决问题,其他方法包括放大图表、将它们移近项目符号和/或使用图钉。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{trees,snakes}
\begin{document}
\pagestyle{empty}
\xdef\mycolor{black}
\xdef\ColorList{{"red","blue"}}
\tikzset{level 1/.style={level distance=32mm,sibling angle=90},
level 2/.style={level distance=16mm,sibling angle=90},
level 3/.style={level distance =8mm,sibling angle=90},
every node/.append style={fill},
my color/.code={\pgfmathparse{\ColorList[mod(#1,2)]}
\pgfkeysalso{/tikz/color/.expanded=\pgfmathresult}},
set label/.code n args={3}{%
\pgfmathtruncatemacro{\myexpXA}{cos(180+#1*90)
+ifthenelse(#2==-1,0,cos(90+#1*90+#2*90))
+ifthenelse(#3==-1,0,cos(#1*90+#2*90+#3*90))}
\pgfmathtruncatemacro{\myexpYA}{sin(180+#1*90)
+ifthenelse(#2==-1,0,sin(90+#1*90+#2*90))
+ifthenelse(#3==-1,0,sin(#1*90+#2*90+#3*90))
}
\xdef\mylabel{x^{\myexpXA}y^{\myexpYA}}}}
\begin{tikzpicture}[grow cyclic,rotate=-45,shape=circle,very thick,level 
distance=13mm,cap=round]
\node {} child [my color=\A]  foreach \A in {0,1,2,3}
{ node[set label={\A}{-1}{-1},label=45:{$\mylabel$}] {} 
child [my color=\A+\B+1] foreach \B in  {0,1,2}
    { node[set label={\A}{\B}{-1},label={[font=\small,xshift=2pt,yshift=2pt]-135:$\mylabel$}] {} child [my color=\A+\B+\C+2] foreach \C in  {0,1,2}
        { node[set label={\A}{\B}{\C},
        label={[font=\tiny,yshift={-sin((\A+\B+\C)*90)*11pt},
        xshift={-cos((\A+\B+\C)*90)*2.5pt}]{(\A+\B+\C)*90}:$\mylabel$}] {} }
    }
};
\end{tikzpicture}
\end{document}  

在此处输入图片描述

相关内容