variable for loop

View: New views
6 Messages — Rating Filter:   Alert me  

variable for loop

by GGBBRR :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I want to do something really simple namely drawing a, say, 5 by 5 grid with numbers 1 to 25 in each square. I thought that would be possible with a nested for loop like this:

\draw (0,0) grid (5,5);
\foreach \i in {0,...,4}
  \foreach \j in {0,...,4}
      \node at (0.5+\j, 4.5-\i ) { 5*\i + \j+1 };

but that does not work since { 5*\i + \j+1 } is not evaluated. I have been searching around but cannot find out how to do this. Closest I got was this:

\draw (0,0) grid (5,5);
\foreach \i in {0,...,4}
  \foreach \j in {0,...,4}
      \node at (0.5+\j, 4.5-\i ) { \pgfmathparse{ 5*\i + \j+1}\pgfmathresult}

But then I get numbers ending with a decimal .0 (i.e 1.0, 2.0, etc.) and I just want integers. Of course I can do this by just having 25 lines printing the numbers, but is there a way to do this in a nested loop?  Can I  perhaps somehow define a variable in the loop and increase it each time I run through the loop?


Re: variable for loop

by Zerothi :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Gerben

What you need is the int function. You should thus use:
\pgfmathparse{int(5*\i +\j+1)}
instead. This will provide with your result.

Regards Nick

2009/10/2 GGBBRR <gerben.rotman@...>

I want to do something really simple namely drawing a, say, 5 by 5 grid with
numbers 1 to 25 in each square. I thought that would be possible with a
nested for loop like this:

\draw (0,0) grid (5,5);
\foreach \i in {0,...,4}
 \foreach \j in {0,...,4}
     \node at (0.5+\j, 4.5-\i ) { 5*\i + \j+1 };

but that does not work since { 5*\i + \j+1 } is not evaluated. I have been
searching around but cannot find out how to do this. Closest I got was this:

\draw (0,0) grid (5,5);
\foreach \i in {0,...,4}
 \foreach \j in {0,...,4}
     \node at (0.5+\j, 4.5-\i ) { \pgfmathparse{ 5*\i +
\j+1}\pgfmathresult}

But then I get numbers ending with a decimal .0 (i.e 1.0, 2.0, etc.) and I
just want integers. Of course I can do this by just having 25 lines printing
the numbers, but is there a way to do this in a nested loop?  Can I  perhaps
somehow define a variable in the loop and increase it each time I run
through the loop?


--
View this message in context: http://www.nabble.com/variable-for-loop-tp25708820p25708820.html
Sent from the pgf-users mailing list archive at Nabble.com.


------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
pgf-users mailing list
pgf-users@...
https://lists.sourceforge.net/lists/listinfo/pgf-users


------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
pgf-users mailing list
pgf-users@...
https://lists.sourceforge.net/lists/listinfo/pgf-users

Re: variable for loop

by Mark Wibrow-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

Slightly more succinct, although perhaps less readable:

\foreach \n in {1,...,25}
        \node [shift={(0.5,4.5)}] at ({mod(\n-1,5)}, {-floor((\n-1)/5)}) {\n};

Regards

Mark

2009/10/2 Nick Papior Andersen <nickpapior@...>:

> Hi Gerben
> What you need is the int function. You should thus use:
> \pgfmathparse{int(5*\i +\j+1)}
> instead. This will provide with your result.
> Regards Nick
>
> 2009/10/2 GGBBRR <gerben.rotman@...>
>>
>> I want to do something really simple namely drawing a, say, 5 by 5 grid
>> with
>> numbers 1 to 25 in each square. I thought that would be possible with a
>> nested for loop like this:
>>
>> \draw (0,0) grid (5,5);
>> \foreach \i in {0,...,4}
>>  \foreach \j in {0,...,4}
>>      \node at (0.5+\j, 4.5-\i ) { 5*\i + \j+1 };
>>
>> but that does not work since { 5*\i + \j+1 } is not evaluated. I have been
>> searching around but cannot find out how to do this. Closest I got was
>> this:
>>
>> \draw (0,0) grid (5,5);
>> \foreach \i in {0,...,4}
>>  \foreach \j in {0,...,4}
>>      \node at (0.5+\j, 4.5-\i ) { \pgfmathparse{ 5*\i +
>> \j+1}\pgfmathresult}
>>
>> But then I get numbers ending with a decimal .0 (i.e 1.0, 2.0, etc.) and I
>> just want integers. Of course I can do this by just having 25 lines
>> printing
>> the numbers, but is there a way to do this in a nested loop?  Can I
>>  perhaps
>> somehow define a variable in the loop and increase it each time I run
>> through the loop?
>>
>>
>> --
>> View this message in context:
>> http://www.nabble.com/variable-for-loop-tp25708820p25708820.html
>> Sent from the pgf-users mailing list archive at Nabble.com.
>>
>>
>>
>> ------------------------------------------------------------------------------
>> Come build with us! The BlackBerry® Developer Conference in SF, CA
>> is the only developer event you need to attend this year. Jumpstart your
>> developing skills, take BlackBerry mobile applications to market and stay
>> ahead of the curve. Join us from November 9-12, 2009. Register
>> now!
>> http://p.sf.net/sfu/devconf
>> _______________________________________________
>> pgf-users mailing list
>> pgf-users@...
>> https://lists.sourceforge.net/lists/listinfo/pgf-users
>
>
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry® Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay
> ahead of the curve. Join us from November 9-12, 2009. Register now!
> http://p.sf.net/sfu/devconf
> _______________________________________________
> pgf-users mailing list
> pgf-users@...
> https://lists.sourceforge.net/lists/listinfo/pgf-users
>
>

------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
pgf-users mailing list
pgf-users@...
https://lists.sourceforge.net/lists/listinfo/pgf-users

Re: variable for loop

by GGBBRR :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

Thanks a lot, guys.

This one works well:
\foreach \n in {1,...,25}
        \node [shift={(0.5,4.5)}] at ({mod(\n-1,5)}, {-floor((\n-1)/5)}) {\n};
Unfortunately this one did not work for me:
\pgfmathparse{int(5*\i +\j+1)}
I get an error message saying "PGF Math Error: Unknown function `int'." This has probably something to do with the version I am running. I am running this on an Ubuntu 8.04 system, which has pgf 1.18-1 as the latest supported version. I have tried installing the Debian package for pgf 2.0 (pgf_2.00-1_all.deb), but that made it only worse. It still could not find 'int' but now it also could not find 'mod'. So, I went back to 1.18-1. Anyways, now that I know about mod and floor I can probably get everything done.

A different solution I found later last night, is this:
\newcount\result
\begin{tikzpicture}
\draw (0,0) grid (5,5);
\foreach \i in {0,...,4}
  \foreach \j in {0,...,4}
  {
      \pgfmathparse{ 5*\i + \j+1}
      \result = \pgfmathresult
      \node at (0.5+\j, 4.5-\i ) { $\the\result$};
  }
\end{tikzpicture}
clearly not as nice as the solutions you have suggested.

Regards,
Gerben

Re: variable for loop

by GGBBRR :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hello,

Thanks a lot, guys.

This one works well:


> \foreach \n in {1,...,25}
>         \node [shift={(0.5,4.5)}] at ({mod(\n-1,5)}, {-floor((\n-1)/5)})
> {\n};
>

Unfortunately this one did not work for me:


> \pgfmathparse{int(5*\i +\j+1)}
>
I get an error message saying "PGF Math Error: Unknown function `int'." This
has probably something to do with the version I am running. I am running
this on an Ubuntu 8.04 system, which has pgf 1.18-1 as the latest supported
version. I have tried installing the Debian package for pgf 2.0
(pgf_2.00-1_all.deb), but that made it only worse. It still could not find
'int' but now it also could not find 'mod'. So, I went back to 1.18-1.
Anyways, now that I know about mod and floor I can probably get everything
done.

A different solution I found later last night, is this:


> \newcount\result
> \begin{tikzpicture}
> \draw (0,0) grid (5,5);
> \foreach \i in {0,...,4}
>   \foreach \j in {0,...,4}
>   {
>       \pgfmathparse{ 5*\i + \j+1}
>       \result = \pgfmathresult
>       \node at (0.5+\j, 4.5-\i ) { $\the\result$};
>   }
> \end{tikzpicture}
>

clearly not as nice as the solutions you have suggested.

Regards,
Gerben
--
View this message in context: http://www.nabble.com/variable-for-loop-tp25708820p25716489.html
Sent from the pgf-users mailing list archive at Nabble.com.


------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
pgf-users mailing list
pgf-users@...
https://lists.sourceforge.net/lists/listinfo/pgf-users

Re: variable for loop

by Alain Matthes-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Le 2 oct. 2009 à 16:11, GGBBRR a écrit :

>
>> \pgfmathparse{int(5*\i +\j+1)}
>>
> I get an error message saying "PGF Math Error: Unknown function  
> `int'." This
> has probably something to do with the version I am running. I am  
> running
> this on an Ubuntu 8.04 system, which has pgf 1.18-1 as the latest  
> supported
> version. I have tried installing the Debian package for pgf 2.0
> (pgf_2.00-1_all.deb), but that made it only worse.

Hi,

I think "int" is the  cvs version of pgf 2.00
> It still could not find
> 'int' but now it also could not find 'mod'. So, I went back to 1.18-1.
> Anyways, now that I know about mod and floor I can probably get  
> everything
> done.

but mod is in version 2.00. With  pgf 2.00 cvs you have mod and  Mod  
( and I'm very happy )

Somebody knows when the new version of pgf arrives ?

Best Regards

Alain Matthes
------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
pgf-users mailing list
pgf-users@...
https://lists.sourceforge.net/lists/listinfo/pgf-users