Global variable in knitr for both R code Chunks and LaTeX -


i trying create numeric variable (in code: called nclusters) can used in knitr document both in r code chunks , latex. example in code below.

here, initialize , assign numeric variable nclusters value of 7. later, in document, call upon in r code chunk, , seems work okay. however, try call in latex section (outside r code chunk), , causing problems:

\documentclass{article} \usepackage{float, hyperref} \usepackage[margin=1in]{geometry} \usepackage{pgffor}  \begin{document}  <<options, echo=false>>= nclusters = 7 # want define nclusters once library(knitr) opts_chunk$set(concordance=true) @  <<echo=false,eval=true,results='asis'>>= # here call nclusters works (i in 2:nclusters){   print(paste("this number",i)) } @  % here call nclusters not work \begin{center} \foreach \i in {2,3,...,nclusters} {   hello \i\ } \end{center}  \end{document} 

when knit this, following output:

current output

when output should be:

desired output

the discrepancy occurring in latex call variable, because if hard-code in 7, works. hence, question is: possible create global variable in knitr can called in both r code chunks , latex parts?


Comments