4

I use R bookdown package to create a gitbook. Recently I noticed an ugly dark stripe under the boxes of code chunks in rendered gitbook (see a printscreen below). It appeared with highlight set to 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:

bookdown::gitbook:
  highlight: espresso
  1. Can anyone else reproduce the problem?
  2. How can I rid off the stripe, but still use highlight: espresso?

enter image description here

GegznaV
  • 4,938
  • 4
  • 23
  • 43
  • what is your version of `pandoc`? – RLesur Feb 03 '18 at 23:21
  • Pandoc version 2.1.1 – GegznaV Feb 04 '18 at 15:03
  • thx. Problem comes from Pandoc >=2. I will enhance my answer soon. – RLesur Feb 04 '18 at 16:26
  • 1
    I added some details to the answer – RLesur Feb 05 '18 at 03:25
  • @Yihui Xie Thanks a lot! You are extremely generous! (we already know that). I'm embarrassed because you give me so much points. – RLesur Feb 05 '18 at 21:10
  • @romles No need to feel embarrassed. You have done excellent work. This is just a way to show my appreciation -- you are essentially setting me free from knitr and rmarkdown questions, which is very, very helpful to me. I really need time to focus on other things. If I could, I'd love to give you all my 20k points :) – Yihui Xie Feb 06 '18 at 04:16
  • @romles you did a really great job. – GegznaV Feb 06 '18 at 19:17
  • 1
    @Vilmantas Thanks! I'm interested in better understanding pandoc css because I began to write a package to render Rmd to pdf with CSS https://github.com/RLesur/weasydoc – RLesur Feb 06 '18 at 20:00

1 Answers1

5

The problem comes from the use of pandoc 2.x.
I reproduced the bug here and you can see the same gitbook with pandoc 1.19.x here.
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).

<div class="sourceCode">
  <pre class="sourceCode r">
    <code class="sourceCode r">
     <span class="kw">install.packages</span>(<span class="st">&quot;bookdown&quot;</span>)
     <span class="co"># or the development version</span>
     <span class="co"># devtools::install_github(&quot;rstudio/bookdown&quot;)</span>
    </code>
  </pre>
</div>

The problem you got is a consequence of a collision between gitbook theme and espresso highlighting.

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).

The builtin espresso highlighting looks like this for pandoc v2.x (it is black, like an 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">&quot;bookdown&quot;</span>)
     <span class="co"># or the development version</span>
     <span class="co"># devtools::install_github(&quot;rstudio/bookdown&quot;)</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).

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:

.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:

  • in pandoc 1.19.x, the enclosing div has no background-color rule (the background color is applied to pre element). So, there's no black stripe.

  • in pandoc 2.x, the background color is set at the div level. There's a black stripe.


From your question, I understand that you want the espresso highlighting without its "ugly" dark companion. In other words, you want a "white coffee" highlighting.

So, there are two options:

  • get rid-off the bottom margin
  • overrule the background color of espresso highlighting

It is a matter of taste.

First solution : get rid-off the bottom margin
Save these lines in a file with .css extension (e.g. 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::gitbook:
  highlight: espresso
  css: fix.css

Second solution: overruling the espresso background color with Hex Gray97
In this case, you can include these lines in 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.
This gist defines a whitecoffee theme (it is the espresso theme with a Hex Gray97 background).
Save the whitecoffee.theme file at the root level of your project and modify the _output.yml file:

bookdown::gitbook:
  highlight: whitecoffee.theme
RLesur
  • 5,810
  • 1
  • 18
  • 53
  • 1
    I appreciate that you gave me two options to fix the issue. The first one is more appropriate for me as I prefer using `sepia` theme. It is this stripe is a result of recent updates in `gitbook` (as several months ago it was not present using the same code of `bookdown`)? – GegznaV Feb 03 '18 at 09:04
  • I really don't know when and how this bug appeared. CSS is really hard to "debug" and this stripe could be a side-effect of another evolution of the CSS , JS or `pandoc`. Finding when and how this bug appeared requires a lot of hours. – RLesur Feb 03 '18 at 10:55
  • I've a fun idea to test when this bug appeared. Not yet the time to do it, but I plan to do this – RLesur Feb 03 '18 at 12:41
  • 1
    I also tried to inherit color and it worked: `div.sourceCode { background-color: inherit !important; }` This enables to use gitbook themes including white, sepia, and night – GegznaV Mar 24 '18 at 20:16