5

I'm trying to make too much node connections to have the same color. I tried to create a list of colors and another of nodes, but the list of nodes doesn't work. Here is my non-MWE:

\documentclass[tikz]{standalone}
\begin{document}
\def\cores{{"0.70 0.00 0.00","0.00 0.70 0.00"}}%cores=colors. the list of collors
\def\nodenames{(no1),(no4)}

\newcommand{\nodefrom}[1]{
\pgfmathparse{\cores[#1]};
\definecolor{cor}{rgb}{\pgfmathresult};
\draw[very thick, cor] (\nodenames[#1])}

\begin{tikzpicture}
    \node (\nodenames[0]) at (0,-1){no1};
    \node (no2) at (1,-1){no2};
    \node (no3) at (0,1){no3};
    \node (\nodenames[1] at (1,1){no4};

    \nodefrom[0]--(no2);
    \nodefrom[0]--(no3);
    \nodefrom[1]--(no2);
    \nodefrom[1]--(no3);
\end{tikzpicture}
\end{document}
4
  • Welcome. Your code does not compile (! Missing number, treated as zero.) and there are at least two typos in it (e. g. docummentclass or documment). Commented Jan 9, 2017 at 19:12
  • Thanks! I don't know how to make it run. the ' \nodenames' is supposed to be a coordinate list based for nodes, but I don't know how to create one for later access. I'll change the code to be clear, but won't compile again Commented Jan 9, 2017 at 19:18
  • Ok. At least I do not fully understand what you try to achieve. Maybe the others here understand it better. Is cores = colors? Commented Jan 9, 2017 at 19:23
  • yes. cores=colors. this is in portuguese. i'll add the comment in the code Commented Jan 10, 2017 at 1:37

2 Answers 2

4

Note see update below for a better (imho) solution, and Update 2 for an answer in the spirit of what the OP requested, but fixing one bug present in the first version.

Original answer

Answering your question about how to create the list of nodes, several points to note:

  1. To use TikZ parser to extract elements of a list (or array), you have to put double braces around the lists, as you did in your \cores list.
  2. I think the code is simpler if your list of namenodes contains only names of nodes, without parenthesis. Moreover, you put again the parenthesis when you extract the element, as in \draw[...] (\nodenames[#1]); so they are not required as part of the node name.
  3. To invoke your \nodefrom macro, the argument should be given in curly braces, not brackets, i.e: \nodefrom{0} for example.

Using these points your original code is fixed like follows:

\documentclass[tikz]{standalone}
\begin{document}
\def\cores{{"0.70 0.00 0.00","0.00 0.70 0.00"}}%cores=colors. the list of collors
\def\nodenames{{no1,no4}}

\newcommand{\nodefrom}[1]{
\pgfmathparse{\cores[#1]};
\definecolor{cor}{rgb}{\pgfmathresult};
\draw[very thick, cor] (\nodenames[#1])}

\begin{tikzpicture}
    \node (\nodenames[0]) at (0,-1){no1};
    \node (no2) at (1,-1){no2};
    \node (no3) at (0,1){no3};
    \node (\nodenames[1]) at (1,1){no4};

    \nodefrom{0}--(no2);
    \nodefrom{0}--(no3);
    \nodefrom{1}--(no2);
    \nodefrom{1}--(no3);
\end{tikzpicture}
\end{document}

which produces:

Result

Update

However, I understand that you want to use lists or arrays of node names in order to loop over them somehow. In this case, I would not use lists, but instead take advantage of \foreach powerful machinery.

This is your code, refactorized to use \foreach loops, removing the need of lists of colors and lists of nodes (and making unnecessary too the macro \nodefrom). The result is much more TikZ idiomatic:

\documentclass[tikz]{standalone}
\begin{document}
\definecolor{redish}{rgb}{0.7 0.0 0.0}
\definecolor{greenish}{rgb}{0.0 0.7 0.0}
\begin{tikzpicture}
    \node (no1) at (0,-1){no1};
    \node (no2) at (1,-1){no2};
    \node (no3) at (0,1){no3};
    \node (no4) at (1,1){no4};
    \foreach \from/\mycolor/\finals in {no1/redish/{no3,no2},no4/greenish/{no3,no2}}
        \foreach \to in \finals
            \draw[very thick, \mycolor] (\from) -- (\to);
\end{tikzpicture}
\end{document}

Note the argument passed to the first \foreach. It consists on a list of comma separated values, each one being a sequence of /-separated elements. The first one is the node from which the connections will be made. The second one is the color of these connections. The third one is a comma separated lists of the destination nodes (enclosed in curly braces). This second list is used in a nested \foreach loop which finally draws the connections using the same color and style.

The result is the same than in your code:

Result

Update 2

The OP pointed out in a comment that the code provided in the original answer didn't work as expected if, for example, first node is defined with \node (no1) ... instead of \node (\nodelist[0])...

Indeed, there is a bug in the original answer. Inside the macro \nodefrom there is a \draw[...] (\nodenames[#1]). I (wrongly) assumed that TikZ would parse the (\nodenames[#1]) part and produce (no1), but this not the case. The macro \nodenames is simply expanded and the result is ({no1,no4}[0]), so a node with the weird name {no1,no4}[0] is used to draw. This node exists if it was previously created with \node (\nodenames[0]) (since the same expansion takes place), but not if \node (no1) is used.

To fix this problem two modifications are needed. First, the list of nodenames requires double quotes around each node name. Second, the name of an individual node has to be extracted from the list using pgf math parser. I prefer to use \pgfmathsetmacro instead of \pgfmathparser to be able to extract the required information to a custom macro.

This is the correct code:

\documentclass[tikz]{standalone}
\begin{document}
\def\colors{{"0.70 0.00 0.00","0.00 0.70 0.00"}}
\def\nodenames{{"no1","no4"}}

\newcommand{\nodefrom}[1]{
\pgfmathsetmacro{\mycolor}{\colors[#1]}
\pgfmathsetmacro{\start}{\nodenames[#1]}
\definecolor{mycolor}{rgb}{\mycolor}
\draw[very thick, mycolor] (\start)}

\begin{tikzpicture}
    \node (no1) at (0,-1){no1};
    \node (no2) at (1,-1){no2};
    \node (no3) at (0,1){no3};
    \node (no4) at (1,1){no4};

    \nodefrom{0}--(no2);
    \nodefrom{0}--(no3);
    \nodefrom{1}--(no2);
    \nodefrom{1}--(no3);
\end{tikzpicture}
\end{document}
2
  • Perfect! just an observation: if I use \node (no1) at (0,-1){no1}; and later \nodefrom{0}--(no2); the code won't compile, it have to be necesseraly \node (\nodenames[0]) at (0,-1){no1};. I'm not going to use loops, but just trying to make the code smarter and simples. the connections are created very before the nodes. Commented Jan 10, 2017 at 12:04
  • @texstarter You are right, there was a bug in the first implementation. See Update 2 for a fix.
    – JLDiaz
    Commented Jan 10, 2017 at 18:41
2

If I understand your question correctly, the following picture is showing what you want to achieve:

Picture

I do not why you want to use a list of nodes. Therefore I removed this inside the following code:

\documentclass[tikz]{standalone}
\usepackage{ifthen}
\begin{document}
\def\colors{{"0.70 0.00 0.00","0.00 0.70 0.00"}}

\newcommand{\nodefrom}[1]{
% decide which color should be used based on the parameter
\ifthenelse{#1=1}{\pgfmathparse{\colors[0]}}{};
\ifthenelse{#1=4}{\pgfmathparse{\colors[1]}}{};

\definecolor{cor}{rgb}{\pgfmathresult};
\draw[very thick, cor] (no#1)
}

\begin{tikzpicture}
    \node (no1) at (0,-1){no1};
    \node (no2) at (1,-1){no2};
    \node (no3) at (0,1){no3};
    \node (no4) at (1,1){no4};

    \nodefrom{1} -- (no2);
    \nodefrom{1} -- (no3);
    \nodefrom{4} -- (no2);
    \nodefrom{4} -- (no3);
\end{tikzpicture}
\end{document}

I am using the ifthen package to determine the right color to use for the given line.

2
  • most of the time i'll need to connect the nodes via non-direct lines, for exemple: \nodefrom{0}--(2,0)--(no2); I'm trying to map the curriculum of my course. each node will be a matter (I don't know the corret word for that). i want to associete each color to a node, so each line from that node will have the specified color. Commented Jan 10, 2017 at 1:42
  • @texstarter I have updated my answer.
    – epR8GaYuh
    Commented Jan 10, 2017 at 7:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .