[英]Ugly dark stripe in code chunk of gitbook with highlight: espresso (R bookdown)
I use R bookdown
package to create a gitbook. 我使用R
bookdown
包来创建一个gitbook。 Recently I noticed an ugly dark stripe under the boxes of code chunks in rendered gitbook (see a printscreen below). 最近我发现在渲染的gitbook中的代码块下面有一条丑陋的暗条纹(见下面的版画屏幕)。 It appeared with
highlight
set to espresso
. 它
highlight
espresso
。 Stripes like this one distract attention when one tries to read the book and looks unattractive. 像这样的条纹在试图阅读这本书并且看起来没有吸引力时会分散注意力。
I can reproduce the result by creating a new bookdown project with RStudio (most probably the same project as the minimal bookdown example ) with the contents of file _output.yml
replaced with two lines: 我可以通过使用RStudio创建一个新的bookdown项目来重现结果(很可能是与最小的bookdown示例相同的项目),文件
_output.yml
的内容被替换为两行:
bookdown::gitbook:
highlight: espresso
highlight: espresso
? highlight: espresso
? The problem comes from the use of pandoc 2.x
. 问题来自于
pandoc 2.x
的使用。
I reproduced the bug here and you can see the same gitbook
with pandoc 1.19.x
here . 我转载的bug 在这里 ,你可以看到同样的
gitbook
与pandoc 1.19.x
这里 。
I wrote a note to explain my debugging worflow . 我写了一个说明来解释我的调试工作流程 。
In both versions, HTML
sources are very close (I voluntary omit div
and lines ids that @Yihui Xie dislikes ). 在这两个版本中,
HTML
源代码非常接近(我自愿省略@Yihui Xie不喜欢的 div
和行ID)。
<div class="sourceCode"> <pre class="sourceCode r"> <code class="sourceCode r"> <span class="kw">install.packages</span>(<span class="st">"bookdown"</span>) <span class="co"># or the development version</span> <span class="co"># devtools::install_github("rstudio/bookdown")</span> </code> </pre> </div>
The problem you got is a consequence of a collision between gitbook
theme and espresso
highlighting . 你遇到的问题是
gitbook
主题和espresso
突出显示之间发生冲突的结果。
Firstly, the dark stripe is the "real" background color of espresso
(see here ): the background-color
of espresso
highlighting is set to #2a211c
by pandoc
(it is hard-coded here ). 首先,暗条纹是“真正的”背景色
espresso
(见这里 ):将background-color
的espresso
高亮设置为#2a211c
由pandoc
(它是硬编码在这里 )。
The builtin espresso
highlighting looks like this for pandoc v2.x
(it is black, like an espresso): 对于
pandoc v2.x
,它内置的espresso
突出显示如此(它是黑色的,像浓咖啡):
code span.kw { color: #43a8ed; font-weight: bold; } code span.st { color: #049b0a; } code span.co { color: #0066ff; font-weight: bold; font-style: italic; } div.sourceCode { color: #bdae9d; background-color: #2a211c; } div.sourceCode { overflow: auto; } div.sourceCode { margin: 1em 0; } pre.sourceCode { margin: 0; }
<div class="sourceCode"> <pre class="sourceCode r"> <code class="sourceCode r"> <span class="kw">install.packages</span>(<span class="st">"bookdown"</span>) <span class="co"># or the development version</span> <span class="co"># devtools::install_github("rstudio/bookdown")</span> </code> </pre> </div>
It is slightly different of pandoc v1.19.x
where the background-color
is set for pre
elements instead of div.sourceCode
(it's important). 它与
pandoc v1.19.x
略有不同,其中background-color
设置为pre
元素而不是div.sourceCode
(这很重要)。
Secondly, gitbook
theme overrules only for pre
elements the dark background color of espresso
with a light grey background ( Hex Gray97
) and defines a bottom margin (contrary to pandoc
CSS) see this file, line #9, column #31963 : 其次,
gitbook
主题仅针对pre
元素 gitbook
espresso
的深色背景颜色和浅灰色背景( Hex Gray97
)并定义底部边距(与pandoc
CSS相反), 请参阅此文件,第9行,第31963行 :
.book .book-body .page-wrapper .page-inner section.normal pre {
overflow: auto;
word-wrap: normal;
margin: 0 0 1.275em;
padding: .85em 1em;
background: #f7f7f7;
}
This bottom margin reveals the background color of the enclosing div
element: 这个底部边距显示了封闭
div
元素的背景颜色:
in pandoc 1.19.x
, the enclosing div
has no background-color
rule (the background color is applied to pre
element). 在
pandoc 1.19.x
,封闭div
没有background-color
规则(背景颜色应用于pre
元素)。 So, there's no black stripe. 所以,没有黑条纹。
in pandoc 2.x
, the background color is set at the div
level. 在
pandoc 2.x
,背景颜色设置在div
级别。 There's a black stripe. 有一条黑色条纹。
From your question, I understand that you want the espresso
highlighting without its "ugly" dark companion. 从你的问题来看,我知道你想要
espresso
突出而没有它“丑陋”的黑暗伴侣。 In other words, you want a "white coffee" highlighting. 换句话说,你想要一个“白咖啡”突出显示。
So, there are two options : 所以, 有两种选择 :
espresso
highlighting espresso
突出的背景颜色 It is a matter of taste. 这是一个品味问题。
First solution : get rid-off the bottom margin 第一个解决方案 :摆脱底部边缘
Save these lines in a file with .css
extension (eg fix.css
): 将这些行保存在扩展名为
.css
的文件中(例如fix.css
):
.book .book-body .page-wrapper .page-inner section.normal pre {
margin-bottom: 0!important;
}
Include this CSS file in your bookdown modifying _output.yml
: 在您的bookdown中包含此CSS文件,修改
_output.yml
:
bookdown::gitbook:
highlight: espresso
css: fix.css
Second solution : overruling the espresso
background color with Hex Gray97
第二种解决方案 :使用
Hex Gray97
espresso
背景颜色
In this case, you can include these lines in fix.css
: 在这种情况下,您可以在
fix.css
包含以下行:
div.sourceCode {
background-color: #f7f7f7!important;
}
Since the use of the important
rule is not recommended, you can obtain the same result in a more elegant fashion: with pandoc 2.x
, you can customise the highlighting theme . 由于不建议使用
important
规则,因此您可以以更优雅的方式获得相同的结果: 使用pandoc 2.x
,您可以自定义突出显示主题 。
This gist defines a whitecoffee
theme (it is the espresso
theme with a Hex Gray97
background). 这个要点定义了一个
whitecoffee
主题(它是带有Hex Gray97
背景的espresso
主题)。
Save the whitecoffee.theme
file at the root level of your project and modify the _output.yml
file: 将
whitecoffee.theme
文件保存在项目的根级别并修改_output.yml
文件:
bookdown::gitbook:
highlight: whitecoffee.theme
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.