2014-12-06 107 views
1

我試圖在LaTex中創建一個循環,但它只會在第一次通過時打印出來。以下是我正在處理的內容,但我的循環無法正常工作。Latex中的循環不起作用

\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} 

回答

1

a number of ways to create a loop in LaTeX,所以沒有必要重新發明輪子:

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}