Hi Folks,
I'm surprised, but I didn't find this question addressed anywhere.
I'd
like to generate a LaTeX caption with R code. I've tried the code
below, but I get the following TeX error:
! Argument of \@caption has an extra }.
<inserted text>
\par
l.21 }
Any thoughts? Perhaps I'll have to write the "\caption{}" text
with R?
thanks!
Sweave document:
\documentclass{article}
\title {test}
\author {me}
\usepackage{Sweave}
\begin {document}
\maketitle
\DeclareGraphicsExtensions{.pdf,.png}
\begin {figure}
<<label=fig1, echo=FALSE, fig=TRUE, pdf=false, png=true>>
plot(runif(100), runif(100))
@
\caption {
This is the caption with some r-code
<<>> 2*2
@
}
\label {fig:1}
\end {figure}
\end{document}
TeX document:
\documentclass{article}
\title {test}
\author {me}
\usepackage{Sweave}
\begin {document}
\maketitle
\DeclareGraphicsExtensions{.pdf,.png}
\begin {figure}
\includegraphics{test-fig1}
\caption {
This is the caption with some r-code
\begin{Schunk}
\begin{Sinput}
> 2*2
\end{Sinput}
\begin{Soutput}
[1] 4
\end{Soutput}
\end{Schunk}
}
\label {fig:1}
\end {figure}
\end{document}
On Aug 21, 2012, at 2:03 PM, Alexander Shenkin <ashenkin at ufl.edu> wrote:> Hi Folks, > > I'm surprised, but I didn't find this question addressed anywhere. I'd > like to generate a LaTeX caption with R code. I've tried the code > below, but I get the following TeX error: > > ! Argument of \@caption has an extra }. > <inserted text> > \par > l.21 } > > Any thoughts? Perhaps I'll have to write the "\caption{}" text with R? > > thanks! > > > Sweave document: > > \documentclass{article} > \title {test} > \author {me} > \usepackage{Sweave} > \begin {document} > \maketitle > \DeclareGraphicsExtensions{.pdf,.png} > > \begin {figure} > <<label=fig1, echo=FALSE, fig=TRUE, pdf=false, png=true>>> plot(runif(100), runif(100)) > @ > \caption { > This is the caption with some r-code > <<>>> 2*2 > @ > } > \label {fig:1} > \end {figure} > > \end{document} > > > TeX document: > > \documentclass{article} > \title {test} > \author {me} > \usepackage{Sweave} > \begin {document} > \maketitle > \DeclareGraphicsExtensions{.pdf,.png} > \begin {figure} > \includegraphics{test-fig1} > \caption { > This is the caption with some r-code > \begin{Schunk} > \begin{Sinput} >> 2*2 > \end{Sinput} > \begin{Soutput} > [1] 4 > \end{Soutput} > \end{Schunk} > } > \label {fig:1} > \end {figure} > \end{document}You can use \Sexpr{} to include R code within the LaTeX, but the code needs to return a scalar. For example: \documentclass{article} \usepackage{Sweave} \begin{document} \begin{figure}[tbp] \centering <<fig=TRUE,width=6,height=6>> plot(1:10) @ \caption{This is the caption \Sexpr{round(sqrt(2), 2)} with an R scalar included} \end{figure} \end{document} will return: \documentclass{article} \usepackage{Sweave} \begin{document} \begin{figure}[tbp] \centering \begin{Schunk} \begin{Sinput}> plot(1:10)\end{Sinput} \end{Schunk} \includegraphics{caption-001} \caption{This is the caption 1.41 with an R scalar included} \end{figure} \end{document} In general, if the R code is not very simple (eg. it is multiple lines/function calls, etc.), I will generally have the R code in an R chunk, assign the result to a scalar and then include that scalar in the \Sexpr{}: This is referenced in the Sweave manual and FAQ: http://www.statistik.lmu.de/~leisch/Sweave/ Regards, Marc Schwartz
This is a LaTeX question. I'll be surprised if \caption{} allows
arbitrary LaTeX environments inside it.
Perhaps it is irrelevant, but you can make it more compact with the
fig.cap option in knitr; see an example at
https://github.com/yihui/knitr-examples/blob/master/019-fig-caption.Rnw
\documentclass{article}
\begin{document}
Knitr can automatically generate a figure environment if fig.cap is specified.
<<test, fig.cap=paste('$2 \\times 2$ is', 2*2),
fig.height=3>>par(mar=c(4, 4, .1, .1)); plot(runif(4))
@
When fig.cap needs to use objects in the current chunk, we need to set
the eval.after options so the objects are available when fig.cap is
used.
<<setup>># evaluate fig.cap after a chunk is evaluated
opts_knit$set(eval.after = 'fig.cap')
@
Dynamically generate a figure caption:
<<t-test, fig.cap=paste('The P-value is',
format.pval(t.test(x)$p.value))>>x = rnorm(30)
hist(x)
@
\end{document}
Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA
On Tue, Aug 21, 2012 at 2:03 PM, Alexander Shenkin <ashenkin at ufl.edu>
wrote:> Hi Folks,
>
> I'm surprised, but I didn't find this question addressed anywhere.
I'd
> like to generate a LaTeX caption with R code. I've tried the code
> below, but I get the following TeX error:
>
> ! Argument of \@caption has an extra }.
> <inserted text>
> \par
> l.21 }
>
> Any thoughts? Perhaps I'll have to write the "\caption{}"
text with R?
>
> thanks!
>
>
> Sweave document:
>
> \documentclass{article}
> \title {test}
> \author {me}
> \usepackage{Sweave}
> \begin {document}
> \maketitle
> \DeclareGraphicsExtensions{.pdf,.png}
>
> \begin {figure}
> <<label=fig1, echo=FALSE, fig=TRUE, pdf=false,
png=true>>> plot(runif(100), runif(100))
> @
> \caption {
> This is the caption with some r-code
> <<>>> 2*2
> @
> }
> \label {fig:1}
> \end {figure}
>
> \end{document}
>
>
> TeX document:
>
> \documentclass{article}
> \title {test}
> \author {me}
> \usepackage{Sweave}
> \begin {document}
> \maketitle
> \DeclareGraphicsExtensions{.pdf,.png}
> \begin {figure}
> \includegraphics{test-fig1}
> \caption {
> This is the caption with some r-code
> \begin{Schunk}
> \begin{Sinput}
> > 2*2
> \end{Sinput}
> \begin{Soutput}
> [1] 4
> \end{Soutput}
> \end{Schunk}
> }
> \label {fig:1}
> \end {figure}
> \end{document}