knitr/pandoc: article template supporting keywords

2019-07-25 06:04发布

问题:

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.

回答1:

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.



回答2:

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