How to produce a PDF title page from an R Markdown document

余生长醉 提交于 2021-01-20 13:46:02

问题


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

Here are the contents of my R Markdown document:

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

And here are the contents of template.tex:

\begin{document}
\maketitle
\end{document}

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

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


回答1:


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

This page shows a simple template.tex example:

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

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.

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}

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.

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).

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


来源:https://stackoverflow.com/questions/65589788/how-to-produce-a-pdf-title-page-from-an-r-markdown-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!