11

Here's what I have created, but how could I access the loop variable \n outside the foreach loop so that I could write sth like this:

\n = \n + 1 % this is definitely not right, and it seems I couldn't reference \n outside foreach loop
\node[element,fill=white,xshift=\n*1cm](l_N\n){...};

instead of hardcoding the value.

This is the complete code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}

\begin{document}

\begin{tikzpicture}[remember picture,
  element/.style={rectangle,draw,fill=red!20,minimum width=1cm,minimum height=1cm},
  outer/.style={circle,draw=green,fill=green!20,thick,inner sep=10pt,minimum size=7cm}
  ]
  \node[outer,draw=green] (A) {
    \begin{tikzpicture}
      \foreach \i [count=\n] in {0, 1, ..., 3}
         \node[element,xshift=\n*1cm](l_N\n){\i};


      \node[element,fill=white,xshift=5*1cm](l_N5){...};
      \node[element,xshift=6cm](l_N9){9};
    \end{tikzpicture}
  };
  \node[outer,draw=green,right=of A] (B) {
    \begin{tikzpicture}
      \foreach \i [count=\n] in {0, 1, ..., 3}
         \node[element,xshift=\n*1cm](r_N\n){\i};

      \node[element,fill=white,xshift=5*1cm](r_N5){...};
      \node[element,xshift=6cm](r_N9){9};
    \end{tikzpicture}
  };
\draw[thick,dashed] (l_N9) -- (r_N1);
\end{tikzpicture}

\end{document}

The result:

enter image description here

2 Answers2

10

One way would be to save the value in a global:

enter image description here

Code:

\documentclass{article}
\usepackage{tikz}

\newcommand*{\LastLoopValue}{No value} \begin{document}

\foreach \i [count=\n] in {0, 2, ..., 14} { \i, \xdef\LastLoopValue{\n}% }

Outside of loop, count=\LastLoopValue \end{document}

Peter Grill
  • 223,288
  • Thanks for the answer. Is it possible to increment \LastLoopValue? For example:Another line count=\LastLoopValue+1 – Albert Netymk May 23 '13 at 12:32
  • 1
    @AlbertNetymk: There are numerous ways to increment values -- there should be a question here that answers that. But, if you don't find it, you should post a new question as incrementing a value is not related to accessing a loop variable outside of the loop. But, one way would be to define \newcommand{\Increment}[1]{\pgfmathparse{#1+1}\pgfmathprintnumber[int detect]{\pgfmathresult}}, and then using \Increment{\LastLoopValue} will provide what you desire (assuming you desire an integer value). – Peter Grill May 23 '13 at 20:23
1

The \foreach loop is fantastic tool, but has the particularity to create and run inside a group. So every action (\def, \clip, ...) that is limited by the scope will not survive outside the loop.

In some situation it is good to know that there are other loops that don't run in a group. For example the xint bundle has such loops. Here is one example :

\documentclass[preview, border=7mm]{standalone}
\usepackage{xinttools}

\newcommand*{\LastLoopValue}{No value}
\newcounter{loopCounter}

\begin{document}

  \xintFor* #1 in {\xintSeq[2]{0}{14}} \do{
    #1, \def\LastLoopValue{#1}
    \stepcounter{loopCounter}
  }

  ... outside of loop, LastLoopValue=\LastLoopValue, loopCounter=\arabic{loopCounter}

\end{document}

enter image description here

Kpym
  • 23,002