2

I would like to iterate over an array and then use the current value of the iterator as an argument to the next for loop. So far my code looks as follows but I can't get the syntax correct.

 \foreach \dest [count=\xi] in {2,...,4}
        \foreach \source in {\xi-1,...,\xi+1}  
            \path (I-\source) edge (H-\dest); % do something with the source and dest values.

For example, in the outer loop the first value would be 2 and therefore the inner loop will go over {2-1,2,2+1}.

In the below image I have manually drawn in lines to illustrate the output, although it doesn't necessarily have to be of different colors. The idea is that each blue node in the first hidden layer is connected to exactly three of the input layer nodes, which is dependant on the index (Note I have missed out one green circle at the end by accident).

enter image description here

  • Could you show us a practical use of this? That way we understand better what you want to achieve. – Alenanno Apr 18 '16 at 11:32
3

If you got trouble getting your data parsed the right way by the foreach-statement, you could do the following. Evaluate the values you need in the outer loop with evaluate and just pass them as the min/max values to the foreach.

\foreach [count=\i,
          evaluate=\i as \ymin using int(\x-1),
          evaluate=\i as \ymax using int(\x+1)] \x in {2,...,4} {
    \foreach \y in {\ymin,...,\ymax} {
        % draw your stuff here
    }
}
  • This is perfect, it was a parse issue. Didn't know about the evaluate statement. – mattdns Apr 18 '16 at 11:50

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.