Search code examples
bookdown

How can I redefine a standard bookdown "theorem environment" in LaTeX/PDF?


I am using bookdown, and would like the standard "example" and "definition" environments (for example) in the PDF version to be more clearly distinguished by, for example, adding some slight background shading.

How do I do so? I cannot even find where these environments are defined (e.g. there is no definition in preamble.tex).

Yihui's book says

"If you are a LaTeX or HTML expert, you may want to customize the style of these environments anyway (see Chapter 4). "

This implies it is possible. I have read that section, but I am still struggling (hence the post): clearly I am not an expert!

For example, just to test if what I am wanting to do is possible, I tried something really simple to begin with; I added this to preamble.tex:

\makeatletter
\renewenvironment{example}
               {\begin{quote}}
               {\end{quote}}
\makeatother

The LaTeX compilation reports:

! LaTeX Error: Environment example undefined.

So perhaps it isn't a standard environment at all. Indeed, the book implies perhaps it is a theorem. But I don't know how to redefine a theorem...

So is there a way to redefine these standard blocks for the PDF output (and if so, how)?

P.


Solution

  • The standard blocks are environments, but they are defined after your custom preamble. Using \AtBeginDocument you can move your own changes past that definition:

    ---
    output: 
      bookdown::pdf_document2:
          keep_tex: yes
    header-includes:
    - \AtBeginDocument{\renewenvironment{example}{\begin{quote}}{\end{quote}}}
    ---
    
    # Some section
    
    ```{example}
    Here is my example.
    ```
    
    ```{definition}
    Here is my definition.
    ```
    

    Notes:

    • I am using header-includes instead of preamble.tex, but that should not matter.
    • keep_tex: yes is very helpful for analyzing things like this.
    • These environments are created using the LaTeX package amsthm. It might offer more convenient hooks for customization.