首页 > 解决方案 > 如何在 R bookdown 中折叠逐字文本(不是代码)?

问题描述

在我的文档中,我想使用 ``` 块显示一些信息,例如:

```
bruin@c7 ~ $ cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core)
bruin@c7 ~ $
```

对于大输出块,我想折叠它们。怎么做?在网上搜索似乎所有相关主题都是关于代码折叠的,但我要折叠的不是任何类型的代码,只是简单的文本......

谢谢!

标签: r-markdownfoldingbookdownverbatim

解决方案


这是自己实现以下内容的一种非常简单的方法:

在此处输入图像描述

您可以在以下可重现的 RMD 文档中找到您需要的所有代码:

---
title: "Hide Verbatim Blocks"
author: "Martin Schmelzer"
date: "June 22, 2018"
output: html_document
---

<style>
.fold-btn { float: right; }
</style>

<script type="text/javascript">
$(document).ready(function() {
  $(".fold").prepend("<button class=\"fold-btn\">Unfold</button>");
  $(".fold").children("code").toggle();
  $(".fold-btn").on("click", function() {
    if($(this).text() === "Fold") {
      $(this).text("Unfold");
    } else {
      $(this).text("Fold");
    }
    $(this).next("code").toggle("linear");
  })
});
</script>

# Rmd file

```{fold}
bruin@c7 ~ $ cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core)
bruin@c7 ~ $
```

在 JS 部分,我们只需为每个标记有 class 的块添加一个按钮fold。之后,我们将onClick事件添加到所有这些按钮。单击按钮时,其文本应在FoldUnfold之间切换。并且相应的代码容器也应该切换其可见性状态。fold打开文档时,每个带有 标记的块都会折叠。如何使用 CSS 设置按钮样式取决于您。


推荐阅读