Set Margin Size When Converting from Markdown to PDF with Pandoc

Set margin size when converting from Markdown to PDF with 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

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

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"

Pandoc - Can you change margins and fontsize simultaneously?

Try this:

pandoc documentation.md -V geometry:margin=1in -V fontsize:12pt -s -o documentation.pdf

Pandoc's FAQs state:

How do I change the margins in PDF output?


The option

-V geometry:margin=1in

will set the margins to one inch on each side.

Note that geometry:margin=1in is the value of the -V flag. However, you have the filename documentation.md between the flag and its value. Therefore, you are causing the value of the flag to be documentation.md and geometry:margin=1in is assumed to be a filename. After all, any string of text no preceded by a flag should be a filename (which explains the "No such file or directory" error).

By way of explanation, the documentation for the -V flag gives this format:

-V KEY[:VAL]

Note that the brackets in [:VAL] indicate that that part is optional. So -V KEY is completely valid with no value, which means that -V documentation.md resulted in documentation.md being the KEY of the -V flag (with a default VAL of true as per the docs).

Admittedly, -V geometry:margin=1in is an especially weird case and its easy to see how one might be confused by it. In this case however, -V is the flag, geometry is the "KEY" and margin=1in is the "VAL". I realize that margin=1in looks like a KEY=VAL, but in this instance it is all a "VAL" on its own. Presumably, Pandoc does some further processing on it later to break the "VAL" up into its parts.

Of course, fontsize is another variable, so you need a second -V flag to define that variable: -V fontsize:12pt.

Finally, the -s flag does not accept a value, so I moved it so that that is clear.

pdf's made from markdown with pandoc eliminate the double space after a period

Given that single-spaces after a sentence is not just a web convention but a print convention too it's highly unlikely that there will be an elegant technical solution for achieving this bad-practice of typography. So the solution in this case is easy, use proper practice of typography :)

Pandoc - Convert from Markdown to PDF and Set File Name From Metadata Title

You'll need a helper file title.plain with content

$title$

With that, the following command should do:

for f in *.md; do
pandoc "$f" -o "$(pandoc --template=title.plain -t plain "$f")".pdf
done


Related Topics



Leave a reply



Submit