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

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

问题描述

我已经在RStudio中创建了一个RMarkdown文件,并设法将其与knitr编织为HTML和.md文件.接下来,我使用pandoc将.md文件转换为PDF文件(如果尝试从.html文件转换,则会出现错误).但是,生成的PDF的边距很大(例如 http://johnmacfarlane.net/pandoc/demo/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.

例如,几何 LaTeX软件包可用于更改页面的页边距.但是,您需要通过将Pandoc包含在应用于转换后的md文件的LaTeX标头中,来告诉Pandoc使用此方法.

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命令行参数和模板"部分.本质上,要么找到并修改默认模板以包含要使用的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乘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天全站免登陆