rstudio / bookdown

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

Enhancement: option to disable bookdown extension #750

Open jm-t opened 5 years ago

jm-t commented 5 years ago

It would be useful to have the option to disable bookdown extension. Or to change where there are injected in the template.

Here is a use case. One basic markdown, one basic preamble.tex

---
title: Theorem test
author: jm
output:
  bookdown::pdf_book:
    includes:
      in_header: preamble.tex
    keep_tex: yes
  bookdown::gitbook: default  
---

# Hello world

Normal text

```{theorem, name="Pythagorean theorem"}
For a right triangle, if $c$ denotes the length of the hypotenuse
and $a$ and $b$ denote the lengths of the other two sides, we have
$$a^2 + b^2 = c^2$$

Normal text

% Start of preamble

\newenvironment{FOO}{}

% of of preamble


The generated tex file contains the following:

% Start of preamble

\newenvironment{FOO}{}

% of of preamble

\usepackage{amsthm} \newtheorem{theorem}{Theorem}[section] \newtheorem{lemma}{Lemma}[section] \newtheorem{corollary}{Corollary}[section] \newtheorem{proposition}{Proposition}[section] \newtheorem{conjecture}{Conjecture}[section] \theoremstyle{definition} \newtheorem{definition}{Definition}[section] \theoremstyle{definition} \newtheorem{example}{Example}[section] \theoremstyle{definition} \newtheorem{exercise}{Exercise}[section] \theoremstyle{remark} \newtheorem{remark}{Remark} \newtheorem{solution}{Solution} \let\BeginKnitrBlock\begin \let\EndKnitrBlock\end \begin{document} \maketitle



Looking at the template, the insertion before the begin{document} is not defined there. My search leads to Latex.R file. I do not understand properly what is happening there.

The issue is that one user cannot define theorems using any of these names in the preamble. The current workaround would be to define them in the include before body.
fyuniv commented 4 years ago

Currently, you may add the following codes at the beginning of your index.Rmd file to remove the LaTeX commands injected from the default template.

options(bookdown.post.latex = function(x) {
    from <- grep("usepackage\\{amsthm\\}", x)
    to <- grep("newtheorem\\*\\{solution", x)
    x <- x[-c(from:to)]
  })

See the Issue 417 and the solution in there.

cderv commented 3 years ago

@jm-t thanks for your patience with this.

The issue is that one user cannot define theorems using any of these names in the preamble. The current workaround would be to define them in the include before body.

Did you try already to define yourself a new theorem environment ?

Currently, if your preamble already include

\newtheorem{theorem}

Then bookdown will not insert any other Theorem or Proof environments. It will let the user add them oneself in the preamble. This is what happens in Latex.R. We do that in order the bookdown book can use these environment without the user knowing how to define them in LaTeX, but we don't add them in the template itself in case someone wants to override it.

Does that work for you ?

I think we can easily add an option to deactivate the inclusion of bookdown supported environment and let the user do it. This would mean that the book would not render if the preamble is not correctly setup.

Something like options(bookdown.theorems_preamble = FALSE) We could also not include it when \newtheorem{[a-z]+} is found.

Would that be better than the current way that should work currently ?

yihui commented 3 years ago

@cderv Does options(bookdown.theorem.preamble = FALSE) work now? https://github.com/rstudio/bookdown/blob/2d692ba4b61f1e466c92e78fd712b0ab08c11d31/R/latex.R#L239

cderv commented 3 years ago

Oh yes I forgot about this issue.

@jm-t setting options(bookdown.theorem.preamble = FALSE) will prevent bookdown adding any definition in the .tex file. Users needs to define the environment or use a package already containing these definitions.

So with this, you'll be able to define yourself some theorem environment.

Does that seem ok for you ?