I have created an RMarkdown file in RStudio and managed to knit it with knitr into an HTML and .md file. Next, I used pandoc to convert the .md file into a PDF file (I get an error if I try and convert from the .html file). However, the PDF that is produced have massive margins (like this http://johnmacfarlane.net/pandoc/demo/example13.pdf). How can I get pandoc to produce something with smaller margins? I have looked through the pandoc user guide, but haven't found anything useful.
R – Set margin size when converting from Markdown to PDF with pandoc
knitrlatexmarkdownpandocr
Best Answer
Recent versions of rmarkdown and pandoc
In more recent versions of rmarkdown, the settings of margins can be done in the YAML header via the top-level element
geometry
. What you specify in thegeometry
tag will be piped into the LaTeX template that ships with Pandoc via the following LaTeX snippetFor example, to specify margins that are 2cm in width one would include
For more complex specifications to be passed to the geometry LaTeX package, string options together as you would with LaTeX:
Original answer
This is a LaTeX question as Pandoc is rendering to PDF via LaTeX - what you linked to represents the default margins on a LaTeX document.
The geometry LaTeX package for example can be used to alter the margins of the page. However you'll need a way to tell Pandoc to use this by including it ins the LaTeX header applied to the converted md file.
How you do this is documented in the Pandoc User Guide. See in particular the
--template=FILE
command line argument and the Templates section. Essentially, either find and modify the default template to include the LaTeX instructions you want to use or start your own template from scratch and place it in the appropriate location; see the--data-dir
command line argument.Another alternative if you are using a recent version of Pandoc is to use the variable argument (set either with
-V KEY[=VAL]
or--variable=KEY[:VAL]
). Thegeometry
package was added to the default LaTeX template in May 2012 (see this discussion). As such, if you wanted to change the page margins, you can use:You can specify multiple variable values too. For instance, if you wanted to create a 4 by 6 inch pdf with half-inch margins, you can use: