且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从 R Markdown 文档生成 PDF 标题页

更新时间:2023-01-05 21:40:24

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