0

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.

lowndrul
  • 3,715
  • 7
  • 36
  • 54
  • Does this work for you, or does it add it back in? I have had some success in the past editing the template. https://rmarkdown.rstudio.com/articles_docx.html –  Apr 29 '19 at 13:42
  • 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 Apr 29 '19 at 16:13

1 Answers1

1

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.

pzhao
  • 335
  • 1
  • 10
  • I still want the Title, Date, and Author to appear in desired locations in the headers/footers of the docx output. Just not in the body of the document docx. Also, I want the Title, Data, and Author to appear as they normally would in pdf output. I updated the original question to emphasize this more clearly. – lowndrul May 10 '19 at 13:32
  • @lowndrul I see. Maybe you could try `|`. See the update in my answer. It would be helpful if you share a reproducible example, such as your docx template. – pzhao May 10 '19 at 13:52
  • This is a clever trick that I'm definitely going to use in the future. But unfortunately, this doesn't work for my current situation where I still need to place the Title, Author, Date in the headers/footers of the docx output. With this approach, the Title, Author, and Date no longer appear as document properties in the docx output. So nothing can be done with them. – lowndrul May 13 '19 at 17:35
  • May there be a docx template we can play with. – pzhao May 15 '19 at 06:08
  • I just edited my original question to include a link to a docx template. Will appear once edit passes peer review. In the meantime, providing it [here](https://www.filehosting.org/file/details/800799/template01.docx) as well. – lowndrul May 16 '19 at 16:25
  • @lowndrul Thanks for the template. I've got a trick. Please see the update 'Case 3' in my answer. – pzhao May 27 '19 at 12:32