luciano
luciano

Reputation: 13802

create function to alter PDF margin width in PDF files using Pandoc

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?

Upvotes: 0

Views: 645

Answers (1)

mnel
mnel

Reputation: 115390

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"

Upvotes: 1

Related Questions