Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic LaTeX references in R comment with knitr

Tags:

r

latex

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?

like image 956
Peter Avatar asked Oct 21 '22 21:10

Peter


1 Answers

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).

like image 123
Yihui Xie Avatar answered Oct 27 '22 10:10

Yihui Xie