how to start toc on a new page after title page in rmd to word doc output. currently the toc starts on the title page itself just below the title. I want to use rmd to generate word document output. I know there are options available in pdf output but cant seem to find any solution for word document output for this issue. Thanks in advance.
I ended up using some code from a post by Garrick Aden-Buie to move my table of contents in blog posts below the "intro" paragraphs. Maybe something like this would work for you?
(I notoriously have punctuation in my headers so I did have to make some slight changes to his gist code.)
@aosmith Thanks for your quick revert. render_toc() works to enable me to have the toc at any location on my rendered word doc. But still it doesnt help me to start the toc on a fresh page after the title page. The toc is rendered immediately after the title (see image below). Any ideas forcing toc onto a fresh page, short of inserting a page break manually after the word doc is rendered?
in addition of the workaround to move the toc where you want, you can have another one to insert a pagebreak where you want.
Currently, PANDOC does not have a pagebreak feature for other document than latex. However, pandoc allows to insert raw code from your desired output.
see https://pandoc.org/MANUAL.html#generic-raw-attribute
With a little research, it seems like a page break in word raw code is
<w:p><w:r><w:br w:type="page"/></w:r></w:p>
So you can use that to create a pagebreak in word output.
Try this:
---
title: My main title
output: word_document
---
```{=openxml}
<w:p><w:r><w:br w:type="page"/></w:r></w:p>
```
# First Header
You should get first header in the second page.
If you move the toc after, you should get what you want.
Tell me if it works as expected.
I may find time to make a feature request in Rmarkdown or build a small package to add this feature more easily.
This works great! In some fabulous timing on my part , I'm working with Rmd -> Word for the first time in ages today and just decided I might need a page break.
I saw some other approaches, including using a Lua filter and \newpage
:
And using a style element in an unintended way:
https://datascienceplus.com/r-markdown-how-to-insert-page-breaks-in-a-ms-word-document/
But your option seemed easiest to me, so that's what I went with.
If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:
Oh yeah I remember about those lua filter. It is not so easy to manipulate but it would help to use pagebreak in any type of document.
My solution here is just quick and simple if you want to produce a word output. If you change to other format, it won't work.
There may be something to do to
- either offer to use easily the lua filter (using
--lua-filter
command) - either integrate the mechanic in an R package directly (without lua)
A new project for this summer...
Using lua filter is as easy as a new yaml header line.
This example should work if you download the lua filter your working directory where the rmd file is compiled
download.file("https://github.com/pandoc/lua-filters/raw/master/pagebreak/pagebreak.lua", destfile = "pagebreak.lua", mode = "wb")
After that, you just add this lua filter using pandoc_args
option and use \newpage
(the latex syntax) whereever you want in the document. This will be parsed and replaced by the correct page break syntax of you desired output format.
---
title: "test-lua-pagebreak"
author: "C. Dervieux"
date: "29/07/2019"
output:
word_document:
pandoc_args: ["--lua-filter=pagebreak.lua"]
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
\newpage
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
This is working for me and this document will work with pdf_document
, html_document
, and any other...
I'll try to suggest a PR for that in Rmardown but I am not sure how they feel about adding a lua script inside the package. Will see.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.