RMarkdown with .tex template overriding content - r

For my RMarkdown document, I would like to use a given Latex template.
Specifically: I try to knit my document to the EMNLP conference template (downloadable zip here).
My mini example below sits in that unzipped folder (containing the .sty and .tex files), so all dependencies work.
---
title: "my sample paper"
author: "A BCD"
date: 'Version: `r format(Sys.time(), "%d %B, %Y")`'
output:
pdf_document:
template: emnlp2021.tex
keep_tex: yes
abstract: "this is an abstract this is an abstract this is an abstract this is an abstract"
---
The body of the paper. The body of the paper. The body of the paper. The body of the paper.
But when knitted, it results in the default pdf that contains the conference instructions (i.e., my RMarkdown content is not shown).
The instructions in the rmarkdown book are great to point it to the .tex but I couldn't find the answer there.
What am I doing wrong?

The template is not a pandoc template. It does not contain the special placeholders that pandoc uses to mark the places where it should actually insert the contents. The EMNLP template is for human processing.
You can take a look at the default latex pandoc template like this:
pandoc -D latex
For example, the placeholder $abstract$ is where the abstract will be placed. Simply replace the fragment after \begin{abstract} in emnlp2021.tex by $abstract$ and compile your Rmarkdown again: the abstract should now be in place. You will have to adapt all other fragments of the template as well!

Related

Purposes of theme, template and includes in beamer presentation generated with R markdown

R markdown presentations with output format beamer can make use of
Classical LaTex beamer themes such as Copenhagen or Madrid or employ custom beamer themes.
Templates , i.e. a template.tex
Includes, e.g. in_header: preamble.tex, before_body: before_body.texand after_body: after_body.tex
which can be implemented in the rmarkdown::beamer_presentation through the YAML-header as follows:
theme: "THEMENAME"
template: template.tex
includes:
- in_header: preamble.tex
- before_body: before_body.tex
- after_body: after_body.tex
What are the specific purposes of these files? That is, what contents should go in each of these files, how do they add or modify the final presentation, and which of the files are equivalent and possibly redundant?
If I understand the rmarkdown lingo correctly, they use the term template for the instructions how the markdown document will be translated into tex, e.g. the default beamer_presentation template for example defines how information from your yaml-header is used in the tex document, e.g. that whatever you put down for title will be translated into \title{...}. It also contains a hole kitchen sink of packages rmarkdown loads by default, many of which make no sense with beamer.
the include hooks offer possibilities to smuggle additional latex code into your document. The difference between the different options is where they will be executed in the intermediate tex document. For example in_header will be included at the end of the preamble. Which of them to use will depend on what you want to add. For most things in_header will be fine.

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:
\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?
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

Bookdown: Cross-reference figure in another file

I have taken the plunge and am preparing a manuscript for publication entirely in RStudio using bookdown. In the main text I would like to cross-reference figures in a separate supporting information .Rmd file.
Let's say this is my main text file called main.Rmd:
---
title: "Main text"
output:
bookdown::pdf_book:
toc: no
---
Here is the main text file. I would like to refer to \#ref(fig:supporting-figure).
Here is the supporting text called supporting.Rmd, and the figure to refer to, saved in the same folder:
---
title: "Supporting info"
output:
bookdown::pdf_book:
toc: no
---
Here is the supporting text.
```{r supporting-figure}
plot(cars)
```
How can I cross-ref supporting-figure in the main text?
I have checked the section on cross-references in Yihui's bookdown manual, but I cannot see how to extend this to cross-references between files.
I also found this question:
Cross-reference figure in a separate Rmarkdown (PDF) file
but the accepted answer does not work for me (perhaps because I am using bookdown rather than base Rmarkdown?)
I am not entirely sure how you are compiling these two files into a single bookdown document, because as it stands they are just two separate R Markdown documents. But there are two issues:
Figures need a caption to be cross-referenced
You can only cross-reference figures which have a caption assigned with fig.cap, as explained here:
If we assign a figure caption to a code chunk via the chunk option
fig.cap, R plots will be put into figure environments, which will be
automatically labeled and numbered, and can also be cross-referenced.
Incorrectly configured bookdown project:
From what I can tell, you have not got the project configured correctly for bookdown:
The main file should be called index.Rmd
Supporting files should not have any YAML
You should include site: bookdown::bookdown_site in the YAML of the main documene
Check out this answer for some tips on a minimal bookdown file.
Solution
index.Rmd
---
title: "Main text"
site: bookdown::bookdown_site
output:
bookdown::pdf_book:
toc: no
---
Here is the main text file. I would like to refer to \#ref(fig:supporting-figure).
supporting.Rmd
Here is the supporting text.
```{r supporting-figure, fig.cap= "Some Figure"}
plot(cars)
```
I ran into the same issue and came up with this solution if you aim at compiling 2 different pdfs. It relies on LaTeX's xr package for cross references: https://stackoverflow.com/a/52532269/576684

knitr/pandoc: article template supporting keywords: and linespacing:

I'm using the latest R Studio, R 3.2.5, rmarkdown 0.9.6, which comes with pandoc 1.15.2 to write a journal article. Submission requires: linespacing: 1.43 and a keywords: line just below the abstract.
When I click Knit PDF I get the default template "C:\R\R-3.2.5\library\rmarkdown\rmd\latex\default-1.15.2.tex" which does not support these fields in the YAML header. I see that default-1.17.02.tex supports linespacing:, but not keywords:.
I tried to modify the latter template to add keywords handling, which seemed pretty straight-forward. The relevant portion of the template, that I called modified-template-1.17.0.2.tex is
$if(title)$
\maketitle
$endif$
$if(abstract)$
\begin{abstract}
$abstract$
\end{abstract}
$endif$
\providecommand{\keywords}[1]{\textbf{\textit{Keywords---}} #1}
$if(keywords)$
\keywords{$keywords$}
$endif$
I used the YAML header below, and pandoc found it; however, it generated .tex syntax errors, unrelated to the keywords: field. I'm thinking that the rmarkdown templates are specific to the version of pandoc, but I have no way to tell.
can anyone help with this?
---
title: "My title"
author: ME
date: '`r format(Sys.time(), "%B %d, %Y")`'
output:
pdf_document:
fig_caption: yes
keep_tex: yes
number_sections: yes
template: "modified-template-1.17.0.2.tex"
keywords: Box M test, HE plots, MANOVA, graphics, ...
abstract: "This paper explores a variety of fascinating topics ... "
---
Note added: I tried this modifying the default-1.15.2.tex template as described above. My result is the same, and the error I get is:
! Undefined control sequence.
l.527 {\centering \includegraphics
pandoc.exe: Error producing PDF from TeX source
Error: pandoc document conversion failed with error 43
One more test: I simply used a copy of the standard default-1.15.2.tex template in my local folder, with the YAML line
template: "default-1.15.2.tex"
This also gives the same error, so I am mystified as to the cause.
This is an answer to the keywords: problem. It is admittedly a kludge, but doesn't require changing the template: Simply include the keywords as an additional paragraph in the abstract in the YAML header.
abstract: "This paper explores a variety of topics ...
**Keywords**: Box's M test; HE plots; MANOVA; graphics
"
To format as a separate paragraph required two blank lines
For posterity, one solution that seems to work is to copy default-1.17.02.tex to the working directory and modify it to include the keywords argument. In the renamed modified-1.17.02.tex, add this after the if block for the author name:
$if(keywords)$
pdfkeywords={$for(keywords)$$keywords$$sep$; $endfor$},
$endif$
And then later in the document, after the if block for placing the abstract:
\providecommand{\keywords}[1]{\textbf{\textit{Keywords---}} #1}
$if(keywords)$
\keywords{$keywords$}
$endif$
In the project Rmd file, we just need to specify that we are using our custom template, specify that graphics = true (to circumnavigate the \includegraphics error) and the keywords themselves:
---
title: "My title"
author: ME
date: '`r format(Sys.time(), "%B %d, %Y")`'
output:
pdf_document:
template: modified-template-1.17.0.2.tex
graphics: yes
keywords: keyword 1; keyword 2; et cetera
abstract: "Abstract here."
---
Body of article here.
Compiling yields a title page with the keywords field present:
EDIT: The OP also mentioned line spacing. When using the 1.17.02.tex style (used automatically if the most recent version of Pandoc is available, either through manual install or by using the latest RStudio preview build), the ability to adjust the line spacing is done by setting linestretch: 1 for single space, and linestretch: 2 for double anywhere in the YAML header.

How to create customized chapter heading in a Dynamic Document R and knitr

I´m not a LaTex user...actually started learning something about it as consequence of dynamic document I´m working with for a book document class I´m writing in R and knitr.
I´ve tried to customize the chapter headings, but meanwhile I did not get....I found the way to do this by LaTex (https://tex.stackexchange.com/questions/73421/how-to-create-specific-chapter-style-in-book-documentclass) however I´m not knowing how to address by RMardkown.
Does anybody could help me with that? How can I arrange the below LaTex commands to be triggered by RMarkdown (by .sty file?) or something similar way to customize the chapter heading in output pdf?
\documentclass{book}
\usepackage{titlesec}
\usepackage{lipsum} % just to generate text for the example
\titleformat{\chapter}[display]
{\bfseries\Large}
{\filright\MakeUppercase{\chaptertitlename} \Huge\thechapter}
{1ex}
{\titlerule\vspace{1ex}\filleft}
[\vspace{1ex}\titlerule]
Thanks
Fabio
You can put the stuff you want in the header, except the documentclass line, in a seperate tex file (I use header.tex below). You can then specify in the header of your R-markdown file that you want to include this file in the header. The documentclass van also be set in the header of your R-markdown file. More info on this can be found on the rstudio site.
Below an example:
---
title: "Untitled"
output:
pdf_document:
includes:
in_header: header.tex
documentclass: book
---
\chapter{Introduction}
Section
=======
This is an R Markdown document.
header.tex looks like:
\usepackage{titlesec}
\usepackage{lipsum} % just to generate text for the example
\titleformat{\chapter}[display]
{\bfseries\Large}
{\filright\MakeUppercase{\chaptertitlename} \Huge\thechapter}
{1ex}
{\titlerule\vspace{1ex}\filleft}
[\vspace{1ex}\titlerule]

Resources