2

The macro \CAMKIIRING draws a hexagon with spheres. Some of them are colored blue and rest red. I collect red nodes in listA and pass listA to fit= parameter. So far, I tried many array related things, none worked. Is there a clean solution to this?

\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}
4

You can define a macro to contain the list and use \xdef to update it in the loop. Take a look at this excellent answer from Andrey Vihrov. Implemented for your question:

\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}

circled circles

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.