问题
我在 tikz 中有一棵圆形生长的树。由于此图形将变得非常复杂,因此我想切换到森林。我grow
在森林中找到了 - 选项,但我无法让它做我想要的事情。
如果 forest 不是适合此目的的正确软件包,我当然也会使用常规 tikz 中的解决方案。
目标输出
节点形状目前是次要的。与下图相反,我希望节点均匀分布在中心/根部周围;基本上,节点应该均匀分布在中心周围的几个“轨道”上,但仍与相应的父节点相连。下图没有将节点均匀分布在中心周围,而是分布在父节点周围。
补充:另外,我想使用循环来创建节点,这样我就可以轻松改变创建的节点数量。这可能吗?
来源(如果需要)
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{trees, shapes}
\begin{document}
\pagestyle{empty}
\tikzstyle{level 1}=[sibling angle=90, shape = rectangle, minimum size=1.5em]
\tikzstyle{level 2}=[sibling angle=45, shape = rectangle, minimum size=1em]
\tikzstyle{level 3}=[sibling angle=22.5, shape = rectangle, minimum size=0.5em]
\tikzstyle{every node}=[fill]
\tikzstyle{edge from parent}=[draw, very thin]
\begin{tikzpicture}[
grow cyclic,
shape = circle,
level distance=3em,
triangle/.style = {fill=blue!20, regular polygon, regular polygon sides=3 },
border rotated/.style = {shape border rotate=180}
]
\node[triangle, border rotated, minimum size=3em] {} child [color=\A] foreach \A in {black, black, black, black}
{ node {} child [color=\B] foreach \B in {black, black, black}
{ node {} child [color=\C] foreach \C in {black, black, black}
{ node {} }
}
};
\end{tikzpicture}
\end{document}
答案1
我真的只是从https://tex.stackexchange.com/a/539395,而且很可能是运气好才成功。虽然我不完全明白发生了什么,但我认为有几件事是正确的:
- 与上面的评论相反
mindmap
的是不是我们需要做的只是修改所谓的增长函数。 - 需要编译多次才能得到最终结果,好像每一级都需要添加一次编译。
代码:
\documentclass[tikz,border=9]{standalone}
\usetikzlibrary{trees,decorations,shapes.geometric}
\makeatletter
\newcount\tikzcountchildi
\newcount\tikzcountchildii
\newcount\tikzcountchildiii
\newcount\tikzcountchildiv
\newcount\tikzcountchildv
\newcount\tikzcountchildvi
\tikzset{garlic growth/.style={growth function=\tikz@grow@garlic,
/tikz/mmap/name=#1,mmap/#1/.is family,
mmap/initialize counts,
execute at end scope={%
\tikz@mmap@store@aux{n-i}{\the\tikzcountchildi}%
\tikz@mmap@store@aux{n-ii}{\the\tikzcountchildii}%
\tikz@mmap@store@aux{n-iii}{\the\tikzcountchildiii}%
\tikz@mmap@store@aux{n-iv}{\the\tikzcountchildiv}%
\tikz@mmap@store@aux{n-v}{\the\tikzcountchildv}%
\tikz@mmap@store@aux{n-vi}{\the\tikzcountchildvi}}},
mmap/.cd,initialize counts/.code={\tikzcountchildi=0%
\tikzcountchildii=0%
\tikzcountchildiii=0%
\tikzcountchildiv=0%
\tikzcountchildv=0%
\tikzcountchildvi=0%
}
}
\def\tikz@mmap@store@aux#1#2{%
\immediate\write\@mainaux{\string\expandafter\xdef\noexpand\csname pgfk@/tikz/mmap/\pgfkeysvalueof{/tikz/mmap/name}/#1\string\endcsname{#2}}}%
\def\tikz@mmap@get@from@aux#1#2{%
\ifcsname pgfk@/tikz/mmap/\pgfkeysvalueof{/tikz/mmap/name}/#1\endcsname
\edef#2{\csname pgfk@/tikz/mmap/\pgfkeysvalueof{/tikz/mmap/name}/#1\endcsname}%
\else
\edef#2{0}%
\fi
}
\newcount\tikzcountgrandchild
\def\tikz@grow@garlic{%
\pgfmathsetmacro{\pgfutil@tempa}{\pgfkeysvalueof{/tikz/mmap/overall rotation}}%
\ifcase\tikztreelevel
\or
\global\advance\tikzcountchildi by1\relax%
\tikz@mmap@get@from@aux{n-i}{\myni}%
\tikz@mmap@get@from@aux{n-ii}{\mynii}%
\tikz@mmap@get@from@aux{n-1-\the\tikzcountchildi}{\mynall}%
\tikz@mmap@get@from@aux{p-1-\the\tikzcountchildi}{\mypi}%
\ifnum\mynii>0
\pgfmathsetmacro{\pgfutil@tempa}{\pgfutil@tempa+\pgfkeysvalueof{/tikz/mmap/sign}%
*(\pgfkeysvalueof{/tikz/mmap/child weight}*(\mypi)*360/\mynii+%
(1-\pgfkeysvalueof{/tikz/mmap/child weight})*(\tikzcountchildi-1)*360/\myni)}%
\fi
\or
\global\advance\tikzcountchildii by1\relax%
\tikz@mmap@get@from@aux{n-ii}{\mynii}%
\tikz@mmap@get@from@aux{n-1-1}{\mynall}%
\ifnum\mynii>0
\pgfmathsetmacro{\pgfutil@tempa}{\pgfutil@tempa+\pgfkeysvalueof{/tikz/mmap/sign}%
*(\tikzcountchildii-1-\mynall/2)*360/\mynii}%
\fi
\ifnum\tikznumberofcurrentchild=1\relax
\tikz@mmap@store@aux{n-1-\the\tikzcountchildi}{\the\tikznumberofchildren}%
\tikz@mmap@store@aux{p-1-\the\tikzcountchildi}{\the\numexpr\tikzcountchildii-1}%
\tikz@mmap@store@aux{a-1-\the\tikzcountchildi-\the\tikzcountchildii}{\pgfutil@tempa}%
\fi
\or
\global\advance\tikzcountchildiii by1\relax%
\ifnum\tikznumberofcurrentchild=1\relax
\tikz@mmap@store@aux{n-1-\the\tikzcountchildi-\the\tikzcountchildii}{\the\tikznumberofchildren}%
\tikz@mmap@store@aux{p-1-\the\tikzcountchildi-\the\tikzcountchildii}{\the\numexpr\tikzcountchildiii-1}%
\fi
\tikz@mmap@get@from@aux{n-iii}{\myniii}%
\tikz@mmap@get@from@aux{a-1-1-1}{\bettera}%
\tikz@mmap@get@from@aux{n-1-1-1}{\mynall}%
\ifdim\bettera pt=0pt\relax
\else
\pgfmathsetmacro{\pgfutil@tempa}{\bettera}%
\fi
\ifnum\myniii>0
\pgfmathsetmacro{\pgfutil@tempa}{\pgfutil@tempa+\pgfkeysvalueof{/tikz/mmap/sign}%
*(\tikzcountchildiii-1-\mynall/2)*360/\myniii}%
\fi
\or
\global\advance\tikzcountchildiv by1\relax%
\tikz@mmap@get@from@aux{n-iv}{\myniv}%
\ifnum\myniv>0
\pgfmathsetmacro{\pgfutil@tempa}{\pgfutil@tempa+\pgfkeysvalueof{/tikz/mmap/sign}*(\tikzcountchildiv-1)*360/\myniv}%
\fi
\or
\tikz@mmap@get@from@aux{n-v}{\mynv}%
\pgfmathsetmacro{\pgfutil@tempa}{\pgfutil@tempa+\pgfkeysvalueof{/tikz/mmap/sign}*(\tikzcountchildv-1)*360/\mynv}%
\ifnum\mynv>0
\global\advance\tikzcountchildv by1\relax%
\fi
\or
\global\advance\tikzcountchildvi by1\relax%
\tikz@mmap@get@from@aux{n-vi}{\mynvi}%
\ifnum\myvi>0
\pgfmathsetmacro{\pgfutil@tempa}{\pgfutil@tempa+(\tikzcountchildvi-1)*360/\mynvi}%
\fi
\fi
\pgftransformreset%
\pgftransformshift{\pgfpoint{\pgfkeysvalueof{/tikz/mmap/overall xshift}}%
{\pgfkeysvalueof{/tikz/mmap/overall yshift}}}%
\pgftransformrotate{\pgfutil@tempa}%
\pgftransformxshift{\the\tikzleveldistance}%
}
\makeatother
\begin{document}
\tikzset{mmap/.cd,
name/.initial=undef,
overall rotation/.initial=0,
overall xshift/.initial=0pt,
overall yshift/.initial=0pt,
sign/.initial=1,
child weight/.initial=0.5,
/tikz/.cd,
Xshift/.style={xshift=#1,mmap/overall xshift=#1},
Yshift/.style={yshift=#1,mmap/overall yshift=#1},
branch color/.style={
concept color=#1!80,ball color=#1!50,
every child/.append style={concept color=#1!50},
}
}
\begin{tikzpicture}[garlic growth=A,mmap/child weight=0,
mmap/overall rotation=15,mmap/sign=-1,
shape = circle,
level 1/.style={shape = rectangle, minimum size=1.5em,
level distance=1.2cm,
},
level 2/.style={shape = rectangle, minimum size=1em,
level distance=2.4cm,
},
level 3/.style={shape = rectangle, minimum size=0.5em,
level distance=3.6cm,sibling angle=40,
},
edge from parent/.style={draw, very thin},nodes=fill,
level distance=3em,
triangle/.style = {fill=blue!20, regular polygon, regular polygon sides=3 },
border rotated/.style = {shape border rotate=180}
]
\node[triangle, border rotated, minimum size=3em] {} child [color=\A] foreach \A in {black, black, black, black}
{ node {} child [color=\B] foreach \B in {black, black, black}
{ node {} child [color=\C] foreach \C in {black, black, black}
{ node {} }
}
};
\end{tikzpicture}
\end{document}