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

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.

Related

RMarkdown with .tex template overriding content

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!

How to eliminate incompatible color warnings in R bookdown?

I'm using RBookdown with multiple chapters such as this minimal example.
After successfully implementing the custom block instructions here everything looks great. The boxes appear in both html and pdf formats, as intended.
However, I am using color fonts across the document, and upon inclusion of the latex custom block preamble items.
\usepackage{tcolorbox}
\newtcolorbox{blackbox}{
colback=black,
colframe=orange,
coltext=white,
boxsep=5pt,
arc=4pt}
now the compile error appears as
Warning: Package xcolor Warning: Incompatible color definition on input line 215.
I see no problems with the colors, they are all working as intended. Similar for the custom blocks, they all appear fine. Unless anyone has a better suggestion, I will just ignore the xcolor warnings.
I found similar discussions here and here and here but I still haven't found any fix to the error messages.
Note: Color is implemented using the solution in previous post
UPDATE: To create MWE, use this minimal example
and edit the index.Rmd file to look as follows
---
title: "A Minimal Book Example"
author: "Yihui Xie"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
output:
bookdown::html_book
documentclass: book
bibliography: [book.bib, packages.bib]
biblio-style: apalike
link-citations: yes
github-repo: rstudio/bookdown-demo
description: "This is a minimal example of using the bookdown package to write a book. The output format for this example is bookdown::gitbook."
---
```{r echo=FALSE}
colorize <- function(x, color) {
if (knitr::is_latex_output()) {
sprintf("\\textcolor{%s}{%s}", color, x)
} else if (knitr::is_html_output()) {
sprintf("<span style='color: %s;'>%s</span>",
color,
x)
} else x
}
```
# Prerequisites
This is a _sample_ book written in **Markdown**. You can use anything that Pandoc's Markdown supports, e.g., a math equation $a^2 + b^2 = c^2$. This is a _sample_ book written in **Markdown** with `r colorize("desired brown text", "brown")` which appears as intended.
Then also edit the preamble.tex file to include the line below.
\usepackage{tcolorbox}
Then use R command
bookdown::render_book("index.Rmd", "bookdown::pdf_book")
to compile into pdf, and the described errors will appear.

Output created by pagedown::html_paged not displaying properly

I am working on a project in RStudio that takes in multiple rmd files and a yml file and uses the bookdown render_book() function to produce paginated output that can be converted to pdf.
The bookdown code is as follows:
bookdown::render_book("prog1.Rmd", "bookdown::gitbook", config_file = config_yml)
bookdown::render_book("prog1.Rmd", "pagedown::html_paged", config_file = config_yml)
The config_yml file:
book_filename: "Report Output"
delete_merged_file: true
new_session: yes
rmd_files: ["Start.Rmd", "prog1.rmd", "appendix.rmd"]
output_dir: ./fakedir/
language:
label:
fig: "FIGURE "
tab: "TABLE "
before_chapter_script: "./fakename.R"
The specifications from the Start.rmd:
---
title: "Report"
subtitle: "`r text`"
date: "`r datetext`"
site: bookdown::bookdown_site
classoption: oneside
documentclass: book
link-citations: yes
description: "My Report"
---
Historically this has caused no issues, with the first bookdown call creating an html doc and the second creating a paginated version. Recently, with no code changes, the paginated version will not open properly once created. It only shows the title page followed by two blank pages. I have thus far been unable to find a reason for this. I am hoping for either a solution to this issue or an alternative method to producing a paginated document. Let me know if more info/code is needed.

Using a custom template for Rmd pdf without changing current setup

I have recently learned that modifying the default (Pandoc?) template for a Rmd pdf report gives access to some neat customisation.
So in order to modify the default template I first move it to my working directory using:
file.copy(system.file("rmd/latex/default-1.17.0.2.tex", package ="rmarkdown"), "template.tex")
Next I type up my Rmd file:
---
output:
pdf_document:
template: template.tex
keep_tex: true
latex_engine: xelatex
includes:
in_header: in_header.tex
number_sections: true
---
This is a rmd kind of document.
Where in_header.tex includes only one line
\geometry{a4paper, top=38mm, left=45mm, right=45mm}
I first just want to run the Rmd file with the template as is (expecting no change) but the report fails to compile if and only if I include the template - otherwise it runs without an issue.
The error is
! Undefined control sequence.
l.58 \geometry
[...]
Question
How can I use the template argument to build on an editable template in my working folder without causing any other change to my current setup.
Info
> R.Version()[["version.string"]]
[1] "R version 3.5.1 (2018-07-02)"
> packageVersion("rmarkdown")
[1] ‘1.10’
> packageVersion("knitr")
[1] ‘1.20’
As you are already driving this from R, you may as well use the (excellent) example set up by the rticles package which provides a larged number of LaTeX customizations for (academic) papers.
And you can then run with that them. For example, over the last two years I added these packages:
tint for a modern 'Tufte-alike' writeup
pinp for very nice two-column pdf vignettes
link for LaTeX letters and some extra
binb for variants of beaner packages
This allows you to
set a custom template.tex
include whichever LaTeX class files / style files you need
programmatically set options
I find this preferable to copying stanzas around which I did previously (eg for slides).
The rmarkdown::pdf_output function seems to do a few things differently when the default template is used. For example, it sets the variable graphics=yes. I suspect that it also sets geometry, but I have not seen where it does that. Anyway, if you want to use a custom template based on the default template you are "fighting" with this block from the template:
$if(geometry)$
\usepackage[$for(geometry)$$geometry$$sep$,$endfor$]{geometry}
$endif$
The geometry package is only loaded, when the geometry header is defined. One simple solution to your problem is to move the geometry definition from the header include to the YAML header:
---
geometry:
- a4paper
- top=38mm
- left=45mm
- right=45mm
output:
pdf_document:
template: template.tex
keep_tex: true
latex_engine: xelatex
includes:
in_header: in_header.tex
number_sections: true
---
This is a rmd kind of document.
With this you can remove \geometry{...} from in_header.tex.

How to add a latex package to R Journal template from rticle package in R

I am using rticle package by RStudio to write an article in R using rmarkdown package.
In the draft template there is an option
...
preamble: >
% Any extra latex you need in the preamble
output: rticles::rjournal_article
...
Where I suspect I can add lines like \usepackage{package_name} or set a file name in which I load all desired Latex packages.
The first approach
I've tried to add one single package to preamble section like this but have stacked on an error
---
title: Capitalized Title Here
author:
- name: Author One
affiliation: Affiliation
address:
- line 1
- line 2
email: author1#work
- name: Author Two
affiliation: Affiliation
address:
- line 1
- line 2
email: author2#work
abstract: >
An abstract of less than 150 words.
preamble: >
\usepackage{rotating}
% Any extra latex you need in the preamble
output: rticles::rjournal_article
---
\begin{sidewaystable}
\centering
\caption{Your caption here}
\begin{tabular}{ll}
First First & First Second\\
Second First & Second Second
\end{tabular}
\end{sidewaystable}
The first approach code end
BŁĄD: running 'texi2dvi' on 'RJwrapper.tex' failed
LaTeX errors:
D:\RTCGA.data\Untitled\Untitled.tex:11: LaTeX Error: Environment sidewaystable
undefined.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help
D:\RTCGA.data\Untitled\Untitled.tex:13: Package caption Error: \caption outside
float.
See the caption package documentation for explanation.
Type H <return> for immediate help
D:\RTCGA.data\Untitled\Untitled.tex:18: LaTeX Error: \begin{article} on input l
ine 20 ended by \end{sidewaystable}.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help
Dodatkowo: Warning message:
uruchomione polecenie '"C:\PROGRA~1\MIKTEX~1.9\miktex\bin\x64\texi2dvi.exe" --quiet --pdf "RJwrapper.tex" --max-iterations=20 -I "C:/PROGRA~1/R/R-32~1.0/share/texmf/tex/latex" -I "C:/PROGRA~1/R/R-32~1.0/share/texmf/bibtex/bst"' otrzymało status 1
Wykonywanie wstrzymane
I've seen the same preamble - ignoring behavior in this issue https://github.com/rstudio/rticles/issues/11, but there was no response for that.
I've also tried to specify file named header.tex that contains a line \usepackage{rotating} in a last argument of a yaml like this but also there happend to be an error.
---
title: Capitalized Title Here
author:
- name: Author One
affiliation: Affiliation
address:
- line 1
- line 2
email: author1#work
- name: Author Two
affiliation: Affiliation
address:
- line 1
- line 2
email: author2#work
abstract: >
An abstract of less than 150 words.
preamble: >
\usepackage{rotating}
output:
rticles::rjournal_article:
includes:
in_header: header.tex
---
BŁĄD: running 'texi2dvi' on 'RJwrapper.tex' failed
LaTeX errors:
D:\RTCGA.data\rticle\rticle.tex:145: LaTeX Error: Environment sidewaystable und
efined.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help
D:\RTCGA.data\rticle\rticle.tex:147: Package caption Error: \caption outside fl
oat.
See the caption package documentation for explanation.
Type H <return> for immediate help
D:\RTCGA.data\rticle\rticle.tex:192: LaTeX Error: \begin{article} on input line
20 ended by \end{sidewaystable}.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help
Dodatkowo: Warning message:
uruchomione polecenie '"C:\PROGRA~1\MIKTEX~1.9\miktex\bin\x64\texi2dvi.exe" --quiet --pdf "RJwrapper.tex" --max-iterations=20 -I "C:/PROGRA~1/R/R-32~1.0/share/texmf/tex/latex" -I "C:/PROGRA~1/R/R-32~1.0/share/texmf/bibtex/bst"' otrzymało status 1
Wykonywanie wstrzymane
EDIT
When I specify yaml in a way it is intended to work for pdf_document output
...
preamble: >
\usepackage{rotating}
output:
rticles::rjournal_article:
includes:
in_header: header.tex
...
I receive an error that
Error in (function () :
unused (includes = list(in_header = "header.tex"))
EDIT 2
Even for an empty new document I get the same error
---
title: Capitalized Title Here
author:
- name: Author One
affiliation: Affiliation
address:
- line 1
- line 2
email: author1#work
- name: Author Two
affiliation: Affiliation
address:
- line 1
- line 2
email: author2#work
abstract: >
An abstract of less than 150 words.
preamble: >
% Any extra latex you need in the preamble
output:
rticles::rjournal_article:
includes:
in_header: header.tex
---
Hello World
The header.tex document (which is in the same directory as saved empty new file) contains only 1 line:
\usepackage{rotating}
I am aware of in_header option in regular pdf_document output format (see even my files with RMD extensions on github) but it looks like to does not work with rticles::rjournal_article
EDIT 3
I am interested in adding other latex package than those that are included automatically in the RJwrappter.tex file which is generated after render of the .RMD file.
\documentclass[a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{RJournal}
\usepackage{amsmath,amssymb,array}
\usepackage{booktabs}
%% load any required packages here
\begin{document}
%% do not edit, for illustration only
\sectionhead{Contributed research article}
\volume{XX}
\volnumber{YY}
\year{20ZZ}
\month{AAAA}
\begin{article}
\input{rticle}
\end{article}
\end{document}
Anyone maybe knows how to solve this?
I am using MiKTeX 2.9.
I know this question's old but I just had the same problem as Marcin and wanted to share my solution.
What worked for me was changing the template.tex file in the rticles package for the ASA article. At the top of the template, where they have all the other \usepackage commands, just add your package(s). I had the same problem that you did and neither of the typical solutions as provided in the first answer worked for me.
To edit the template file, go to the rticles directory in your package library folder (.libPaths() in R), then open up rmarkdown/templates/rjournal_article/resources/RJWrapper.tex. Add your package amongst all the others. This should work for any of the other *_article/resources/template.tex files.
No, that is wrong. In the YAML header declare that you want an include.
See the rmarkdown reference:
---
title: "Habits"
output:
pdf_document:
includes:
in_header: header.tex
before_body: doc_prefix.tex
after_body: doc_suffix.tex
---
I have used header.tex for this purpose in several documents.
Edit: As you appear to get lost in the woods, here is a simplyfied example started from the default useR abstract template:
---
title: "Title of Your Submission"
author:
- name: FirstNameA LastNameA
keywords: First, Second, ... up to 5 keywords
output:
pdf_document:
includes:
in_header: marcin_header.tex
---
Content to be added here.
which I coupled with the simplest possible header called marcin_header.tex (placed in the same directory) and containing just
\usepackage{booktabs}
Works for me.
Edit 2: Oh boy.
---
title: Capitalized Title Here
author:
- name: Author One
affiliation: Affiliation
address:
- line 1
- line 2
email: author1#work
- name: Author Two
affiliation: Affiliation
address:
- line 1
- line 2
email: author2#work
abstract: >
An abstract of less than 150 words.
preamble: >
% Any extra latex you need in the preamble
\usepackage{booktabs}
output: rticles::rjournal_article
---
## Introduction
Rest omitted.
As dash2 mentioned you need to edit one of your rticles templates to take advantage of the header-includes or preamble clause. For example for the rticles::aea_article format the template could be in:
/home/user/R/x86_64-pc-linux-gnu-library/3.3/rticles/rmarkdown/templates/aea_article/resources/template.tex
This is for a linux system, but there's a location on your computer, whatever the OS, where R libraries are stored and you need to edit this file to include the snippet that dash2 provided.
There is no easy way to do this, as of Oct 16. What you need to do is find the relevant template and edit it to include the lines:
$for(header-includes)$
$header-includes$
$endfor$
You can find the template by looking at the source code for e.g. rjournal_article.
I managed to include the float package by adding
header-includes:
- \usepackage{float}
into my yaml-header at the beginning of the frontiers-template

Resources