如何避免项目符号列表中的代码块增加垂直行距?

时间:2021-08-01 18:03:56

标签: r-markdown markdown pandoc

使用设置

output:
  github_document:
    toc: true
    number_sections: true

```{r, include=FALSE}
knitr::opts_chunk$set(collapse = TRUE)
```

比较

# Test
* foo
* bar
* boo

带输出

enter image description here

# Test
* foo
  ```{r, echo = TRUE}
  print("eg")
  ```
* bar
* boo

带输出

enter image description here

我们可以看到垂直间距已经改变,而没有任何命令这样做。如何防止这种情况发生?

1 个答案:

答案 0 :(得分:2)

考虑这个快速解决方法:

---
output:
  github_document:
    toc: true
    number_sections: true
---

```{css, echo = FALSE}
li>p {
    margin-top: 0;     # default value: 16px
  }
p, blockquote, ul, ol, dl, table, pre {
    margin-bottom: 0;  # default value: 16px
  }
```

```{r, include = FALSE}
knitr::opts_chunk$set(collapse = TRUE)
```

输出:
enter image description here

默认情况下,R Markdown 中的代码块包含在 HTML 输出的标记 <div class="..." /> 中。当 <div> 元素嵌入到列表中时,项目内容 (foo) 将被包裹在 <p> 标签中。因此,我们需要将其 CSS 属性:margin-topmargin-bottom 设置为 0

<ul>
    <li>
        <p>foo</p>
        <div class="sourceCode" id="...">...</div>
    </li>
    .
    .
</ul>