0
votes

I'm in an org that likes to cram as much info onto each page of a Word document as possible. For example, they prefer to show the document Title, Date, Author in headers/footers rather than in the body of the document. I can display these fields in the headers/footers of a Word document compiled from bookdown by creating a .docx template and adding the Word field codes TITLE, CREATEDATE, and AUTHOR to the desired locations in headers/footers of the .docx template. This works great. But the problem is that when I compile to Word, the Title, Date, and Author still show in the body of the Word document. This persists even if I delete them from the body of the .docx template. Yes, I can easily delete these from the output Word doc before sharing. But it would be nice if I could have a single .docx template which got things just right. Is there a straightforward way to do this?

UPDATE: I don't want to leave the title, date, and author fields blank in the Rmd YAML header because I still want them to appear in particular spots in the headers/footers of the docx output and appear as they normally would in the pdf output.

1
Does this work for you, or does it add it back in? I have had some success in the past editing the template. rmarkdown.rstudio.com/articles_docx.htmlAdam
It doesn't work for me. This link talks about getting control of the style of the output Title. But as far as I can tell, there's not a way to tell the Word template that the style of the output Title should be null, that is, print no characters, newlines, etc.lowndrul

1 Answers

1
votes

Case 1

If you simply leave the title, author, and date fields blank in the yaml, they will not be displayed in the output. Here is a reproducible example. I use a template from the bookdownplus package:

bookdownplus::bookdownplus('demo', to = 'demo', more_output = 'word_document2')

In the folder 'demo', you can edit the yaml header of 'index.Rmd' like this:

title:
author:
date:

Build the book and you will get an ms-word document free of title, author and date.


Case 2

If you need these fields for html or pdf output only except the word document, use |:

title: |
  | "title"
author: |
  | "author"
date: |
  | "`r Sys.Date()`"

Then the title, author and date will appear in pdf and html output and disappear in MS-Word output.


Case 3

If you want the title, author and date to appear only in the header/footer of the word document, you can play some tricks in the word template. For example, you can hide them manually like this:

  • Open the word template with MS-Word
  • Press ctrl + shift + s to open the Apply Styles toolbox.
  • Select the field, say, Title, and click Modify.
    • Change the font colour as the background colour (usually 'white').
    • Click Format - Paragraph. Set Spacing as 0, and Line Spacingas the smallest value.
    • Click OK.
    • Click OK again.
  • Now the title is hidden. Yes, it is still there, but nobody can see it. Save the template.

Now use this template for bookdown. The title will appear as you like.