Rmarkdown render () + knitr spin (): how to mix code blocks and nested elements

I generate html and pdf notebooks from R scripts using the rmarkdown function render() and the knitr function spin() . Sometimes I use nested lists and mix them with blocks of code. The following is an example of using the rmarkdown and knitr .

 #' (1) This is normal text. #' (a) This is normal text but indented. #+ echo = TRUE, eval = TRUE print("This is code") #' (b) This is supposed to be normal text with the same #' indentation as (a). However, it will be formatted as code. #' By this I mean that eg in a pdf-notebook it will be #' correctly indented but the font will be the same font as #' the code. 

However, everything that follows the code after list item (a) will also be marked as code (for example, (b)). But I want to ensure that (b) is marked as plain text and uses the same indentation as (a). Can this be done?

+6
source share
2 answers

There is an indent inner block option that can add padding to the block output. In your case, you can specify four spaces, for example.

 #+ echo = TRUE, eval = TRUE, indent = ' ' 
+3
source

You should use what is called The four-space rule in the documentation: http://rmarkdown.rstudio.com/authoring_pandoc_markdown.html#the-four-space-rule

So the following code works

  (1) This is normal text. Continued. (a) This is normal text but indented. ```{r, echo = TRUE, eval = TRUE} summary(cars) ``` (a) This is normal text with the same indentation as (a). 

Note: There is

  • 2 Spaces infront (1)
  • 4 spaces infront of each (a)
  • 8 Spaces in front of a code block

As a result: enter image description here

I ran it using rmarkdown::render("test.Rmd") and this is my session information

 R version 3.1.1 (2014-07-10) Platform: x86_64-w64-mingw32/x64 (64-bit) locale: [1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 LC_MONETARY=German_Germany.1252 [4] LC_NUMERIC=C LC_TIME=German_Germany.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] digest_0.6.8 evaluate_0.5.5 formatR_1.0 htmltools_0.2.6 knitr_1.9 rmarkdown_0.5.1 [7] stringr_0.6.2 tools_3.1.1 XML_3.98-1.1 yaml_2.1.13 
+3
source

Source: https://habr.com/ru/post/985414/


All Articles