I am trying to create a numerical variable (in code: called nClusters) that can be used in a knitr document both in fragments of R-code and in LaTeX. An example is given in the code below.
Here I initialize and set the nClusters numerical variable to 7. Later in the document, I call it in the R code snippet, and this seems to work fine. However, I then try to call it in the LaTeX section (outside the R code snippet), and this causes problems:
\documentclass{article}
\usepackage{float, hyperref}
\usepackage[margin=1in]{geometry}
\usepackage{pgffor}
\begin{document}
<<options, echo=FALSE>>=
nClusters = 7
library(knitr)
opts_chunk$set(concordance=TRUE)
@
<<echo=FALSE,eval=TRUE,results='asis'>>=
for (i in 2:nClusters){
print(paste("This is number",i))
}
@
% Here the call to nClusters does not work
\begin{center}
\foreach \i in {2,3,...,nClusters} {
Hello \i\
}
\end{center}
\end{document}
When I knit this, I get the following output:

When the output should be:

LaTeX , , 7, . , : knitr, R-, LaTeX?