TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm new to sweave and was wondering what is the appropriate way to label a code section. For example, if I have:

<<echo= true>>=

How would I get it to put a either above it or below it, with something like "Code Example 1". I would like something similar to how you can label figures or tables in LaTeX.

share|improve this question

migrated from stackoverflow.com Jun 21 '12 at 16:19

This question came from our site for professional and enthusiast programmers.

Welcome to TeX.sx! Your question was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. – Werner Jun 21 '12 at 17:09

In LaTeX captions can be used within float environments like figures or tables. So if your R output is e.g. a figure you can enclose it in a figure environment:


<<fig=TRUE, echo=FALSE, eps=TRUE>>=

\caption{A boxplot of the famous Fisher data set.}

If you want your R-Code as a float environment (with the possibility to use \caption) you will have to define your own float environment. Have a look at LaTeX WikiBook. Something like the following will do it:

In your document preamble:

\floatname{rcode}{Code Example}

Then in your document body:

\caption{This code shows something.}
share|improve this answer

Try something like this:

<<label = MyChunk, echo= true>>=
share|improve this answer
I did, it has no effect! – user1357015 Jun 21 '12 at 16:01
That wasn't what the OP meant by label the chunk. Yours is the correct answer to a different Q. See the Answer of Stefan Pointner for what appears to have been wanted. – Gavin Simpson Jun 21 '12 at 16:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.