Creating Custom Blocks in Rstudio's Bookdown

creating custom blocks in RStudio's bookdown

Thanks to @Frank's tip (see his solution for using local images), I was able to come up with the following.

I added this to the style.css file in the root of my book's directory based on this SO answer and this specific example:

.rmdcomment {
padding: 1em 1em 1em 4em;
margin-bottom: 10px;
background: #f5f5f5;
position:relative;
}

.rmdcomment:before {
content: "\f075";
font-family: FontAwesome;
left:10px;
position:absolute;
top:0px;
font-size: 45px;
}

I got the value f075 for the comment icon from this FontAwesome cheatsheet.

Then I downloaded the CSS toolkit from FontAwesome and copied the font-awesome.min.css file into the root of my book directory. I added the following to my output.yml file (in the template I started with, style.css, toc.css was already present):

bookdown::html_book:
css: [style.css, toc.css, font-awesome.min.css]

Finally, I inserted a code chunk into my Rmd file using the type option:

```{block, type='rmdcomment'}
Some text for this block. Some text for this block. Some text for this block. Some text for this block. Some text for this block. Some text for this block.
```

Sample Image

How to use custom blocks with bookdown in PDF

To give your some context for my answer, here is a "Learn LaTeX in 30 Minutes" talk. It's probably worth your time to take a look at it, just so that you understand more of the basic environment syntax.

To answer your question, bookdown projects have a file called preamble.tex. I believe you should add the following lines to that file:

\newenvironment{Foo}{
\begin{center}
\begin{tabular}{|p{0.9\textwidth}|}
\hline \\
}

{
\\\\\hline
\end{tabular}
\end{center}
}

I'm not sure if that answers all of your question, but that's what I have so far.

How to create custom blocks in RMarkdown?

You can find information of supported engines in the official documentation of the knitr package.

Also have a look at the code.

Now you can manipulate the code to your own needs, build the package and use it with your own needs.

Can I create custom blocks in bookdown without new paragraph p added?

I strongly recommend using fenced Div blocks instead of the block engine of knitr. You can find more information about the former here. For your question, the Div block will be:

```{css, echo=FALSE}
.QUESTION {
font-style: italic;
display: inline-block;
}
.QUESTION > :first-child:before {
font-weight: bold;
content: "Question: ";
}
```

::: {.QUESTION}
What is the point of this?

Another paragraph.
:::

Problem in custom blocks in bookdown conversion to in enumerate for latex/pdf

I don't know what's wrong with the block engine in your question, but if all you want is the ability to toggle content, you may use the asis engine and put the Div inside an asis chunk:

```{r, echo=FALSE}
instructorVersion <- TRUE
```
What is 1+1?

```{asis, echo=instructorVersion}
::: {.ANSWER data-latex=""}
1. This answer is easy
2. Yes, really, that's all that I was asking.
:::
```

What is the meaning of life?

```{asis, echo=instructorVersion}
::: {.ANSWER data-latex=""}
The Answer is only for Instructor's Eyes

1. The answer is hard
2. You won't get it right
3. But try anyway
:::
```

Please note that data-latex="" will be required in the next version of rmarkdown.



Related Topics



Leave a reply



Submit