5

I am writing an .Rnw document in RStudio. I would like to be able to use latex expressions in Figure captions created by knitr.

First a code chunk for the caption:

<<cap_4, echo=FALSE>>=
cap <- "My figure. 
Some \textit{italics}. 
A reference to another figure: \ref{fig:other-fig}.
"
@

Now the plot chunk:

<<my-plot, echo=FALSE, fig.cap=cap>>=
ggplot(aes(x = x, y = y), data = data)
@

This fails to correctly render the italics or reference in the figure caption. Instead I get only part of the text (minus the first letter of the expression), e.g. instead of

A reference to another figure: 1.

I get

A reference to another figure: ef{fig:other-fig}.

Is it possible to use latex expressions in a knitr caption?

1
  • Most likely you need to escape the slash: \\ref{fig:other-fig}
    – darthbith
    Commented Apr 15, 2016 at 11:33

1 Answer 1

6

You need to escape the slash, so that R doesn't treat the \r as a symbol to interpret:

\\ref{fig:other-fig}

You must log in to answer this question.

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