How to fold verbatim text (not code) in R bookdown?

后端 未结 1 1139
栀梦
栀梦 2021-01-06 18:35

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 ~          


        
相关标签:
1条回答
  • 2021-01-06 19:08

    Here is a very easy way to implement the following yourself:

    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 ~ $
    ```
    

    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.

    0 讨论(0)
提交回复
热议问题