使用TikZ
和pgf
版本v3.1.9a (3.1.9a)
,我尝试编译以下代码
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{trees,mindmap}
\tikzset{grow cyclic list/.code={%
\def\tikzgrowthpositions{{#1}}%
\foreach \n [count=\i,remember=\i]in {#1}{}%
\let\tikzgrowthpositionscount=\i%
\tikzset{growth function=\tikzgrowcycliclist}}}
\def\tikzgrowcycliclist{%
\pgftransformshift{%
\pgfpointpolar{\tikzgrowthpositions[mod(\the\tikznumberofcurrentchild-1,\tikzgrowthpositionscount)]}%
{\the\tikzleveldistance}}}
\begin{document}
\begin{tikzpicture}[small mindmap, concept color=blue!20,
every child node/.style={concept}]
\node [concept] (languages)
{O} [grow cyclic list={90,0,-45,210}]
child { node {A} }
child { node {B} }
child { node {C}
[clockwise from=0, sibling angle=45]
child { node {C1} }
child { node {C1} }
child { node {C1} }
}
child { node {D} };
\end{tikzpicture}
\end{document}
这是 Mark Wibrow 的回答的一部分这里。但它抛出了以下错误:
\pgfmath@dimen@@ 的参数有一个额外的}。^^Ichild { node {D} };
有人知道如何解决吗?
答案1
问题在于,旧答案利用了\foreach
现已修复的错误功能。
实际情况是,被“记住”的“计数”在 的末尾仍然可用\foreach
,但这是错误的:你会破坏 的含义\i
。如果你在某个节点中有一个重音符号“î”……
有一种更简单的方法来计算列表的元素数量:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees,mindmap}
\ExplSyntaxOn
\cs_set_eq:NN \clistcount \clist_count:n
\ExplSyntaxOff
\tikzset{
grow cyclic list/.code={%
\def\tikzgrowthpositions{{#1}}%
% \foreach \n [count=\i,remember=\i]in {#1}{\global\let\tikzgrowthpositionscount=\i}%
\edef\tikzgrowthpositionscount{\clistcount{#1}}%
\tikzset{growth function=\tikzgrowcycliclist}
}
}
\def\tikzgrowcycliclist{%
\pgftransformshift{%
\pgfpointpolar{%
\tikzgrowthpositions[mod(\the\tikznumberofcurrentchild-1,\tikzgrowthpositionscount)]%
}%
{\the\tikzleveldistance}
}%
}
\begin{document}
\begin{tikzpicture}[
small mindmap,
concept color=blue!20,
every child node/.style={concept}
]
\node [concept] (languages)
{O} [grow cyclic list={90,0,-45,210}]
child { node {A} }
child { node {B} }
child { node {C}
[clockwise from=0, sibling angle=45]
child { node {C1} }
child { node {C1} }
child { node {C1} }
}
child { node {D} };
\end{tikzpicture}
\end{document}
我留下了替代\foreach
方法的评论,但我的建议更有效。
对于“纯钛钾“Z”解决方案
\tikzset{
grow cyclic list/.code={%
\def\tikzgrowthpositions{{#1}}%
\pgfmathsetmacro\tikzgrowthpositionscount{dim({#1})}%
\tikzset{growth function=\tikzgrowcycliclist}
}
}
答案2
我认为\tikzgrowthpositionscount
需要在循环内设置\foreach
,并且应将其设为全局变量,以便在循环外可访问。该remember
选项仅存储最后一个项目的值,但不会使变量在 for 循环外可访问(可能在某个时间点已更改)。
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{trees,mindmap,calc}
\tikzset{grow cyclic list/.code={%
\def\tikzgrowthpositions{{#1}}%
\foreach \n [count=\i]in {#1}{%
\global\let\tikzgrowthpositionscount=\i%
}%
\tikzset{growth function=\tikzgrowcycliclist}}}
\def\tikzgrowcycliclist{%
\pgftransformshift{%
\pgfpointpolar{\tikzgrowthpositions[mod(\the\tikznumberofcurrentchild-1,\tikzgrowthpositionscount)]}%
{\the\tikzleveldistance}}}
\begin{document}
\begin{tikzpicture}[small mindmap, concept color=blue!20,
every child node/.style={concept}]
\node [concept] (languages)
{O} [grow cyclic list={90,0,-45,210}]
child { node {A} }
child { node {B} }
child { node {C}
[clockwise from=0, sibling angle=45]
child { node {C1} }
child { node {C1} }
child { node {C1} }
}
child { node {D} };
\end{tikzpicture}
\end{document}