tchevrier
tchevrier

Reputation: 1171

Bookdown (proof) custom environment: how to nest code chunks or inline R inside custom (e.g. proof) environments and get proper parsing?

I guess my question is a potential if not probable duplicate of How to use inline R code in a bookdown theorem or example environment. It's been nearly 3 years since this question was asked, so a refresh might be welcome in any case, all the more given @YiHui's comment: "That is not possible with bookdown (at least for now)"?

Background: I am trying to use @YiHui bookdown package to produce a book with lecture slides (see creating accompanying slides for bookdown project). A key feature for this purpose is to be able to use conditional formatting, such as eval = (out_type=="beamer"). The issue I am facing is therefore to be able to add code chunks or inline R code inside bookdown custom environments (see https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#theorems and https://bookdown.org/yihui/bookdown/custom-blocks.html). I would love to get the following to compile properly. Is there a solution? Would a hook work (see https://yihui.org/knitr/hooks/)? There is a hack that works for forcing pandoc to parse inside a latex environment, but i do no think it applies here. Would a lua filter work (I have no idea how to use this)?

En passant, I am also surprised to see $\digamma$ showing up properly in the html output, but not in the pdf output, though it is parsed correctly in the tex file. The amssymb package is loaded and it's supposedly defined in there - what am I missing?

Much appreciated any help on this. Here is a MWE - I just edited chapter 4 from the default bookdown project. You can simply replace the contents of the file 03-method.Rmd with the following:

# Methods

We describe our methods in this chapter.

So here is a theorem to prove 1+1 = `r 1+1`.

```{theorem, echo=TRUE}  
we want to show that in R, 1+1 = 2.  
We also wonder why `$\digamma$` ($\digamma$) not showing.  
Shows in the html output only, not pdf.  
But when I recompile the tex file produced by bookdown - shows up just fine!!  
Indeed, is defined correctly from `\usepackage{amssymb}`.  
```

```{proof, echo=TRUE}  
- If I am not mistaken, I cannot get an Rmarkdown style list to work either in the proof environment.
- Well, this is where i would like to be able to use inline code.  
- we use r to compute 1+1: `r 1+1`.  
- Does not work.  
- Just shows verbatim inline code.  
```

Upvotes: 3

Views: 694

Answers (1)

Yihui Xie
Yihui Xie

Reputation: 30194

With the current development version of bookdown:

remotes::install_github('rstudio/bookdown')

you can write theorems in Pandoc's fenced Divs, e.g.,

# Methods

We describe our methods in this chapter.

So here is a theorem to prove 1+1 = `r 1+1`.

::: {.theorem}  
we want to show that in R, 1+1 = 2.  
We also wonder why `$\digamma$` ($\digamma$) not showing.  
Shows in the html output only, not pdf.  
But when I recompile the tex file produced by bookdown - shows up just fine!!  
Indeed, is defined correctly from `\usepackage{amssymb}`.  
:::

::: {.proof}  
- If I am not mistaken, I cannot get an Rmarkdown style list to work either in the proof environment.
- Well, this is where i would like to be able to use inline code.  
- we use r to compute 1+1: `r 1+1`.  
- Does not work.  
- Just shows verbatim inline code.  
:::

However, please note that this new syntax is not supported for beamer output yet.

Upvotes: 2

Related Questions