bookdown custom blocks don't work with pdf output

I'm currently working on a bookdown project that makes use of custom blocks. In the .Rmd file, they look something like this:

::: {.rmd-details}
text goes here!
:::

My style.css file has a bit that accompanies this:

.rmd-details {
  padding: 1em 1em 1em 4em;
  margin-bottom: 10px;
  background: /* #99FF99 */ url("pics/idea.png") left center/3em no-repeat !important;
  border-style:solid;
  border-color:#00FF00;
}

and everything works great as long as I build it into an html object. However, when I build it to a pdf book (i.e. bookdown::render_book(output_format = "bookdown::pdf_book")), then the text inside the custom block appears regularly, as if it hadn't been enclosed in a custom block.

To make matters worse, I read "they [custom blocks] mainly work for HTML output and do not work for LaTeX output." from [here](9.6 Custom blocks (*) | R Markdown Cookbook.

How can I get around this?

You can do something like this in your latex preamble:

% poetry styling
\newenvironment{poetry}[0]{\par\leftskip=2em\rightskip=2em}{\par\medskip}

That creates the style for the PDF for a block given a "poetry" style.

Hope that helps.

Thanks for the very fast response. It seems like I'm definitely posting this question in the right place.

I'm not sure about the connection between this latex code, the css, and the rmarkdown code? Where did "poetry" come from? Will I have to change anything besides preamble.latex? I'm not sure how ::: {.rmd-details} will trigger something like \begin{poetry} in a generated .tex file.

It's supposed to look something like it does when I render it to a non-pdf output I have these neon popup boxes.

Sorry. That was just meant to be an example. That is directly out of my preamble file and not meant to be exactly what you want.

This will get you closer, but I have no idea what styling you want in the PDF.

% details block styling
\newenvironment{rmd-details}[0]{\par\leftskip=2em\rightskip=2em}{\par\medskip}

This will set off your text block with 2em indents left and right. Nothing fancy. If you need help with the latex styling, I suggest you ask on stack overflow. I had to get help just to get this simple style working the way I wanted.

Oh okay, thanks. That makes sense.

Throwing that into preamble.tex doesn't change the output pdf, though. Which part of the rmarkdown code uses that? I imagine rmarkdown copy/pasting \begin{rmd-details} in places, but I'm not sure which pieces I need to check.

First, do you have the following in your _output.yml file?

bookdown::pdf_book:
  includes:
    in_header: preamble.tex

If not, you need it, otherwise the preamble.tex file will be ignored.

Second, I don't know latex at all, so I can't remember whether this matters, but in my preamble.tex I have that code just before this line:

\makeatother

Now, to the actual answer to your question. I thought you could just do it the way you already had. I'm 99% sure that used to work, but I just checked my book, and it wasn't working in my PDF either! So I managed to track down the problem. You just need to slightly modify your RMD to be like this instead:

::: {.rmd-details data-latex=""}
text goes here!
:::

Let me know if that fixes it.

1 Like

Custom blocks as described in 9.6 Custom blocks (*) | R Markdown Cookbook is something that should work for all HTML and PDF Rmd format. For PDF, you need to use data-latex or latex attributes - either 1 or true or to a character value that will be passed as an option to the latex environment. See example in the cookbook.
This is like this because otherwise you would get an error in LaTeX if you use custom environment without defining the correct preamble definition. So this is ignored by default unless data-latex is set.

bookdown is a bit different because it supports some special environments: Theorems and Proofs (2.2 Markdown extensions by bookdown | bookdown: Authoring Books and Technical Documents with R Markdown)

For those environments only, the data-latex is not needed because bookdown will process them anyway, because bookdown will add the correct preamble content for you for those environments.

If you don't want this processing and want do it all yourself, using the dev version of bookdown you can now deactivate this processing by using options(bookdown.theorem.enabled = FALSE) before rendering (or set in your session).

Please report any issue if that does not work as expected

1 Like

This topic was automatically closed 7 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.