在循环中,按序列/列表收集 tikz 节点,然后将其与 `fit=` 选项一起使用

在循环中,按序列/列表收集 tikz 节点,然后将其与 `fit=` 选项一起使用

该宏\CAMKIIRING绘制了一个带有球体的六边形。其中一些是蓝色的,其余的是红色的。我收集红色节点listA并传递listAfit=参数。到目前为止,我尝试了许多与数组相关的东西,但都没有奏效。有没有一个干净的解决方案?

\newcommand{\CAMKIIRING}[6]
{
    %% listA = Some list like constainer 
    \pgfmathsetmacro{\theta}{360/#6};
    \node[] (a) at (10,10) {$\theta$};
    \foreach \i in {1,...,#6}
    {
        \IfSubStr {#4} {\i}
        {
            \node[ball color=red,draw,circle,minimum size=#5cm] (r\i) at ($(\i*60:#5)+(#2,#3)$) {};
            %% Only add this node named (r\i) to listA.
        }
        {
            \node[ball color=blue,circle,minimum size=#5cm] (r\i) at ($(\i*60:#5)+(#2,#3)$) {}; 
        }
    };
    %% Often fit is used as following 
    % \node[circle,fit=(r0) (r1) (r4)] (#1) {};
    %% Use listA in fit= .
    %\node[circle,fit=listA ] (#1) {};
}
\begin{document}

\begin{tikzpicture}[scale=1
    , every node/.style={}
    ]
    \CAMKIIRING{camkii0}{1}{1}{42}{1}{6}; % 42 means 4 and 2 are blue.
\end{tikzpicture}    

\end{document}

答案1

您可以定义一个宏来包含列表,并\xdef在循环中使用它来更新它。看一下这个优秀的答案安德烈·维赫罗夫针对您的问题实施:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{fit}
\usepackage{xstring}
\newcommand{\CAMKIIRING}[6]
{
    %% listA = Some list like constainer 
    \pgfmathsetmacro{\theta}{360/#6};
    \node[] (a) at (10,10) {$\theta$};
    \def\fitlist{}
    \foreach \i in {1,...,#6}
    {
        \IfSubStr {#4} {\i}
        {
            \node[ball color=red,draw,circle,minimum size=#5cm] (r\i) at ($(\i*60:#5)+(#2,#3)$) {};
            \xdef\fitlist{\fitlist(r\i)}
            %% Only add this node named (r\i) to listA.
        }
        {
            \node[ball color=blue,circle,minimum size=#5cm] (r\i) at ($(\i*60:#5)+(#2,#3)$) {}; 
        }
    };

    %% Use listA in fit= .
    \node[draw,circle,fit=\fitlist] (#1) {};
}
\begin{document}

\begin{tikzpicture}[scale=1
    , every node/.style={}
    ]
    \CAMKIIRING{camkii0}{1}{1}{42}{1}{5};
\end{tikzpicture}    

\end{document}

圆圈

相关内容