1

I try to create a bunch of graphics within a loop, and would like to add captions above by using this hook. Is it possible to modify this hook so it would be possible to submit a vector with length > 1 to capT and receive the same result as with fig.cap but with the caption above? This document ends with an error!

The expected outcome would be: First graphic with first caption, Second graphic with second caption etc.

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\begin{document}

<<init, echo=FALSE, results='hide'>>=
library(knitr)
knitr::opts_chunk$set(echo=FALSE, 
                      results='hide', 
                      warning=FALSE, 
                      message=FALSE,
                      error = FALSE)

f <- function(x, options) {
  paste("\\end{kframe}\n", 
        "\\caption{", options$capT, "}\n",
        #"\\begin{figure}",
        hook_plot_tex(x, options), 
        #"\\end{figure}",
        "\n\\begin{kframe}", sep = "")
}
knitr::knit_hooks$set(plot = f)
@

Some Text.

<<first, capT=c('one', 'two', 'three')>>=
for(i in 1:3)
{
plot(1:10,1:10)
}
@

\end{document}

Update

This is working now. This was easy to fix. But now I have the problem with ggplot2.

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\begin{document}

<<init, echo=FALSE, results='hide'>>=
library(knitr)
knitr::opts_chunk$set(echo=FALSE, 
                      results='hide', 
                      warning=FALSE, 
                      message=FALSE,
                      error = FALSE)

f <- function(x, options) {
  paste("\\end{kframe}\n", 
        "\\begin{figure}",
        "\\caption{", options$capT, "}\n",
        hook_plot_tex(x, options), 
        "\\end{figure}",
        "\n\\begin{kframe}", sep = "")
}
knitr::knit_hooks$set(plot = f)
@

Some Text.

<<first, capT=c('one', 'two', 'three')>>=
for(i in 1:3)
  {
  plot(1:10, 1:10)  
  }
@

\end{document}

While using plot() leads to:

caption1 - plot1; caption2 - plot2; caption3 - plot3; (expected output)

ggplot() leads to:

caption1 - plot1; caption1 - plot2; caption1 - plot3; caption2 - plot1 etc.

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\begin{document}

<<init, echo=FALSE, results='hide'>>=
library(knitr)
knitr::opts_chunk$set(echo=FALSE, 
                      results='hide', 
                      warning=FALSE, 
                      message=FALSE,
                      error = FALSE)

f <- function(x, options) {
  paste("\\end{kframe}\n", 
        "\\begin{figure}",
        "\\caption{", options$capT, "}\n",
        hook_plot_tex(x, options), 
        "\\end{figure}",
        "\n\\begin{kframe}", sep = "")
}
knitr::knit_hooks$set(plot = f)
@

Some Text.

<<first, capT=c('one', 'two', 'three')>>=
library(ggplot2)

for(i in 1:3)
  {
  d <- data.frame(a=1:10,b=1:10)
  p = ggplot(d, aes(a,b)) + geom_point()
  print(p)
  }
@

\end{document}

Why is the output from ggplot2 behaving differently?

Thank you!

Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
Manuel
  • 265
  • 3
  • 11

0 Answers0