Skip to content

Conversation

cderv
Copy link
Collaborator

@cderv cderv commented Apr 20, 2021

This PR closes #1143

After all I think it is best the bookdown does not interfere with beamer. beamer defines its own theorem env with some not supported by bookdown. So it would be lead to have a specific syntax for some env supported by bookdown and another syntax for other

::: {.lemma name="my name"}
`lemma` is known by bookdown
:::

::: {.fact data-latex='[Special]'}
fact is not known but define by beamer. It would only work in beamer.
:::

If we don't process beamer output, this means that the syntax is

::: {.lemma data-latex="[my name]"}
Using `data-latex` works also as `latex`
:::

::: {.fact data-latex='[Special]'}
Using beamer own definintion environment not in **bookdown** works with the same syntax
:::

The syntax offer by bookdown is there because we want the same type of output for these supported environment for HTML and PDF book. I am not sure that when bookdown is used with beamer, the gitbook() format is also one output. is it ?

The Lua filter is now easy to change - providing support for beamer with bookdown syntax would mean changing this only

diff --git a/inst/rmarkdown/lua/custom-environment.lua b/inst/rmarkdown/lua/custom-environment.lua
index d7d389e5..4986f9d2 100644
--- a/inst/rmarkdown/lua/custom-environment.lua
+++ b/inst/rmarkdown/lua/custom-environment.lua
@@ -133,7 +133,7 @@ Div = function (div)
     print_debug("label for reference -> ", label)

     -- TODO: should we support beamer also ?
-    if (FORMAT:match 'latex') then
+    if (FORMAT:match 'latex' or FORMAT:match 'beamer') then
         local label_part
         if label then
             label_part = string.format("\\protect\\hypertarget{%s}{}\\label{%s}", label, label)
@@ -192,7 +192,7 @@ Div = function (div)
 end

 -- only run filter for supported format
-if (FORMAT:match 'html' or FORMAT:match 'latex') then
+if (FORMAT:match 'html' or FORMAT:match 'latex' or FORMAT:match 'beamer') then
     return {{Meta = Meta}, {Div = Div}}
 else
     print_debug("Lua Filter skipped. Output format not supported:", FORMAT)

So @yihui this is as we discussed : skip bookdown Lua custom environment processing for beamer usage.
I think you know the pro & cons now.

@XiangyunHuang, the below is what is working now with this PR what are your thought ?

Examples

---
title: "beamer"
output: 
  bookdown::beamer_presentation2:
    toc: no
    latex_engine: xelatex
    citation_package: natbib
    theme: Xiaoshan # https://www.ctan.org/pkg/pgfornament-han 
    template: null
link-citations: yes
colorlinks: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

## some blocks from beamer theme [*Xiaoshan*](https://github.com/liantze/pgfornament-han/)

::: {.block latex="{Metropolis}"}
block is ok.
:::

::: {.exampleblock latex="{Metropolis}"}
exampleblock is also ok.
:::

::: {.theorem latex="[Metropolis]"}
theorem is now ok.
:::

## Some other blocks

::: {.lemma data-latex="[Metropolis]"}
Using `data-latex` works also as `latex`
:::

::: {.fact data-latex='[Special]'}
Using beamer own definintion environment not in **bookdown** works with the same syntax
:::

Also this would work using `rmarkdown::beamer_presentation` without change

Changes

  • do not add \newtheorem in preamble when output is beamer format. This fixes the use of bookdown special environment that are using block2 knitr engine.

  • Only support specific format - beamer defines its own environment so we don't let bookdown feature interfere.

  • Improve lua filter base on latex-divs.lua in rmarkdown

This fixes the use of bookdown special environment that are using block2 knitr engine.
@cderv cderv changed the title Beamer support for special environments Theorem and Proof Improve usage of fenced divs for Theorem and Proof environment Apr 21, 2021
@cderv cderv marked this pull request as ready for review April 21, 2021 10:05
@XiangyunHuang
Copy link

all works well in beamer, but not book documentclass.

Here is a minimal example.

---
title: "book"
output: 
  bookdown::pdf_book:
    toc: no
    latex_engine: xelatex
    citation_package: natbib
    template: null
link-citations: yes
colorlinks: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

## some blocks from book


::: {.theorem data-latex="[Metropolis]"}
[WARNING] data-latex attribute can't be used with one of bookdown custom environment. It has been removed.
:::


::: {.theorem name="Metropolis"}
NO WARNING. theorem is ok.
:::


## Some other blocks

::: {.lemma data-latex="[Metropolis]"}
[WARNING] data-latex attribute can't be used with one of bookdown custom environment. It has been removed.
:::

The output is ok.

截屏2021-04-21 下午7 31 58

My session info

xfun::session_info(c('bookdown', 'rmarkdown'))
R version 4.0.3 (2020-10-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 10.16, RStudio 1.3.1093

Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8

Package version:
  base64enc_0.1.3   bookdown_0.21.11  digest_0.6.27     evaluate_0.14     glue_1.4.2       
  graphics_4.0.3    grDevices_4.0.3   highr_0.9         htmltools_0.5.1.1 jsonlite_1.7.2   
  knitr_1.32        magrittr_2.0.1    markdown_1.1      methods_4.0.3     mime_0.10        
  rlang_0.4.10      rmarkdown_2.7.12  stats_4.0.3       stringi_1.5.3     stringr_1.4.0    
  tinytex_0.31.4    tools_4.0.3       utils_4.0.3       xfun_0.22         yaml_2.2.1       

Pandoc version: 2.13

@cderv
Copy link
Collaborator Author

cderv commented Apr 21, 2021

With bookdown::pdf_book, the syntax is the current working one

::: {.env #id name="NAME"}
:::

with

  • env one of bookdown supported environment, ``
  • #id the identifier to use to reference theorem-like block
  • name= the name to append in parenthesis after the env name

So you example should be

---
title: "book"
output: 
  bookdown::pdf_book:
    toc: no
    latex_engine: xelatex
    citation_package: natbib
    template: null
link-citations: yes
colorlinks: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

## some blocks from book

::: {.theorem name="Metropolis"}
A theorem
:::


## Some other blocks

::: {.lemma name="Metropolis"}
A lemma
:::

To be clear, this is the way it is supposed to work without this PR. it works this way for HTML and PDF format.
beamer is not supported though because of its specificity - it is your issue.

Do you expect the same syntax with bookdown::beamer_presentation2 ?

I really appreciate your feedback because I have a hard time choosing the correct solution for beamer.

@XiangyunHuang
Copy link

Do you expect the same syntax with bookdown::beamer_presentation2 ?

Yes. I feel better if they stay the same.

@cderv
Copy link
Collaborator Author

cderv commented Apr 21, 2021

Ok I could change that.

However, here are the limitations this will lead to:

  • bookdown's special syntax with name= attributes could be used with beamer_presentation2 only for environment defined in beamer and known by bookdown, as listed in don't respect beamer theme's buildin theorem/proof block #1143 (comment) (so theorem, lemma, corollary, definition, example, solution).
  • Other environment known by bookdown could be used if user defines them in a preamble.tex (so proposition, conjecture, exercise, hypothesis, remark)
  • For environment known only by beamer, the data-latex= or latex= syntax should be used. (so I think fact, problem, block and exampleblock).

So

  • For common environment known by bookdown, the syntax would be the same between pdf_document2, pdf_book and beamer_document2
  • For special beamer env, the syntax would be different. Also pdf_* format would not about those.

Is that ok ?

@XiangyunHuang
Copy link

XiangyunHuang commented Apr 21, 2021

OK, That's awesome! Thanks for your patience and great work!

Copy link
Contributor

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't review the Lua script carefully. Feel free to merge whenever both you and @XiangyunHuang are happy with the PR. Thanks!

Co-authored-by: Yihui Xie <[email protected]>
@cderv
Copy link
Collaborator Author

cderv commented Apr 21, 2021

@XiangyunHuang let's try that then. I'll push the change for you to test.

@yihui this is only a matter of choice with the syntax to use for beamer and related potential limitation if we support the bookdown syntax. If you have an opinion feel free to share. Otherwise I'll merge it as soon as we are ok with @XiangyunHuang

About the Lua filter, the changes are mainly to update based on latex-div.lua in rmarkdown as this is almost the same here that we are doing. It is also only about applying the filter for some known format to avoid issue with other unsupported format.

@cderv
Copy link
Collaborator Author

cderv commented Apr 21, 2021

@XiangyunHuang if you reinstall new this PR this is the syntax to use

---
title: "book"
output: 
  bookdown::beamer_presentation2:
    toc: no
    latex_engine: xelatex
    theme: Xiaoshan
    citation_package: natbib
    template: null
link-citations: yes
colorlinks: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

## some blocks from book

::: {.theorem name="Metropolis"}
A theorem
:::

::: {.lemma name="Metropolis"}
A lemma
:::

## Some other blocks

::: {.fact latex="[A fact]"}
Some interesting stuff
:::

::: {.problem latex=true}
A problem
:::
  • theorem and lemma are supported by bookdown, so you can use the name= syntax
  • fact and problem are only know by beamer so you can use them in a beamer format but you need to use the classic custom block syntax from rmarkdown (latex= or data-latex=)

Are we merging this ? 😄

@XiangyunHuang
Copy link

@cderv only one small problem, theorem and lemma block have extra blank line.

截屏2021-04-22 上午8 49 28

@cderv
Copy link
Collaborator Author

cderv commented Apr 22, 2021

Yes, there is an extra line where add the \label part to allow referencing. That is the main value of bookdown feature. It seems this happens when a theme is used

I don't get this extraline when using default
image

Basically, the Lua filter here will insert a \label for referencing and it seems to cause this new lines when a beamer theme is used. This label is need in other format for the referencing. If I compile a document with this syntax in pure LaTeX, then we get the new line

---
title: "book"
output: 
  beamer_presentation:
    keep_tex: true
    toc: no
    latex_engine: xelatex
    theme: Xiaoshan
    citation_package: natbib
    template: null
link-citations: yes
colorlinks: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

## some blocks from book

\begin{theorem}[Metropolis]
\label{thm:th1}A theorem with a label to reference
\end{theorem}

\begin{theorem}[Metropolis]
Another one without 
\end{theorem}

## Some other blocks

See Theorem \ref{thm:th1}

image

So what is the expected syntax in LaTeX for this ?
Are referencing not used with beamer theorem environment ?

If they aren't I don't think there is any value of using bookdown special handling of this environment and that we should only allow the other syntax for beamer presentation.

I could maybe remove this label insertion if there is no id provided in the div (no #id) but it will still insert a blank line if used with beamer.

cderv added 2 commits April 22, 2021 12:17
For HTML we need to generate a dummy label so that the numbering of environment works even if they won't be reference
@cderv
Copy link
Collaborator Author

cderv commented Apr 22, 2021

I could maybe remove this label insertion if there is no id provided in the div (no #id) but it will still insert a blank line if used with beamer.

I did that because we don't need to insert \label{} where there is no explicit id provided in the fenced div.

This should work better for you now I guess. But there would still be the new line inserted if you provided an id to use as a label reference.

Is this last change better ?

@yihui I added manual test file for visual inspection that everything is ok. I hope it is fine.

@yihui
Copy link
Contributor

yihui commented Apr 23, 2021

I added manual test file for visual inspection that everything is ok. I hope it is fine.

That's totally fine. Thanks!

@cderv
Copy link
Collaborator Author

cderv commented Apr 23, 2021

@XiangyunHuang just waiting for you confirmation before merging this PR. Hope it is ok for you now. I think so but I prefer to check. Thanks !

@XiangyunHuang
Copy link

@XiangyunHuang just waiting for you confirmation before merging this PR. Hope it is ok for you now. I think so but I prefer to check. Thanks !

It's OK. Thanks

@cderv cderv merged commit 2d692ba into master Apr 23, 2021
@cderv cderv deleted the block-beamer-support branch April 23, 2021 15:08
@cderv cderv added this to the v0.23 milestone Apr 27, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

don't respect beamer theme's buildin theorem/proof block theorem like environment are conflicted with template using cls file that already define it.
3 participants