简体   繁体   中英

Insert many images each on separate page with unique captions in rmarkdown

I want to insert a different image(.pdf) on each page with its own unique caption. What I have tried either puts multiple images and captions on some pages or (with the code below) puts images on their own page but doesn't allow a caption to be pulled from a matrix in the environment (the image label comes out as "NA"). The "x6" is a matrix with text that I want to use as captions.

Any ideas?

TIA

---
title: "Title"
geometry: margin=1.25cm 
output: 
  pdf_document:
    keep_tex: yes
    fig_caption: true
fontfamily: arev     

header-includes:
  \AtBeginDocument{\let\maketitle\relax}
  \usepackage{caption}
  \captionsetup[figure]{labelformat=empty}
  \usepackage[font={small}]{caption}
  \usepackage[maxfloats=256]{morefloats}
  \maxdeadcycles=1000
fontsize: 10pt
#sansfont: Calibri Light

---
---
{r, echo=FALSE,warning=FALSE}
library(staplr)
library(readtext)
library(stringr)
library(formatR)
x <- readtext("mptext.docx")
x2 <- str_split(x,"Slide ")
#x3$slide <- paste("Slide ",1:nrow(x3),sep = "")
x3 <- (matrix(unlist(lapply(unlist(x2)[1:10], function(y) str_sub(unlist(y),end = -2,start = 3))),nrow=length(lapply(unlist(x2)[1:10], function(y) str_sub(unlist(y), end = -2,start = 5))),byrow=TRUE))
x4 <- (matrix(unlist(lapply(unlist(x2)[11:100], function(y) str_sub(unlist(y),end = -2,start = 4))),nrow=length(lapply(unlist(x2)[11:100], function(y) str_sub(unlist(y), end = -2,start = 4))),byrow=TRUE))
x5 <- (matrix(unlist(lapply(unlist(x2)[101:251], function(y) str_sub(unlist(y),end = -2,start = 5))),nrow=length(lapply(unlist(x2)[101:251], function(y) str_sub(unlist(y), end = -2,start = 5))),byrow=TRUE))

x6 <- rbind(x3,x4,x5)
x6 <- as.matrix(x6[-1])
---
---
{r,results='asis',echo=FALSE}

library(knitr)
flist <- list.files("./ind_pdf2")
for (i in flist[1:50]){
cat(paste("![",(x6[i]),"](", i, ")\n\n\\pagebreak"))
 
}
---

If I change the first i in last line (x6[i]) to a 1 (x6[1]) , I can get the images to come out on separate pages but with the first caption/label repeated for each image.

After tinkering with this I came up with the following for the last code chunk.

library(knitr)
flist <- list.files("./ind_pdf2")
counter <- 0
for (i in flist[1:50]){
counter <- counter + 1
cat(paste("![",(x6[counter]),"](", i, ")\n\n\\pagebreak"))
 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM