# Looping a code-chunk in LaTeX

I want to generate a report for three types of analysis I am performing. But each time when I perform the analysis it can happen that one or more than one type of analysis occur. I want to put condition in the report that will run the part of report according to the type of analysis performed. For each analysis I need to repeat the chunk say n times. I am stuck at writing a for loop for the code chunk. 'a' is a vector eg code:

\myboolean\Sexpr{myboolean}
for(i in 1:n)
\section{Frequency Analysis}
<<for-loop>>=
print(a[i])
@

-
 I'm not quite sure what you are trying to accomplish, but have a look at either tikz/pgf or luatex, both have for-loops. – Psirus Jul 19 '12 at 17:49

See https://gist.github.com/3145751 for an example with knitr. You cannot insert LaTeX into R code directly (it is not like PHP), but you can knit a child document for n times with different values of i.
 I'm not able to run your example. When I am running the code a .tex file gets generated but fails to give a pdf document. I have attached the log file and the .tex file along. – ankit Jul 20 '12 at 10:22 Error: running 'texi2dvi' on 'temp.tex' failed LaTeX errors: :1: LaTeX Error: Can be used only in preamble. :1: LaTeX Error: Can be used only in preamble. :14: LaTeX Error: Can be used only in preamble. :16: LaTeX Error: Command \hlnumber already defined. Or name \end... illegal, :17: LaTeX Error: Command \hlfunctioncall already defined. Or name \end... illegal, In addition: Warning message: running command '"C:\PROGRA~1\Miktex\miktex\bin\texi2dvi.exe" --quiet --pdf "temp.tex" -I "C:/PROGRA~1/R/R-214~1.2/share/texmf/tex/latex" -I "C:/PROGRA~1/R/R-214~1.2/share/texmf/bibtex/bst"' had status 1 – ankit Jul 20 '12 at 10:33 Inorder to save space I have removed- :See the LaTeX manual or LaTeX Companion for explanation. Type H for immediate help – ankit Jul 20 '12 at 11:09 could you paste the log somewhere like pastebin? pastebin.com I cannot really read the log here. BTW, are you using the latest version of knitr? – Yihui Jul 20 '12 at 21:59 After using the latest knitr package I was able to get a pdf file but there were following problems with them. i. when I use knit2pdf no pdf is generated and here is the link to log-file link ii. when I use compile pdf tab a pdf is generated with only one file(Now i is 1) in it along with some text articlegraphicx, color upqoute framed alltt alltt I understand these are the package names, but dont know why they are printed in the pdf. link – ankit Jul 23 '12 at 14:39