Dica: forLoop em LaTeX

143 views
Skip to first unread message

Elvio Prado

unread,
Jun 1, 2012, 11:56:14 AM6/1/12
to LaTeX-BR
Pessoal, aqui vai uma dica que gostei muito,
e estou compartilhano com vocês:
 
Loop FOR em LaTeX.
 
 
 
 
Here is some beautiful code I found on the internet some time ago, which produces a recursive for loop in LaTeX:
%%% 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
The syntax is:
\forLoop[STEP]{START}{END}{COUNTERNAME}{COMMANDS}
This code will execute COMMANDS inside a for loop that starts at START, goes up by STEP, and ends at END. COUNTERNAME is the counter used to maintain the index of the loop. As usual, the best way to show this is using a sample document, so here it is! There are 3 examples: one simple sentences, a second in math mode, and a third complicated example that calculates the Fibonacci numbers!! Enjoy!!
\documentclass{article}
\usepackage{calc} %% Just need this to be able to divide values in example 3
%%% 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}
\newcounter{index}
\section{Example 1}
\forLoop[2]{6}{12}{index} {
This is inside the for loop: iteration \theindex\\
}
\section{Example 2}
$$
\forLoop{1}{20}{index} {
x^{\theindex}
}
$$
\section{Example 3}
\newcounter{fibold1}%
\newcounter{fibold2}%
\setcounter{fibold1}{1}%
\setcounter{fibold2}{1}%
\newcounter{fibcurr}%
\newcounter{tempcounter}%
\begin{eqnarray*}
& 1, 1, \forLoop{1}{40}{index} {
\setcounter{fibcurr}{0}
\addtocounter{fibcurr}{\value{fibold1}}
\addtocounter{fibcurr}{\value{fibold2}}
\thefibcurr,
\setcounter{tempcounter}{\value{index}-(\value{index}/5)*(5)}
\ifthenelse{ \value{tempcounter} = 0 }
{
\\
&
}
{}
\setcounter{fibold1}{\value{fibold2}}
\setcounter{fibold2}{\value{fibcurr}}
}
\end{eqnarray*}
\end{document}
 
 
==================================================
Elvio Prado da Silva
Professor do IFBA-Instituto Federal da Bahia
Área de Elétrica

FHRB Toledo

unread,
Jun 1, 2012, 6:57:46 PM6/1/12
to late...@googlegroups.com
Elvio,

Boa dica! Obrigado por compartilhar.

att,
FH

Emílio Kava

unread,
Jun 2, 2012, 10:16:00 PM6/2/12
to Latex - Brasil
Aproveitando o bonde...

Para trabalhar sobre uma imagem, superpor texto, gráficos:
http://tex.stackexchange.com/questions/15314/how-can-i-superimpose-latex-tex-output-over-a-pdf-file

Para criar campos de preenchimento em arquivos pdf:
http://tex.stackexchange.com/questions/14842/creating-fillable-pdfs/14844#14844


Abraços,
Emílio E. Kavamura, MSc
DEMEC-UFPR


Date: Fri, 1 Jun 2012 19:57:46 -0300
Subject: Re: [LaTeX-br] Dica: forLoop em LaTeX
From: fernand...@gmail.com
To: late...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages