How to indent a code chunk without adding a list

Hi,
Does anyone know how to indent a code chunk without adding a (ordered) list in R markdown? In my homework, if I remove "a. Refer to", the code chunk will become plain text.


Thanks

I don't really understand what do you mean by this.

If I use the following, where I've used no lists, I obtain reprex.pdf (119.5 KB). As you can see, the code chunk is identified correctly.

---
title: "Example"
output: pdf_document
---

# first heading
some text

# second heading
will include some code now

```{r }
y <- rnorm(n = 100)
plot(x = y)
```

text after code

# third heading
again some text

Will you please share a small Rmarkdown code to illustrate your problem in form of a reprex?

Thank you so much for replying! Here is my example code:

---
title: "Example"
output: pdf_document
---

#### 1. header 1
  a. ordered list
```{r, indent = "     "}
1+1
```

#### 2. header 2
  \hspace{0.8cm}no ordered list: code chunk doesn't work right now
```{r, indent = "     "}
1+1
```

The output I got was:

Now I understand what you mean. The code chunks are actually detected correctly in both case, but in second case they are not displayed as usual (and as expected), and also they are not indented.

I can't provide you a solution, as I didn't know of indent as a chunk option before your post.

But after going through the documentation, there's a possibility that it's behaving as it should. I'm not sure though.

There is a hidden option indent which stores the possible leading white spaces of the chunk, e.g. for the chunk below, indent is a character string of two spaces:

  ```{r}
  rnorm(10)
  ```

Currently this option is only used to indent markdown output, because leading white spaces have special meanings in markdown.

May be someone else on this community will be able to solve your problem. But I hope that you figure out something, you'll share it with us.

P.S. I don't understand why : code chunk doesn't work right now is not present in your rendered output. Did you use this .Rmd code to generate this?

Thank you for taking time on it. I add code chunk doesn't work right after I had the previous output. I am very sorry about the confusion.
I switched to Rsweave to finish this homework to guarantee the tidy output. I think R sweave is a better option than R markdown if you have high standard on formatting.
For this problem, i have two "solutions" so far:

  1. Add an ordered list under header 2. But I don't need this ordered list mark. If there is a way to generate an ordered list without having the mark, it would be the solution.
#### 1. header 1
  a. ordered list
```{r, indent = "     "}
1+1
```

#### 2. header 2
  a. ordered list
```{r, indent = "     "}
1+1
```
  1. Live with the plain text output, and indent this chunk by adding 3 more tabs in the indent option.
#### 1. header 1
  a. ordered list
```{r, indent = "     "}
1+1
```

#### 2. header 2
  a. ordered list
```{r, indent = "          "}
1+1
```

I just noted that unordered lists are fine too. Check this:

---
title: "Example"
output: pdf_document
---

#### 1. header 1
  * unordered list
```{r, indent = "     "}
1+1
```

#### 2. header 2
* unordered list
```{r, indent = "     "}
1+1
```

So, if you (or someone else) can figure out how to suppress bullets, or use one of your choice (you can use blank space as a bullet figure then), your problem is solved.

In LaTeX, that's easy. You just need this:

\begin{itemize}
  \item[] something
  \item[] some other thing
\end{itemize}

I don't know it's equivalent in Rmarkdown. I'll update, if I can find it.

Yes, exactly. Latex itemize can not work with code chunk in Rmarkdown. I will share the solution once I find it.
Thank you so much for your informative help.

One hacky way to do this is using unordered lists and the keep_tex argument of pdf_document to itemize your code chunks, then adding [] to the \item portions within the LaTeX and then compiling it into a modified PDF.

First, here's my sample indent_chunk.Rmd file:

---
title: "Example"
output: 
  pdf_document: # Add this portion to your yaml header
    keep_tex: true # to keep the .tex file used to create the PDF
---

#### Header

  \hspace{0.8cm}no ordered list: code chunk doesn't work right now

# Create an unordered list with no text, indent your code chunk with 2 spaces so it becomes an item within your list
-
  ```{r}
  2+2
  ```

When you compile the above, it will create the indented code chunks attached to a
bullet point.

Indented Code Chunk with Bullets

The next step is to open the indent_chunk.tex file that should be in your directory now.

The itemize code chunk should be preceded by \item, so following @Yarnabrina's advice, just change it to \item[].

\hspace{0.8cm}no ordered list: code chunk doesn't work right now

\begin{itemize}
\item # Add a [] to this portion of the TeX and re-compile the PDF
\begin{Shaded}
\begin{Highlighting}[]
\DecValTok{2}\OperatorTok{+}\DecValTok{2}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] 4
\end{verbatim}

If you're in RStudio, you should see a Compile PDF button on the source window. Clicking that will compile the document and your code chunk should now be indented.

Indented Code Chunk with no Bullet

1 Like

Well, I like this idea much, but can't seem to reproduce it. The .pdf file that I'm getting doesn't have the "indented" code chunk. Also, the .tex file I'm getting is much more complicated than yours.

I'll try once again tomorrow and will update this post.

And, what do you suggest in case one tries html_document?


Update

I got that working by adding a blank line before the code chunk.

Modified Rmd file
---
title: "Example"
output: 
  pdf_document: # Add this portion to your yaml header
    keep_tex: true # to keep the .tex file used to create the PDF
---

#### Header

  \hspace{0.8cm}no ordered list: code chunk doesn't work right now

# Create an unordered list with no text, indent your code chunk with 2 spaces so it becomes an item within your list

-
  ```{r}
  2+2
  ```

That's weird. It might be the version of the packages or LaTeX. For an html_document, the answer is much simpler, with no need to use an unordered list.

Running the document below should get you what you'd need.

---
title: "indent_html"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

You can add a CSS chunk that adds space using the left-margin to any elements with the `indent` class.

```{css, echo = FALSE}
.indent {
 margin-left: 30px;
}
```

Then add the `indent` class to any code chunk and output using the `class.source` and `class.output` options. 

```{r cars, class.source="indent", class.output="indent"}
summary(cars)
```
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.