Imagine one has the LaTeX environments Example and Solution defined from the theorem environment. I have several Examples in numerous child documents which make up a book. LaTeX will automagically track the chapter and Example, Solution, Figure, etc number for a labeled Example, Solution, Figure, etc., respectively. Suppose the first code chunk below corresponds to the 6th Example in Chapter 4 of a book. Then \ref{SetSeed} will return the number 4.6 when using \documentclass{book}. I would like to be able to provide names to my code chunks that will stay in synchronization with the labels of my different LaTeX environments (Example, Solution, Figure, etc.). What I do not want to do is hard code my code chunks with 'Example 4.6', etc. Any suggestions would be most welcome. I am aware of the code chunk options Yihui has for figure captions and labels but would be willing to do an "old style" code chunk surrounded with a \begin{figure}, \end{figure} environment if the code chunks can be named appropriately. The thought is that the named code chunk might be generated with something like

<<paste(Solution,'\ref{SetSeed}', sep=" "), echo = TRUE>>=    ...(which does not work) 

to generate the named chunk 'Solution 4.6'. The rationale is to be able to later purl() the master document to produce labeled code chunks from the individual chapters that correspond to the environments (Figure, Example, Solution, etc.) displayed in the book. Thanks in advance, Alan.

\begin{Example} \label{SetSeed}
Use the function \texttt{set.seed()} with a value of 13 and generate 
20 values from a normal distribution with a mean of 100 and a standard 
deviation of 15.  Find the mean of the randomly generated values.

<<paste(Solution,'\ref{SetSeed}', sep=" "), echo = TRUE>>=
# some R code
xs <- rnorm(20, 100, 15)
The mean of the values generated in Example \ref{SetSeed} have a 
mean of \Sexpr{mean(xs)}.
  • 2
    Can you lay your question out better? That's a lot of text in one chunk!
    – Lee Taylor
    Commented Dec 7, 2012 at 2:27
  • Yes. I did not format the initial question to display properly. Sorry.
    – user1884192
    Commented Dec 8, 2012 at 12:54
  • 1
    This might be hard, since the flow of information is one-way from R to LaTeX. That is, knitr gets run before LaTeX, and completely in ignorance of any LaTeX machinery ... you might be able to produce a chunk named something like "Solution\ref{setSeed}", but getting one named "Solution4.6" would be hard ... ? Apparently brew is more flexible, although I haven't used it
    – Ben Bolker
    Commented Dec 8, 2012 at 17:06
  • Why do you want the chunks to have the same name as the environment (and number) they fall within, I don't understand why this would be of interest or necessary... Commented Jan 12, 2013 at 10:55
  • Some people ask also for automatic labelling in LaTeX, and the problem is essentially the same: Beside the technical difficulty to obtain that, the important issue is the utility of that, The name xxxx of the chunk is used to warning you about where is some error i s in xxxx ... now what?. The xxxx name is also used to label the R figures as fig:xxxx. Now you want include a reference to that figure, but now you can not be sure that label will be xxxxor something ... what a mess!
    – Fran
    Commented May 24, 2019 at 8:41

1 Answer 1


That is a challenging problem. As Ben Bolker pointed out, it is a one-way flow: when R is done, nothing will come back from LaTeX to R again, so R will not be able to know the value of \ref{SetSeed} in LaTeX.

However, I do not think it is completely impossible, because you actually have the *.aux file generated from LaTeX, which you can parse with R for the solution numbers, and update the raw R script from purl() with this information. One approach is that you use the same label for the R chunk as you used for the Example environment, and you will get a code chunk in the output like:

## @knitr SetSeed
# R code

Hopefully you will also see this in the *.aux file after you have run LaTeX on the *.tex file:


Then you replace SetSeed in the R code with Solution 1.1. In all, you need some post-processing of the R script.

  • Thanks. I will have to think about that approach deeper over the break. Commented Dec 13, 2012 at 18:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .