# How to simulate 2 dimensional loop using 1 dimensional loop?

animate package does not allow nested loops. Only a single loop is allowed. However, we often need to simulate 2 dimensional space in a single loop.

Please see the following C# code that I want to implement in LaTeX.

static void Main()
{
const int M = 3;
const int N = 4;
int i = 0;
int j = 0;

for (int c = 0; c < M * N; c++)
{
if (i >= N)
{
j++;
i = 0;
//newline
}
//print i and j
i++;
}
}


The real scenario is to create a PDF animation using animate package instead of the GIF animation at this link.

Last edit:

Because I used Altermundus' answer in my real scenario below,

\documentclass{article}
\usepackage{graphicx}
\usepackage{animate}
\usepackage{pstricks}
\SpecialCoor

\newsavebox\IBox
\savebox\IBox{\includegraphics{Images/bald}}
\def\N{2}% columns
\def\M{3}% rows
\psset{xunit=\dimexpr\wd\IBox/\N\relax,yunit=\dimexpr\ht\IBox/\M\relax}

\begin{document}
\animateinline[palindrome,autoplay]{1}
\newcounter{i}\newcounter{j}
\multiframe{\numexpr\N*\M\relax}{}
{
\unless\ifnum\value{i}<\N\relax
\setcounter{i}{0}%
\fi
\begin{pspicture}[showgrid](\N,\M)
\begin{psclip}{\psframe[linestyle=none]
(!\thei\space \thej)
\rput[bl](0,0){\usebox\IBox}
\end{psclip}
\end{pspicture}
}
\endanimateinline
\end{document}


so I will accept his answer.

Other answers also get +1 of course!

NOTE: Compile it with xelatex. Animation importing images does not work with latex-dvips-ps2pdf. \newcount does not work as well, use \newcounter instead.

-
I've updated my answer to match your update. –  Loop Space Aug 15 '11 at 9:18
I've updated also my answer –  Alain Matthes Aug 15 '11 at 9:30

I don't know why you use \numexprin this case. This macro is useful if you want to use a math expression like \numexpr 3-3*13/5\relax. You need to properly finish the expression to stop \numexpr, so \relax is needed. I don't understand what you want and the code so I give you some possible codes :

\ifnum\n<3 \relax ... \ifnum\n<\numexpr 3\relax \ifnum\n<\numexpr3*\yy\relax

\thexxis wrong in your case you need \the\xx perhaps you make a mix between \newcount and newcounter. Tex and LaTeX counters !

I don't know why you need multido in this case, if the step is 1, you can use a simple \loop... \repeat.

With you explanation, my new code is ( It's the same that Andrew's code but I don't use multidoand I use numexpr

\documentclass{article}

\def\M{3}
\def\N{4}

\newcount\i
\i=0
\newcount\j
\j=0
\newcount\n
\n=0
\parindent=0pt

\begin{document}

\loop
\unless\ifnum\n=\numexpr\N*\M\relax  % like Herbert, in this case
% better is \ifnum\n<\numexpr\N*\M\relax without \unless
(\the\i,\the\j)
\unless\ifnum\the\j<\N\relax
\newline
\j=0\relax
\fi
\repeat
\end{document}


The result is the same

-
\documentclass{article}
\parindent=0pt
\newcount\xx\xx=1
\newcount\yy\yy=1
\newcount\n\n=1
\newcount\N
\begin{document}
\loop
\ifnum\N<9
\ifnum\n<4
(\the\yy,\the\xx)
\else
\newline
\xx=1\n=1
\fi
\repeat

\end{document}


-

The following referred to the first version of this question. I suspect that the reason that the code in the current version is not working is similar, but I do not know enough about the primitives involved to debug it.

There's a problem with the \number\numexpr3\yy\relax bit. What is getting passed to the \ifnum is simply the 3, leaving the \yy alone. So the test is \ifnum\n<3. Then it reads \yy; as this is a new statement, it is expecting to assign a number to this count but it gets \relax so it complains.

I don't know enough about how \ifnum, \number, and \numexpr interact to be able to suggest a fix that just sorts out that line, but I do have a suggestion as to how to get the output that you want from a single loop.

The following now refers to the second version of the question.

When looking at your code, I see three variables: the dummy \n, then the two coordinate variables, \i and \j. The test is being done on the dummy, but it is more natural do it on the coordinates. In the following, \n is simply there to ensure that the loop is done enough times. Then we increment \j each time, until it reaches the threshold when we reset it and increment \i.

\documentclass{article}
%\url{http://tex.stackexchange.com/q/25722/86}
\usepackage{multido}

\def\M{3}
\def\N{4}

\newcount\i
\i=0

\newcount\j
\j=0

\parindent=0pt

% Figure out how many iterations we need to do.
\makeatletter
\@tempcnta=\M\relax
\multiply\@tempcnta by \N\relax
\edef\MN{\the\@tempcnta}%
\makeatother

\begin{document}

\multido{\n=0+1}{\MN}
{
(\the\i,\the\j)
\unless\ifnum\the\j<\N\relax
\newline
\j=0\relax
\fi
}

\end{document}


Result:

(0,0) (0,1) (0,2) (0,3)
(1,0) (1,1) (1,2) (1,3)
(2,0) (2,1) (2,2) (2,3)


(I suspect I've swapped \i and \j from your code. For me, rows come first and are i, columns come second and are j.)

-
Interestingly, in testing this I called my test file multido.tex. Big Mistake! The multido.sty file inputs a file called multido.tex and the test file kept getting loaded instead of the real one. Ooops. –  Loop Space Aug 15 '11 at 7:55
You can use \unless\ifnum\xx<3 \newline ...  –  Alain Matthes Aug 15 '11 at 9:01
@Altermundus: Where is \unless defined? (As in, when can I know that I can use it?) I'm forever writing \if<condition>\else and it would be nice to be able to write \unless. –  Loop Space Aug 15 '11 at 9:03
\unless (eTeX extension) "The \unless command is an extension of ε-TeX; the behavior of \unless\iftrue is the same as \iffalse. This means the following: This command is expandable; it reads a token; this token must be a conditional, but not \ifcase. The conditional computes a truth value, which is then negated. Expansion of the command is the same as that of the conditional, said otherwise, the next token, if the test is true, otherwise what follows the \else or \fi." –  Alain Matthes Aug 15 '11 at 9:08
@Altermundus: Very nice to know. Thanks. –  Loop Space Aug 15 '11 at 9:23