簡體   English   中英

R markdown:如何用內部css改變風格?

[英]R markdown: how to change style with internal css?

我知道如何使用自定義css文件更改R markdown樣式。 但是,當更改很小時,我更喜歡內部或內聯css,以節省管理兩個文件的麻煩。 我用谷歌搜索,並沒有找到解決方案。 下面是使用外部css文件更改樣式的簡單示例。 有沒有辦法用內部或內聯css做到這一點?

R降價文件:

---
title: "test"
output: 
    html_document:
        css: test.css
---

## Header 1 {#header1}
But how to change style with internal css?

test.css文件:

#header1 {
color: red;
}

Markdown接受原始HTML並將其傳遞給未更改的內容,因此將“樣式化”元素定義為HTML:

<h2 style="color: red;">Header 1</h2>

當然,有些工具實際上不允許原始HTML傳遞(出於安全原因或最終輸出不是HTML),因此您的里程可能會有所不同。

根據您使用的Markdown實現,您可以在屬性列表中定義樣式(如果它支持任意鍵):

## Header 1 {style="color: red;"}

但是,這是最不可能工作的。

請記住,HTML <style>標簽不需要在文檔<head>中工作。 如果您可以使用原始HTML,則可以在文檔正文中包含<style>元素(如注釋中@ user5219763所指出的):

---
title: "test"
output: 
    html_document
---

<style>
    #header1 {
        color: red;
    }
</style>

## Header 1 {#header1}
But how to change style with internal css?

另一種hacky選項是在腳本中指定一個css文件,然后在第一個塊中創建它。

例如.Rmd文件的前18行:

  ---
  title: "Something Important"
  output: 
    html_document:
      css: mystyle.css
  ---


  ```{r b, echo=F}
  writeLines("td, th { padding : 6px } 
             th { background-color : coral ; 
                  color : white; 
                  border : 1px solid white; } 
             td { color : black ; 
                  border : 1px solid skyblue }
             h1, h2, h3, h4, h5, p { font-family: consolas; ",
             con = "mystyle.css")
  ```

在上面,我首先在markdown的標題塊中引用文件mystyle.css 然后,我使用writeLines()創建文件,並將其保存到con = ...指定的文件中。

就個人而言,我認為最好的選擇是在一些<script></script>標簽之間拋出代碼,如果它是一個一次性的R腳本。

但是,如果您確實要創建外部文件,但又不想編輯單獨的文件,則上述方法提供了一種解決方法。 這感覺很奇怪。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM