1

I want to justify text in an Rmarkdown html file on both sides. I know how to do this using <style> body {text-align: justify} </style> after the YAML header (as per this answer):

---
output: html_document
---

<style> body {text-align: justify} </style> <!-- Justify text. -->

# Text that is justified on both sides

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. 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. 

However, I want to find a way to specify this in the YAML header directly, instead of after the YAML header, but without having to resort to additional .css files in the same repository. In other words, I don't want to put a tiny bit of html code in a separate .css file and call it via

output:
  html_document:
    css: justify.css

and I don't want it in the main body of the Rmarkdown file. How can I do this?

captain
  • 543
  • 1
  • 3
  • 20

1 Answers1

0

Using plain pandoc, one would use the include-headers field. Citing from pandoc's manual:

Raw content to include in the document's header may be specified using header-includes; however, it is important to mark up this content as raw code for a particular output format, using the raw_attribute extension), or it will be interpreted as markdown. For example:

header-includes:
  - |
    ```{=latex}
    \let\oldsection\section
    \renewcommand{\section}[1]{\clearpage\oldsection{#1}}
    ```

My interpretation of this RMarkdown issue is that this won't work in RMarkdown. Only files can be included. However, one of the linked issues therein offers a workaround, in which the file is generated through an R snippet in the YAML header:

---
output:
  html_document:
    includes:
      in_header: header.html
dummy: "`<style>body {text-align: justify; color: green}</style>`{cat, engine.opts=list(file='header.html')}"
---

Not exactly pretty, but works as desired.

tarleb
  • 19,863
  • 4
  • 51
  • 80
  • Thanks for your answer. Unfortunately, this doesn't work for me. I get this error ```pandoc: header.html: openBinaryFile: does not exist (No such file or directory) Error: pandoc document conversion failed with error 1```. Also, it seems that your solution creates the file header.html in the same repository, which I don't want, because I don't want any additional files in my repo. – captain Nov 14 '19 at 11:01