drmariod
drmariod

Reputation: 11762

Change headline style when writing a word document using r markdown

I am using R and markdown and the bookdown package to write word documents. I have a template file with very specific custom styles for the headline and the user expects the headlines to be in this specific style.

How can I change the default headline style when writing the word document. As defined here in the pandoc manual under --reference-doc, The default level one headline style will be Heading 1. I want to use Custom Headline 1 instead.

It has to be the different style, so just adapting the template.docx so the headlines look the same as the preferred style is not an option.

How can I change this default behaviour?

Or are there ways to later modify the created docx document and rename all the Heading x styles into Custom Headline x?
So far I found this SO question pandoc - replace heading with custom style for Word docx which looks like it is doing what I am looking for. Searching further for R solutions, I found the package pandocfilters, but I don't see how to use it to change the style of the specific headline.

Upvotes: 1

Views: 487

Answers (1)

tarleb
tarleb

Reputation: 22544

A simple method would be to use a Lua filter. E.g., this should do it:

local heading_styles = {
  "CustomHeading1",
  "CustomHeading2",
  "CustomHeading3",
  "CustomHeading4",
  "CustomHeading5",
  "CustomHeading6",
}

function Header (el)
  local attr = el.attr
  attr.attributes['custom-style'] = heading_styles[el.level]
  return pandoc.Div(pandoc.Para(el.content), attr)
end

Save the code to a file custom-headings.lua and add this to your YAML:

---
output:
  word_document:
    pandoc_args:
      - '--lua-filter=custom-headings.lua'
---

See the R Markdown cookbook for details.

Upvotes: 1

Related Questions