# How to indent a code chunk without adding a list

#1

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

0 Likes

#2

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
---

some text

will include some code now

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


text after code

again some text


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

0 Likes

#4

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

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

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


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



The output I got was:

0 Likes

#5

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?

0 Likes

#6

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


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


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


0 Likes

#7

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

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

* unordered list
{r, indent = "     "}
1+1


* 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.

0 Likes

#8

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.

0 Likes

#9

Hi lobshi,

Did you find any solution?

I was trying to find online resources. I came across these two posts on SO:

Both of these categorically state that it can't be done in raw markdown directly, but CSS can be used.

I don't know CSS at all, so basically I imitated blindly and created the following no_bullet_mark.css file:

ul {
list-style-type: none;
margin-left: 40px;
}


Then, using that in .Rmd seems to do what you want.

---
title: "Example"
output:
html_document:
css: no_bullet_mark.css
---

* unordered list
{r, indent = "     "}
1+1


1. ordered list
{r, indent = "     "}
1+1



The screenshot of the generated .html file is given below:

I'm not really satisfied with it, and I expect you won't be either. Not only it fails to hide the numbered bullets, but also you can't use unordered lists later in your document (you may use, but won't get the bullets then, and the points will be indented, which may not be desired).

Hopefully, you can modify the CSS file and get a better solution. If you get one, please share it with all of us.

Best of luck!

0 Likes

#10

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:
keep_tex: true # to keep the .tex file used to create the PDF
---

\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{Highlighting}[]
\DecValTok{2}\OperatorTok{+}\DecValTok{2}
\end{Highlighting}

\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

#11

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?

0 Likes

#12

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

#13

I got that working. The markdown couldn't detect the list. So, I added a blank line before it and got the result.

Modified Rmd file
---
title: "Example"
output:
keep_tex: true # to keep the .tex file used to create the PDF
---

\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



Thanks for the solution for html_document.

0 Likes

closed #14

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.

0 Likes