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

查看:73
本文介绍了如何从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:

\begin{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}
\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

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

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