使用 pandoc 从 Markdown 转换为 PDF 时设置边距大小 [英] Set margin size when converting from Markdown to PDF with pandoc

查看:41
本文介绍了使用 pandoc 从 Markdown 转换为 PDF 时设置边距大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 RStudio 中创建了一个 RMarkdown 文件,并设法用 knitr 将它编织成一个 HTML 和 .md 文件.接下来,我使用 pandoc 将 .md 文件转换为 PDF 文件(如果我尝试从 .html 文件进行转换,则会收到错误消息).但是,生成的 PDF 有很大的边距(例如 http://johnmacfarlane.net/pandoc/演示/example13.pdf).我怎样才能让 pandoc 生产利润较小的东西?我浏览了 pandoc 用户指南,但没有发现任何有用的东西.

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.

推荐答案

rmarkdown 和 pandoc 的最新版本

rmarkdown 的更新版本中,可以通过顶级元素 geometry 在 YAML 标头中设置边距.您在 geometry 标记中指定的内容将通过以下 LaTeX 片段传送到 Pandoc 附带的 LaTeX 模板中

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$

例如,要指定宽度为 2 厘米的边距,可以包括

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
---

对于要传递给 geometry LaTeX 包的更复杂的规范,将选项串在一起,就像使用 LaTeX 一样:

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
---

<小时>

原答案

这是一个 LaTeX 问题,因为 Pandoc 通过 LaTeX 呈现为 PDF - 您链接到的内容代表 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.

geometry LaTeX 包可以用来改变页面的边距.但是,您需要一种方法来告诉 Pandoc 使用它,方法是将其包含在应用于转换后的 md 文件的 LaTeX 标头中.

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.

Pandoc 用户指南中记录了如何执行此操作.特别参见 --template=FILE 命令行参数和 Templates 部分.本质上,要么找到并修改默认模板以包含您要使用的 LaTeX 指令,要么从头开始创建自己的模板并将其放置在适当的位置;查看 --data-dir 命令行参数.

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.

如果您使用的是最新版本的 Pandoc,另一种选择是使用变量参数(使用 -V KEY[=VAL]--variable=KEY[:VAL]).geometry 包已于 2012 年 5 月添加到默认 LaTeX 模板中(请参阅 本次讨论).因此,如果您想更改页边距,您可以使用:

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

您也可以指定多个变量值.例如,如果您想创建一个 4 x 6 英寸、边距为半英寸的 pdf,您可以使用:

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

这篇关于使用 pandoc 从 Markdown 转换为 PDF 时设置边距大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆