Peter
Peter

Reputation: 7790

Dynamic LaTeX references in R comment with knitr

I had ask a similar question to this with respect to Sweave ( Dynamic references to figures in a R comment within Sweave document ) and would like to see if anyone as a similar answer when using knitr.

The goal is to have the following code chunk

<<"example", fig.cap = "some figure", highlight = FALSE>>=
# the following code generated Figure \ref{fig:example}
plot(1:10, 1:10)
@

have be displayed in the resulting .pdf as

# the following code generated Figure 1.1
plot(1:10, 1:10)

So far I have found that by setting highlight = FALSE the R code is placed into a verbatim environment in the resulting .tex file. If the environment could be alltt instead of verbatim then we'd have the desired output. Is it possible to have the non-highlighted code chunks be placed in alltt environments via a knitr option?

Upvotes: 5

Views: 829

Answers (1)

Yihui Xie
Yihui Xie

Reputation: 30114

I have added an example 072-latex-reference.Rnw in the knitr-examples repository. The basic idea is to restore the escaped \ref{} (which should have been \textbackslash{}ref\{\} in the default output).

Upvotes: 3

Related Questions