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:
- 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.
- 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.
- 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:
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:
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}
! Missing number, treated as zero.
) and there are at least two typos in it (e. g. docummentclass or documment).cores = colors
?cores=colors
. this is in portuguese. i'll add the comment in the code