[R] Sweave: R chunk inside caption?

Alexander Shenkin ashenkin at ufl.edu
Tue Aug 21 21:03:17 CEST 2012


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}



More information about the R-help mailing list