问题描述
在我的文档中,我想显示一些信息,使用```块,例如:
In my document, I want to show some info, using ``` block, such as:
```
bruin@c7 ~ $ cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core)
bruin@c7 ~ $
```
对于大的输出块,我想折叠它们.怎么做?在网上搜索似乎所有相关主题都是关于代码折叠的,但我想要折叠的不是任何类型的代码,只是简单的文本...
For large output chunks, I want to fold them. How to do that? Searching the web it seems all related topics are about code folding, but what I want to fold is not any type of code, just simple text...
谢谢!
推荐答案
这里有一个非常简单的方法可以自己实现以下内容:
Here is a very easy way to implement the following yourself:
您可以在以下可重现的 RMD 文档中找到您需要的所有代码:
You can find all the code you need in the following reproducible RMD document:
---
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 部分,我们简单地向每个标有 fold
类的块添加一个按钮.然后我们将 onClick
事件添加到所有这些按钮.单击按钮时,其文本应在 Fold 和 Unfold 之间切换.并且相应的代码容器也应该切换其可见性状态.打开文档时,每个标有 fold
的块都会折叠.如何使用 CSS 设置按钮样式取决于您.
In the JS part we simply add a button to every chunk that is marked with the class fold
. Afterwards we add the onClick
event to all those buttons. When a button is clicked, its text should toggle between Fold and Unfold. And the corresponding code container should toggle its visibility state as well. Every chunk marked with fold
is folded when the document is opened.How you style the button using CSS is up to you.
这篇关于如何在 R bookdown 中逐字折叠文本(不是代码)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!