Search code examples
rlatexbookdown

How to use custom blocks with bookdown in PDF


I am trying to create custom background colors for blocks of text in a book chapter. If I add this code:

```{block, type="FOO"}
blah
```

to my Rmd file and this:

div.FOO {
   font-weight: bold;
   background-color: LightGray;
}

to my style.css file the html page looks great but the PDF output no longer includes the chapter and the table of contents is missing.

Can someone please tell me what to tweak to get a PDF to use a custom block? Unfortunately I don't speak TeX so clues for the clueless would be greatly appreciated.


Solution

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