7

I'm generating html- and pdf-notebooks from R-scripts using rmarkdown's function render() and knitr's function spin(). Sometimes I use nested lists and mix them with code blocks. Here is an example using rmarkdown and knitr chunk options.

#' (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 e.g. 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 be marked up as code as well (e.g. (b)). But what I want to achieve is to have (b) marked up as normal text and use the same indentation as (a). Is it possible to do this?

lord.garbage
  • 5,884
  • 5
  • 36
  • 55

2 Answers2

4

You have to 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 are

  • 2 Spaces infront of the (1)
  • 4 Spaces infront of each (a)
  • 8 Spaces infront of the code-block

Resulting in: enter image description here

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

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    
Rentrop
  • 20,979
  • 10
  • 72
  • 100
  • Thanks. I cannot get it to work when compiling an `R` script directly with `render("test.R", output_format = "pdf_document")`. – lord.garbage Apr 18 '15 at 13:40
  • Your file should be named `*.Rmd` instead if `*.R` but other than this: Strange. For me it works perfectly: Resulting in http://www.filedropper.com/test_50 – Rentrop Apr 18 '15 at 14:04
  • `render()` can also be used to pure, plain `R`-scripts, not just `*.Rmd` files to notebooks. And by that I mean an script like [this](https://drive.google.com/open?id=0B_UAut69TSAic0ZvZC1qUlZDY28&authuser=0). It is based on `spin()` which converts compile pure `R`-scripts to `*.Rmd` files and this is what I'm talking about. An explanation can be found [here](https://github.com/yihui/knitr/blob/master/inst/examples/knitr-spin.R). @Yihui, can you maybe help? – lord.garbage Apr 18 '15 at 15:58
4

There is an internal chunk option indent that can add indentation to the chunk output. In your case, you can specify four spaces, e.g.

#+ echo = TRUE, eval = TRUE, indent = '    '
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419