rstudio / bookdown

Authoring Books and Technical Documents with R Markdown
https://pkgs.rstudio.com/bookdown/
GNU General Public License v3.0
3.77k stars 1.27k forks source link

Unnumbered or manually numbered theorems etc [FR] #1168

Open thosgood opened 3 years ago

thosgood commented 3 years ago

I'm aware of e.g. https://github.com/rstudio/bookdown/issues/869 which suggests the ability to do consecutive numbering amongst numbered theorem/lemma/proposition environments, but something that would also be useful is just to have unnumbered/manually numbered theorems.

For example, I would love for

::: {.proposition}
  This is an example.
:::

to produce

Proposition. This is an example.

and for

::: {.proposition, 3.1}
  This is an example.
:::

to produce

Proposition 3.1. This is an example.

Are either of these currently possible, or not at all?

thosgood commented 3 years ago

Here is my current workaround, which I think could maybe be adapted to solve a few other problems (such as asking for more environment names) all at the same time.

I have two custom environments, .itenv and .rmenv (the former for italic text and the latter for normal), which I use for all my theorems/propositions/definitions/remarks. The syntax is

::: {.itenv #I.7.8 title="Corollary 7.8"}
  Lorem ipsum dolor sit amet.
:::

which, along with some css (the key part being

.itenv:before {
  content: attr(title);
  font-style: normal;
  font-weight: bold;
  padding-right: 0.5em;
  float: left;
}

which prepends the content of the title attribute) allows me to number (and reference) any sort of environment however I want.

The main problem with this approach is the repetition: if I want this to work in the LaTeX output, then I'd need to have

::: {.itenv #I.7.8 title="Corollary 7.8" data-latex="Corollary 7.8"}

and I don't know a way around this (apart from running some custom script to add this data-latex attribute automatically before building).

cderv commented 3 years ago

Hi @thosgood, thanks for opening this feature request.

Currently, bookdown supports Theorems & Proofs environement with this definition:

a “theorem” is just a numbered/labeled environment, and it does not have to be a mathematical theorem (e.g., it can be an example irrelevant to mathematics). Similarly, a “proof” is an unnumbered environment.

Proposition is among the Theorems and so it is only numbered in the context of bookdown. In addition to the doc above, we'll support the fenced div syntax that you mention.

---
title: "Untitled"
output: bookdown::html_document2
---

# About fenced div environment 

One can use fenced div syntax from Pandoc to create a Proof environment.

# Example for a proposition environment

::: {.proposition #prop1 name="My Prop"}
  This is an example.
:::

See the proposition \@ref(prp:prop1)

image

The environment will be numbered so that it can be easily referenced.

So

The supported unnumbered theorem-like environments are called Proof and for now they are proof, remark and solution

---
title: "Using Proof env"
output: bookdown::html_document2
---

::: {.remark name="My Remark"}
  This is an example of an unnumbered remark environment.
:::

image

Those can't be referenced and are unnumbered.

About your workaround, this is great ! You can definitely use Custom block with fenced div to customize as you want your document. Theorems and Proof in bookdown is powered by this, but this is just a special handling so that the output is the same in HTML and PDF. You can definitly create your own.

If you want to improve the handling for HTML and PDF output, it would require a Lua filter as we do in bookdown so that you can tweak the output of HTML and PDF according to the attributes passed in your fenced div. Pandoc does not support creating LaTeX environment using fenced div and you need Lua filter for this.

We could definitely improve the Lua filter included in bookdown to give more flexibility. So to go further, can I ask:

The tricky part if we want to improve would be to be sure to support all the output format the same, and that the referencing mechanism must keep working.

Thanks.

thosgood commented 3 years ago

So to go further, can I ask:

* Are you specifically searching for a _proposition_ that is unnumbered ?

* Do you find Theorems & Proofs env in **bookdown** not working as you expect ?

* Why do you need to manually number your environment instead of relying to auto numbering ?

My main motivation here is that I do a lot of translations of mathematics articles, which means that the numbering is already set in stone. Since different articles number things differently (some by section, some sequentially, some with a shared numbering for all theorems/propositions etc.), the easiest way to reproduce things is to manually number everything myself. Another thing that sometimes appears though is e.g. unnumbered corollaries, or numbered remarks.

The most flexible solution then (for me) is to simply have an .itenv and a .rmenv, as I described above, and be able to number these (or not) individually (and if they are numbered then I can reference them; if they are not numbered then I don't expect to be able to reference them).

I understand that my use case is very specific, but I was thinking that people might be happy with this as a way to (1) be able to have any theorem titles they want, rather than having to ask for them to be implemented specifically; and (2) have a sort of workaround for https://github.com/rstudio/bookdown/issues/869.