I am wanting to fold general text when using bookdown in R.
There are plenty of ways to fold code.
There is some advice here for folding verbatim text which isnot quite the same thing, but perhaps a good starting point.
However, I cannot even get this verbatim-text folding to work, despite following (as far as I can tell) the advice given in response to that question.
On my Mac, the verbatim-text folding works... but issues this warning:
Warning messages:
1: In get_engine(options$engine) :
Unknown language engine 'fold' (must be registered via knit_engines$set()).
whenever I add this chunk. as advised:
```{fold}
Here I am
```
On Windows, the code does not produce any folding at all; the text appears, but no folding of said text.
In any case, the text appears verbatim (not surprisingly, given the instructions were for that), but ideally I would like to be able to fold general text.
Can anyone please offer advice? That is: How does one fold general text in R bookdown?
As background: I am new to bookdown, I know R well, but I don't know anything associated with javascript.
Perhaps someone in the know would be able to amend the original code given for folding verbatim text. Since code and verbatim text folding both seem possible, I live in hope that folding of general text may be relatively easy...
Thanks.
P.
The idea is the same as in the other answer. You just have to make the paragraphs to be folded identifiable by wrapping a div
element around them. Take the following MRE:
---
title: "Hide Verbatim Blocks"
author: "Martin Schmelzer"
date: "June 22, 2018"
output:
bookdown::html_document2
---
<style>
.fold-btn {
float: right;
margin: 5px 5px 0 0;
}
.fold {
border: 1px solid black;
min-height: 40px;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
$folds = $(".fold");
$folds.wrapInner("<div class=\"fold-blck\">"); // wrap a div container around content
$folds.prepend("<button class=\"fold-btn\">Unfold</button>"); // add a button
$(".fold-blck").toggle(); // fold all blocks
$(".fold-btn").on("click", function() { // add onClick event
$(this).text($(this).text() === "Fold" ? "Unfold" : "Fold"); // if the text equals "Fold", change it to "Unfold"or else to "Fold"
$(this).next(".fold-blck").toggle("linear"); // "swing" is the default easing function. This can be further customized in its speed or the overall animation itself.
})
});
</script>
# Rmd file
<div class="fold">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis.
At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur
</div>
As you can see, we wrapped a div
container with class .fold
around the text. The comments next to the JS snippet should clarify what is happening there.
For further styling you may add and alter the CSS as you wish.