Dylan Russell
Dylan Russell

Reputation: 1108

Replicating exact Word document format with RMarkdown

In R Markdown, one can out generate a word document with the YAML header output: word_document. This produces a very basic Word document. The basic appearance of this document can be customized by providing a reference template as described here.

However, I frequently produce Word documents of an exact template. Many of the journals I commonly submit to have a particular template they require submission in.

Is there anyway that I can more specifically customize how R Markdown converts to a Word document? Perhaps with additional custom YAML arguments? And also with the ability to calculate counts or other items from the .Rmd file, such as a word count or reference count? For example, ideally I would be able to provide a YAML like so:

---
title: How to make a custom Word template
authors:
    - name: "John Doe, M.D."
      affiliation: University of Texas
    - name: "Jane Doe, M.D."
      affiliation: University of California
    - name: "Foo Bar, M.D."
      affiliation: University of Texas
output: word_document
bibliography: bibliography.bib
keywords: word, pandoc, r
abstract: |
    blah blah blah blah blah
---

And it produce an output like:


How to make a custom Word template

John Doe, M.D.1, Jane Doe, M.D.1, Foo Bar, M.D.1,2

  1. University of Texas
  2. University of California

ABSTRACT WORD COUNT: 4
MANUSCRIPT WORD COUNT: 500
NUMBER OF TABLES: 3
NUMBER OF FIGURES: 5
KEYWORDS: word, pandoc, R
NUMBER OF REFERENCES: 10

Abstract

Blah blah blah blah.


What is the - or is there a - better way to wield control over the Word document output in Rmarkdown in order to produce very specific output templates?

Upvotes: 1

Views: 1142

Answers (1)

tarleb
tarleb

Reputation: 22659

A true template system for docx output is currently not available; see the pandoc issues #7256 and #3109 for details.

However, you can do what you need by using a pandoc Lua filter (R Markdown cookbook section on Lua filters). E.g., for the authors and affiliations, see the author-info-blocks filter. It would be possible to extend the filter such that it suits your needs, but it'd be not as straight-forward as one might wish.

Upvotes: 1

Related Questions