在 R bookdown 如何设置默认代码输出背景颜色 [英] In R bookdown how do I set the default code output background color
问题描述
我的带有 gitbook 样式的 R bookdown 书在灰色框中呈现代码输出.我想将其更改为带有黑色边框的白框.我找到了一些帖子,展示了如何为特定块设置颜色,但没有找到如何设置默认值.我想我需要添加一个这样的 css 文件:
My R bookdown book with gitbook style renders with the code output in a gray box. I would like to change it to be a white box with a black border. I have found posts showing how to set the color for a specific block but not how to set the default. I think I need to add a css file like this:
---
title: x
author: clueless
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
documentclass: book
output:
bookdown::gitbook:
css: "style.css"
---
但我不知道从哪里开始.我知道的CSS很少.因此,对于无知者的线索将不胜感激.
but I a don't know where to go from here. I know very little css. So clues for the clueless would be greatly appreciated.
推荐答案
只使用 CSS
上一个答案对于 CSS 规则是正确的,但选择器必须更具体地用于 gitbook()
格式.这是CSS 特异性的问题.
使用浏览器检查器,您可以了解需要用于覆盖 gitbook
设置的默认 CSS 样式的选择器.
Using a browser inspector, you can get a sense of the selector you need to use to override default CSS style set by gitbook
.
这个简单的 css 将替换背景并添加边框
This simple css will replace background and add a border
.book .book-body .page-wrapper .page-inner section.normal pre {
background: #fff;
border: 1px solid #ddd;
border-color: #000;
}
您像在 style.css
中一样放入并在 index.html 中使用
或 bookdown::gitbook
的 css
参数.Rmd_output.yml
取决于您使用的.
You put in as you do in style.css
and use the css
argument of bookdown::gitbook
in index.Rmd
or in _output.yml
depending of which you are using.
这将获取 CSS.
我认为你也可以使用 !important
来对 pre
和
I think you could also use !important
to have the most specify on pre
with
pre {
background: #fff !important;
border: 1px solid #ddd !important;
border-color: #000 !important;
}
(在上面评论后编辑)
另一种方法是使用 class.source
和 class.output
https://bookdown.org/yihui/rmarkdown-cookbook/chunk-styling.html
Another way would be to use class.source
and class.output
https://bookdown.org/yihui/rmarkdown-cookbook/chunk-styling.html
这将允许您为您的输出设置一个特定的类,并在您的 CSS 中定位这个类.
This would allow you to set a specific class for your output and target this class in your CSS.
它需要使用块选项将块选项应用于所有代码块
it would require to apply the chunk option to all code chunk using
knitr::opts_chunk$set(class.output = "custom-output")
然后您可以使用 pre.custom-output
仅针对代码块输出部分,使用上述两种语法之一(完整选择器或 !important)
Then you could use pre.custom-output
to target only the code chunk output part, using one of the two syntax above (full selectors or !important)
这篇关于在 R bookdown 如何设置默认代码输出背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!