title: Evaluate LaTeX syntax in a loop inside knitr chunk & adjust plot dpi
body: <p>Sorry if this question is very stupid. I am trying to run regressions and send summary tables and diagnostic plots to a PDF file. I found this package is very helpful, but have two basic questions.</p>
  
  <ol>
  <li>Can I use LaTeX to format page inside a Chunk?</li>
  <li>Is there a way to reduce the size of plots (I need to reduce pdf file size)? I tried to set dpi and use png rather than plot, which did not work. </li>
  </ol>
  
  <p>Thanks for any suggestions.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>\documentclass[10pt]{article}
  \usepackage{graphicx}
  \usepackage{amssymb}
  \usepackage{amsmath}
  \usepackage{setspace}
  \usepackage[margin = 1in]{geometry}
  
  \begin{document}
  \SweaveOpts{concordance=TRUE}
  \section{Risk $\sim$ log(Parameters)}
  &lt;&lt;fig.width=5,  fig.height=4.5, message=FALSE, tidy=TRUE, fig.align="center", echo=FALSE, warning=FALSE, eval=TRUE&gt;&gt;=
  library(R.matlab)
  for (k in 1:6){
  data_t=readMat("I:\\Dropbox\\Rregression\\matlab_size.mat")
  
  data=data.frame(NA, nrow = length(data_t[[1]]), ncol = 7)
  data=data.frame(rho=data_t[[1]],Risk=data_t[[2]],Mass.f=data_t[[3]],Mass.utf=data_t[[4]],Mass.w=data_t[[5]],
                  ach=data_t[[6]],p=data_t[[7]])
  
  data2=log(subset(data,select=-c(Risk)))
  N_reg=dim(data2)[2]
  num_ele=k
  Comb=combn(N_reg,num_ele)
  N_comb=ncol(Comb)
  Results=data.frame(NA,ncol=4,nrow=N_comb)
  TSS=sum((data$Risk-mean(data$Risk))^2)
  
  for (i in 1:N_comb){  
    data_temp=data2[,Comb[,i]]  
    lm_temp=lm(data$Risk~as.matrix(data_temp))
    name=paste("lm_", i, sep="")
    assign(name, lm_temp)
    Results[i,1]=summary(lm_temp)$r.squared
    Results[i,2]=summary(lm_temp)$adj.r.squared
    Results[i,3]=sum(resid(lm_temp)^2)
    Results[i,4]=sum((fitted(lm_temp)-mean(fitted(lm_temp)))^2)
  } 
  colnames(Results)=c("R2", "Adj_R2", "RSS", "ESS")
  best_col=which(Results[,2]==max(Results[,2]))
  
  cat ('Number of independent variables=', k)
  print (summary(eval(parse(text=(paste("lm_", best_col, sep=""))))))
  print (Results[best_col,])
  par(mfrow=c(2,2),mar=c(3,3,3,3))
  plot(eval(parse(text=(paste("lm_", best_col, sep=""))))) #question 2: is there a way to reduce dpi of plot? 
  \newpage #Here is question 1, I want to add a page break after printing a pair of summary table and plots
  }
  
  @
  \end{document}
  </code></pre>
  
  <h3>Update</h3>
  
  <p>Found a format issue when you involved a loop inside a chunk. Based on Yihui's suggestions, I ran</p>
  
  <pre><code>library(devtools)
  install_github('evaluate', 'hadley')
  install_github('knitr', 'yihui')
  </code></pre>
  
  <p>in R Console. However, met the following errors related to vignettes:</p>
  
  <pre><code>* installing the package to re-build vignettes
  * creating vignettes ... ERROR
  C:/PROGRA~1/R/R-215~1.1/bin/Rscript -e "library(knitr); knit2pdf('knitr-intro.Rnw')"
  /usr/bin/sh: -c: line 0: syntax error near unexpected token `('
  /usr/bin/sh: -c: line 0: `C:/PROGRA~1/R/R-215~1.1/bin/Rscript -e \library(knitr); knit2pdf(knitr-intro.Rnw)\'
  make: *** [knitr-intro.pdf] Error 1
  Error in tools::buildVignettes(dir = ".") : running 'make' failed
  Execution halted
  Error: Command failed (1)
  In addition: Warning message:
  running command '"C:/PROGRA~1/R/R-215~1.1/bin/x64/R" --vanilla CMD build "C:\Users\th\AppData\Local\Temp\RtmpSMRGAq\yihui-knitr-568a092" --no-manual --no-resave-data' had status 1 
  </code></pre>
  
  <p>So, any suggestions on how to solve this? I have installed MikTex 2.9, and tried to download Knitr package directly from Git and build in Windows shell. I had the following errors:</p>
  
  <pre><code>C:\Users\th\Documents\R\win-library\2.15&gt;R CMD build knitr --no-vignettes
  * checking for file 'knitr/DESCRIPTION' ... OK
  * preparing 'knitr':
  * checking DESCRIPTION meta-information ... OK
  Warning in file(con, "r") :
    cannot open file 'man': No such file or directory
  Error in file(con, "r") : cannot open the connection
   ERROR
  computing Rd index failed
  </code></pre>