1

I'm trying create a command that will be used in a barycentric cs:.

I will use the barycentric cs a lot of time to get a node the middle of others, so I'm trying to make it less painfull for myself. It will also be used in another command that uses a list as its parameter.

Let's say I have this code:

\documentclass[border=5mm]{standalone}

\usepackage{tikz}

\usetikzlibrary{matrix}
\usetikzlibrary{positioning}

\begin{document}
    \tikzset{
        basic/.style={
            draw, 
            rounded corners=2pt, 
            thick, 
            text width=8em, 
            align=flush center, 
            node distance=2em
        }
    }
    \begin{tikzpicture}
        \matrix[row sep=2em, column sep=4em, every node/.style={basic}] {
            \node(a){text}; & \node(c){text}; \\
            \node(b){text}; & \node(d){text}; \\
            % Loads of other nodes
        };
    \end{tikzpicture}
\end{document}

I'm trying to create a command so that, instead of using \node (bc#) at (barycentric cs:a=1,b=1,c=1,d=1,<...>) {text};, I only have to write \node (x) at (baricentric cs:\listforbarycentrics{a,b,c,d}) {text};.

I tried with this command code:

\newcommand{\listforbarycentrics}[1]{\foreach \n in {#1}{
        \n=1,
    };
}

However, it doesn't works. It may be because a comma at the end of the list in the barycentric cs: (like thisbarycentric cs:a=1,b=1,c=1,d=1,) creates an error, however I'm not sure.

  • Your strategy works in principle if you do something like \newcommand{\listforbarycentrics}[1]{\foreach \n [count=\m] in {#1}{ \ifnum\m=1 \xdef\mybary{\n=1} \else \xdef\mybary{\mybary,\n=1} \fi}} \listforbarycentrics{a,b,c,d} \node (x) at (barycentric cs:\mybary) {text}; but I would use a style for that. – marmot Mar 19 at 23:49
3

Something like this?

\documentclass[border=5mm]{standalone}

\usepackage{tikz}

\usetikzlibrary{matrix}
\usetikzlibrary{positioning}

\begin{document}
    \tikzset{
        basic/.style={
            draw, 
            rounded corners=2pt, 
            thick, 
            text width=8em, 
            align=flush center, 
            node distance=2em
        },
        barycentric setup/.code={\foreach \X [count=\Y] in {#1}
        {\ifnum\Y=1
         \xdef\baryarg{\X=1}
        \else
         \xdef\baryarg{\baryarg,\X=1}
        \fi}},
        barycentric list/.style={barycentric setup={#1},insert path={%
        (barycentric cs:\baryarg)}}
    }
    \begin{tikzpicture}
        \matrix[row sep=2em, column sep=4em, every node/.style={basic}] {
            \node(a){text}; & \node(c){text}; \\
            \node(b){text}; & \node(d){text}; \\
            % Loads of other nodes
        };
        \path[barycentric list={a,b,c,d}] node {center}; 
    \end{tikzpicture}
\end{document}

enter image description here

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.