0

I'm converting a markdown document to a PDF using Pandoc from within R. The default margins of PDFs outputted by Pandoc are too large.

In this post: Set margin size when converting from Markdown to PDF with pandoc, the following code is given to alter margin widths of PDFs:

pandoc -V geometry:margin=1in -o output.pdf input.md

I've using this code in a function from within R,

makePDF <- function(name) {
  library(knitr)
  knit(paste0(name, ".Rmd"), encoding = "utf-8")
  system(paste0("pandoc -o -V geometry:margin=1in ", name, ".pdf ", name, ".md"))
}

but this gives this error:

pandoc: geometry:margin=1inmpAnnual.pdf: openFile: does not exist (No such file or directory)

How can I create a function in R to alter margin widths of PDFs?

Community
  • 1
  • 1
luciano
  • 13,158
  • 36
  • 90
  • 130

1 Answers1

1

Your issue is that your paste0 construction does not create what you are after. Your error message also doesn't reflect the code you have provided.

name <- 'name'
paste0("pandoc -o -V geometry:margin=1in ", name, ".pdf ", name, ".md")
## [1] "pandoc -o -V geometry:margin=1in name.pdf name.md"

The you have put -o in the wrong spot.

I think it easier to use sprintf to create calls such as this, using %s for where you want to insert your file name.

callformat <-"pandoc -V geometry:margin=1in  %s.md -o %s.pdf"
sprintf(callformat, name,name)
## [1] "pandoc -V geometry:margin=1in  name.md -o name.pdf"
mnel
  • 113,303
  • 27
  • 265
  • 254