且构网

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

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

更新时间:2023-01-05 21:36:31

您的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}
\begin{document}
$body$
\end{document}

该示例的目的是演示您的Markdown内容将转换为LaTeX,并插入到 $ body $ 所在的位置.因此,由于您的模板中没有 $ body $ 可能会插入生成的LaTeX的模板,因此不会显示markdown的内容.

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

\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文档中进行比所显示的更多的操作时,您将很快遇到其他未定义的错误.

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