Set margin size when converting from Markdown to PDF with pandoc
Asked Answered
E

3

270

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.

Encyclopedic answered 22/11, 2012 at 15:31 Comment(0)
G
378

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
Grand answered 22/11, 2012 at 15:40 Comment(11)
If you are using the latest version of Pandoc, you can also set this using the variable command instead of having to make a template from scratch or hard-coding the margins in the default template. For example, for 1 inch margins, you can use pandoc -V geometry:margin=1in -o output.pdf input.md.Additory
@mrdwab That is exactly what I need. Thanks a lot. Want to post that as an answer so I can Accept it?Encyclopedic
@mchangun, I've edited Gavin's answer to include my comment since it is a valid one that might be useful for users who want to use other custom LaTeX packages not included in the default Pandoc templates.Additory
Very new to pandoc. I was finding that the -V geometry:margin=1in option was having no effect. Via googling, I found somewhere (sorry lost the link) documentation saying that this option has no effect if using the default template. I just want to convert a one page markdown file to a PDF to send to someone as a flier. My quick solution: Edit the template file directly and add the line \usepackage[margin=1.0in]{geometry}. On my install, the file is in: /usr/share/pandoc-1.9.1.1/templatesSequent
Is there a way, from the command line to affect just one of the margins? My entire book has a giant gap at the bottom and I just want to affect that margin. -V geometry:margin:bottom doesn't seem to work.Kelcey
@AustinFatheree I've updated the answer to address your question. It is easiest to do this via the new(ish) geometry element in the YAML metadata.Grand
Does not seem to work for pandoc 2.9.2.1 when using --pdf-engine=wkhtmltopdfOracle
@Oracle I guess there is no LaTeX involved in that conversion of HTML to PDF. I would suggest you ask a new question as that seems a more unique and specific request than the question posed here originally. I had a quick look and I couldn't see anything that immediately jumped out at me where one could configure things like this, but I'd start with wkhtml itself to see what customisation is possibleGrand
@Oracle oh, actually missed this on first pass. See pandoc.org/MANUAL.html#variables-for-wkhtmltopdf and I think you will need to pass this via the V mechanism, so in rmarkdown that would be via the pandoc_args element of the YAML header. (not tested). If you can throw together a minimal example I post a new Q I can take a closer look? (just link it here in a comment)Grand
This is the solution that finally worked for me, #46041362Oracle
When i set geometry margins it moved only the title and author but it did not include the kable tables i added in the pdf as well.Darra
S
125

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.

Selfinductance answered 15/10, 2014 at 0:14 Comment(1)
This is a much better solution! It is quite useful when you are automatically building the document in an editor like Kile or Sublime Text 2/3 and changing command line arguments would be a pain.Gummosis
S
3

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.

Sizeable answered 26/11, 2020 at 9:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.