0

I am creating a report that contains several chapters (one chapter per species, following the same template). I think this is the best approach to creating it:

  • make the intro materials as one Rmd file, knitted to Word
  • loop through the list of species, creating one Word file per chapter
  • combine intro material with all chapters in a Word doc (this must be the final format I use, sadly).

The issue I'm running into is that in each of the parameterized chapter files, the "tag" for the figure in the in-text citation is the same for the first figure in each chapter, the second figure in each chapter, etc. This means that when I put all the chapters together into Word and update the fields, I end up with Figures 1-10, but in-text cross-references that only point to Figures 1 and 2. I am stuck.

The code is a little involved to post all in one StackOverflow post, so I have made a GitHub repo to replicate this issue here.

The rendering loop looks like this:

cyl.vec <- c(4,6)

for (i in 1:length(cyl.vec)){
  rmarkdown::render("markdown/ParameterizedChapter.Rmd",
                    params = list(
                      cylcode = cyl.vec[i]
                    ), output_file = paste0(
                      "ParameterizedChapter_",
                      cyl.vec[i],
                      "cylinders.docx")
                    )
}

and ParameterizedChapter.Rmd, the template, looks like this: Screenshot of Rmd file in question

I thought about including each species as a child document in the main .Rmd, but that would become annoying with more than about 10 species to loop through. I could hard-code the figure numbers but the beauty of Markdown is supposed to be that you don't need to. How can I get the in-text cross-references to be properly tagged, so that when all the chapters are together, those in-text cross-references are referring to the correct figures?

MargaretS
  • 21
  • 4

1 Answers1

0

I let this question sit for months not remembering that I found the answer! For parameterized reports with repeated sections, you can knit to Word and preserve indexing by using knit_expand() in a for loop.

A method to do this in RMarkdown and in Quarto is here.

MargaretS
  • 21
  • 4