Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set margin size when converting from Markdown to PDF with pandoc

People also ask

Can pandoc convert from PDF?

You can use the program pandoc on the SCF Linux and Mac machines (via the terminal window) to convert from formats such as HTML, LaTeX and Markdown to formats such as HTML, LaTeX, Word, OpenOffice, and PDF, among others.

Does Rmarkdown use pandoc?

The R package rmarkdown is a library which proceses and converts . Rmd files into a number of different formats. The core function is rmarkdown::render which stands on the shoulders of pandoc. This function 'renders the input file to the specified output format using pandoc.


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 the geometry tag will be piped into the LaTeX template that ships with Pandoc via the following LaTeX snippet

$if(geometry)$
\usepackage[$for(geometry)$$geometry$$sep$,$endfor$]{geometry}
$endif$

For example, to specify margins that are 2cm in width one would include

---
title: "Habits"
author: John Doe
date: March 22, 2005
geometry: margin=2cm
output: pdf_document
---

For more complex specifications to be passed to the geometry LaTeX package, string options together as you would with LaTeX:

---
title: "Habits"
author: John Doe
date: March 22, 2005
geometry: "left=3cm,right=3cm,top=2cm,bottom=2cm"
output: pdf_document
---

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]). The geometry 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:

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

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:

pandoc -V geometry:paperwidth=4in -V geometry:paperheight=6in -V geometry:margin=.5in -o output.pdf input.md

In more recent versions of pandoc, you can set a number of parameters in the YAML header. You can set the geometry here, for example:

---
title:  Pandoc nice margins example
author: naught101
geometry: margin=3cm
---

body text of document

When pandoc converts it to a pdf, it should have correct margins.


FTR: "recent version" above seems to include Knitr 1.30, before I needed following header:

---
  pdf_document: null
  geometry: margin=1cm
  output: pdf_document
---

Without the additional pdf_document: null the geometry setting had no effect. With it, settings from https://bookdown.org/yihui/rmarkdown-cookbook/latex-variables.html worked.