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



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

以下是我的R Markdown文档的内容:

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

以下是template.tex:的内容

begin{document}
maketitle
end{document}

当我编织成pdf时,没有出现R Markdown文本。只有模板可以。

有人能解释一下我如何在使用乳胶模板后输入R Markdown吗?

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

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

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

该示例的重点是展示您的降价内容将转换为LaTeX并插入到$body$所在的位置。因此,您的markdown的内容不会显示,因为您的模板中没有$body$,可能会在其中插入生成的LaTeX。

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

我不确定你希望你的标题页是什么样子,但这里有一个简单的模板示例,它包含一个仅由标题组成的标题页。

documentclass[]{report} % use report class because it contains a title page
usepackage{hyperref}    % load the hyperref package to avoid an undefined 
% control sequence error
title{$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`
begin{document}
maketitle
$body$
end{document}

但这个模板非常简单,当你试图在你的R Markdown文档中做更多的事情时,你会很快遇到其他未定义的错误。

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

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

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

最新更新