Lcat91
Lcat91

Reputation: 794

Loop in Latex isn't working

I'm trying to create a loop in LaTex, but it will only print out on the first time through. Here's what the gist of what I'm working with, but I my loop isn't working correctly.

 \documentclass[12pt]{article}

 %%% FOR LOOP CODE
 \usepackage{ifthen}
 \newcommand{\forLoop}[5][1]{%
   \setcounter{#4}{#2} %
   \ifthenelse{ \value{#4}<#3 }%
   {%
     #5 %
     \addtocounter{#4}{#1} %
     \forLoop[#1]{\value{#4}}{#3}{#4}{#5} %
   }%
  {%
    #5 %
  }%
  }%
 %% END FOR LOOP CODE

 \begin{document}

 Practice loop
 \newcounter{index}

 \section{Example 1}

 \forLoop[2]{1}{5}{index} 
 {
    This is inside the for loop: iteration \theindex\\
 }

 \end{document}

Upvotes: 1

Views: 1651

Answers (1)

Werner
Werner

Reputation: 15065

There are a number of ways to create a loop in LaTeX, so there's no need to re-invent the wheel:

enter image description here

\documentclass{article}

\usepackage{multido}
\newcommand{\forLoop}[4][1]{\multido{\i=#2+#1}{#3}{#4}}

\setlength{\parindent}{0pt}% Just for this example
\begin{document}

\section{Example}

\forLoop[2]{1}{5}
{%
  This is inside the for loop: iteration \i\endgraf
}

\end{document}

Upvotes: 1

Related Questions