首頁 > web前端 > css教學 > 主體

如何將 CSS 類別應用於 RMarkdown 中的程式碼區塊?

Linda Hamilton
發布: 2024-10-24 18:52:30
原創
379 人瀏覽過

How Can I Apply CSS Classes to Code Chunks in RMarkdown?

將CSS 類別應用於RMarkdown 中的程式碼區塊

如果您希望將CSS 類別專門加入RMarkdown 中的程式碼區塊,有

class.source 和class.output 選項

對於knit 版本1.16 及更高版本,您可以使用class.source 和class.output 選項將HTML 類別指派給原始碼區塊和輸出程式碼區塊。例如:

summary(cars)

這會將 'myClass' 類別加入到原始程式碼區塊中。

使用 fenced_code_attributes 和 knit Hooks

在knitr 1.16之前,您可以將fenced_code_attributes Pandoc 擴展與knitr中的輸出掛鉤結合使用。考慮以下:

---
title: "Untitled"
  output: 
    html_document:
      md_extensions: +fenced_code_attributes
---
登入後複製

knitr::knit_hooks$set(source = function(x, options) {
return(paste0(

"```{.r",
ifelse(is.null(options$class),
  "", 
  paste0(" .", gsub(" ", " .", options$class))
),
"}\n",
x,
"\n```"
登入後複製

)))
})

Then, within the code chunk, you can specify the class as follows:
登入後複製
summary(cars)
登入後複製
This will render the code chunk with the HTML class:
登入後複製

</p><pre class="brush:php;toolbar:false"><code>
    summary(cars)
<</code>/code>
登入後複製

以上是如何將 CSS 類別應用於 RMarkdown 中的程式碼區塊?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!