Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
813 views
in Technique[技术] by (71.8m points)

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

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:

egin{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?

question from:https://stackoverflow.com/questions/65599708/how-to-add-text-in-r-markdown-when-using-a-latex-template

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

This page shows a simple template.tex example:

documentclass{article}
egin{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

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}

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...