
I seem to remember that formatting of R chunks works ok, but I also seem to recall that sometimes manual intervention is required.

Some other quirks I noticed is that you seem to need empty lines before and after your custom style block. One thing you see in this example is that your own styles overwrite all others, so headers inside your custom style will just be formatted like your custom style. The resulting word document looks like this:

Note that the header formatting is overwritten. This format includes a border and it also works with an equation. This is formatted according to the _mystyle1_ format. This text is not getting a special format. This example markdown text shows how to use the styles. Here I created 3 styles called mystyle1/mystyle2/mystyle3 in the Word doc, and assign them to specific parts of the text. You can now assign text blocks in your R markdown file specific styles. The important part is the last line, which specifies the word document with the custom styles you created in the previous step. Note that I’m using bookdown as the output format here, but any others that can produce word output, e.g. the standard R Markdown format, should work equally well. Title: "An example of formatting text blocks in Word" Your YAML header should look something like this:. Repeat to create as many custom styles as you want, save the word document into the folder of your RMarkdown file.Depending on your version of Word, this might be somewhere else. Mark the text you wrote, click on the arrow to the left of the Styles box (see the red “circle” in the figure) and choose Create a style.For instance you can write Text for mystyle1. It doesn’t matter what you write, it’s just meant so you can create and apply new styles to it. Create a new word document (either through RMarkdown -> Word, or just open Word and create a new empty one).I tried to dig out the 2 year old project (it turns out I ended up not needing it for that project and haven’t used it since).Īs I was trying to contemplate how to best share the example with Dean, I figured I’ll write a brief blog post, which might benefit others, too. Then recently, Dean Attali (yes, the guy who does a lot of cool Shiny stuff ✨) posted a reply asking for an example. I asked online and got some help from JJ Allaire (yes, the guy who started RStudio 🤩). Specifically, I wanted to find a way to format certain parts of the R Markdown document in a specific style. I found some good information in this RStudio article, but it didn’t quite address everything I was looking for. A good while back (around 2 years as of this writing), I needed the feature to turn an R Markdown document into a Word document (that’s easy) and to apply custom styles to specific parts of the Word document (that was trickier).
