如何从 R Markdown 文档生成 PDF 标题页 [英] How to produce a PDF title page from an R Markdown document

查看:21
本文介绍了如何从 R Markdown 文档生成 PDF 标题页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将我的 R Markdown 文档编织成 pdf 时,我想生成一个自定义标题页.

I would like to produce a custom title page when I knit my R Markdown document to pdf.

这是我的 R Markdown 文档的内容:

Here are the contents of my R Markdown document:

---
output:
    pdf_document:
        template: template.tex
---
# abstract
this is just some text 

这里是template.tex的内容:

And here are the contents of template.tex:

egin{document}
maketitle
end{document}

当我编织到 pdf 时,没有出现任何 R Markdown 文本.只有模板可以.

When I knit to pdf none of the R Markdown text appears. Only the template does.

谁能解释我在使用乳胶模板后如何输入 R Markdown?

Could anyone explain how I could type in R Markdown after using a latex template?

推荐答案

您的 R Markdown 文档似乎正确.问题在于您的 template.tex 文档.

Your R Markdown document seems correct. The issue lies in your template.tex document.

这个页面展示了一个简单的template.tex例子:

This page shows a simple template.tex example:

documentclass{article}
egin{document}
$body$
end{document}

这个例子的重点是展示你的 markdown 的内容将被转换为 LaTeX 并插入到 $body$ 所在的位置.因此,您的降价内容没有显示出来,因为您的模板中没有 $body$ 存在可能插入生成的 LaTeX 的位置.

The point of the example is to showcase that the contents of your markdown will be converted to LaTeX and inserted into where the $body$ is located. Therefore, the contents of your markdown are not showing up because there is no $body$ present in your template where the generated LaTeX might be inserted.

但是,当我尝试使用简单模板时,我收到关于未定义控制序列的错误.这是因为正在插入特定的 LaTeX 命令,但未加载包含该命令的必要 LaTeX 包.

However, when I try to use the simple template, I receive an error about an undefined control sequence. This is because a specific LaTeX command is being inserted, but a requisite LaTeX package containing that command is not being loaded.

我不确定您希望您的标题页是什么样的,但这里有一个有效的简单模板示例,其中包含仅由标题组成的标题页.

I am unsure what you want your title page to look like, but here is a working, simple-example of a template that contains a title page consisting of just the title.

documentclass[]{report} % use report class because it contains a title page

usepackage{hyperref}    % load the hyperref package to avoid an undefined 
                         % control sequence error

	itle{$title$}          % set title to what you passed from the yaml in the R 
                         % Markdown document

author{}                % set author to empty to avoid warning message about 
                         % no author set; use author{$author$} if you want to 
                         % set author from the yaml like `author: "My Name"`

date{}                  % set date to empty to avoid a date on the title page;
                         % use date{$date$} to set from yaml `date: 2021-01-08`

egin{document}
    maketitle
    $body$
end{document}

但是该模板非常简单,当您尝试在 R Markdown 文档中执行的操作超出您所展示的内容时,您很快就会遇到其他未定义的错误.

But that template is pretty simple, and you will quickly run into additional undefined errors as you attempt to do more in your R Markdown document than what you have showed.

我建议从 Pandoc 的默认 LaTeX 模板开始,然后对其进行调整以到达您想去的地方.Pandoc 的默认 LaTeX 模板可以在 https://github.com/找到jgm/pandoc/tree/master/data/templates(命名为default.latex).

I recommend starting with the default LaTeX template of Pandoc and tweaking that to get where you want to go. The default LaTeX template of Pandoc can be found at https://github.com/jgm/pandoc/tree/master/data/templates (named default.latex).

事实上,您也许可以按原样使用默认模板,只需更改您的 yaml,因为以下将创建一个如上所示的标题页:

In fact, you might be able to use the default template as is and just change your yaml because the following will create a title page as above:

---
title: "My Super Awesome Title"
documentclass: report
output: pdf_document
---
# abstract
this is just some text

这篇关于如何从 R Markdown 文档生成 PDF 标题页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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