简体   繁体   中英

Justify text in Rmarkdown html output in YAML header

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM