If you inspect `bookdown::epub_book` you can see that it already adds `--mathml` which overwrites your `--webtex` argument passed to pandoc. That's why it does not work.
```
> format <- bookdown::epub_book(
+ fig_width = 5,
+ fig_height = 4,
+ dev = "png",
+ fig_caption = TRUE,
+ number_sections = TRUE,
+ toc = FALSE,
+ toc_depth = 2,
+ stylesheet = "style.css",
+ metadata = NULL,
+ chapter_level = 1,
+ md_extensions = NULL,
+ template = "default",
+ pandoc_args = c("--webtex")
+ )
> format$pandoc$args
[1] "--metadata-file"
[2] "C:\\Users\\..."
[3] "--webtex"
[4] "--number-sections"
[5] "--toc-depth"
[6] "2"
[7] "--mathml" <-- This overwrites "--webtex" argument
[8] "--split-level"
[9] "1"
[10] "--css"
[11] "C:\\Users\\..."
```
So in your `renderbook.R` use a simple wrapper function `epub_webtex` to replace `"--mathml"` with `"--webtex"`
```
# EPUB:
epub_webtex <- \(...) {
fmt <- bookdown::epub_book(...)
fmt$pandoc$args[format$pandoc$args == "--mathml"] <- "--webtex"
fmt
}
bookdown::render_book(
input = "index.Rmd",
output_format = epub_webtex(
fig_width = 5,
fig_height = 4,
dev = "png",
fig_caption = TRUE,
number_sections = TRUE,
toc = FALSE,
toc_depth = 2,
stylesheet = "style.css",
metadata = NULL,
chapter_level = 1,
md_extensions = NULL,
template = "default"
)
)
```
and testing with [epub-reader.online](https://epub-reader.online/#) gives
[](https://i.sstatic.net/M1HMsJpB.png)
Testing with [Kindle for Windows](https://www.amazon.com/b/ref=ruby_redirect?ie=UTF8&node=16571048011)
[](https://i.sstatic.net/H3PC5cHO.png)
Thanks for this RE. Moving to an answer because that's easier to share code. First, you have to use the `stylesheet` argument in `bookdown::epub_book` + the `!important` notation to enforce your styles. But even then, some ebook readers like calibre still overwrite your styles. The only thing that works for Calibre is to use a pandoc header-id or class CSS-selector as shown [here](https://stackoverflow.com/a/63075155/28479453):
```css
#header h1,
#header h2,
#header h3,
#header h4{
page-break-after: avoid !important;
page-break-inside: avoid !important;
break-after: avoid-page !important;
widows: 3 !important;
orphans: 3 !important;
}', "style.css")
```
````
# Title Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here. {#header}
...
## Subtitle Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here.
...
\newpage
````
and in the HTML, this will wrap your entire #Title-DOM in a section with the id = header
```
<section id="header" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Title Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here.</h1>...
<section id="subtitle-lots-of-text-here.-lots-of-text-here.-lots-of-text-here.-lots-of-text-here.-lots-of-text-here.-lots-of-text-here.-lots-of-text-here.-lots-of-text-here.-lots-of-text-here.-lots-of-text-here." class="level2" data-number="1.1">
<h2 data-number="1.1"><span class="header-section-number">1.1</span> Subtitle Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here. Lots of text here.</h2>...
<!--chapter:end:000_Book_body.Rmd-->
</section>
</section>
```
We can use this, to apply CSS to only child-objects of #header. And *only then* Calibre respects our styles.
## Reprex
**renderbook.R**
```
writeLines(text = '
bookdown::pdf_book:
css: style.css
config:
toc:
before: |
<li><a href="./">Book title</a></li>
after: |
<li><a href="https://github.com/rstudio/bookdown" target="blank">Impaginato con bookdown</a></li>
bookdown::epub_book:
css: style.css
toc_depth: 2', '_output.yml')
writeLines(text = '
p.caption {
color: #777;
margin-top: 10px;
}
p code {
white-space: inherit;
}
pre {
word-break: normal;
word-wrap: normal;
}
pre code {
white-space: inherit;
}
.caption { text-align: left; }
/* Apply styles to children of object with id = header */
#header h1,
#header h2,
#header h3,
#header h4 {
page-break-after: avoid !important;
page-break-inside: avoid !important;
break-after: avoid-page !important;
widows: 3 !important;
orphans: 3 !important;
}', "style.css")
writeLines(
text = paste(
'---
title: My title
subtitle: My subtitle
author: "Me"
toc-title: Indice
toc_depth: 2
toc: false
output: pdf_book
---
\newpage
\thispagestyle{empty}
This page intentionally blank
\newpage
\thispagestyle{empty}
This page intentionally blank
\newpage
\tableofcontents
\newpage'),
"index.Rmd"
)
lots <- \(n) paste(rep("Lots of text here.", n),collapse = " ")
sections <- c(
paste("# Title", lots(10), "{#header}\n\n", lots(13), "\n\n", lots(1), "\n\n", lots(12), "\n\n", lots(1), "\n\n", lots(60)),
paste("## Subtitle", lots(10), "\n\n", lots(1), "\n\n", lots(12), "\n\n", lots(61), "\n\n", lots(12)),
"\\newpage"
)
writeLines(paste(sections, collapse = "\n\n"), "000_Book_body.Rmd")
# Build EPUB:
bookdown::render_book(
input = "index.Rmd",
bookdown::epub_book(
fig_width = 5,
fig_height = 4,
dev = "png",
fig_caption = TRUE,
number_sections = TRUE,
toc = FALSE,
toc_depth = 2,
stylesheet = "style.css",
metadata = NULL,
chapter_level = 1,
md_extensions = NULL,
pandoc_args = NULL,
template = "default"
)
)
```
**File Structure `printtree::print_rtree(max_depth = 1)`**
```
main/
|-- _output.yml
|-- 000_Book_body.Rmd
|-- index.Rmd
|-- renderbook.R
`-- style.css
```
___
## Tests
[`https://epub-reader.online/`](https://epub-reader.online/)
[](https://i.sstatic.net/jd0VMgFd.png)
___
[`Calibre`](https://calibre-ebook.com/download), you can see that headers do not get broken across pages anymore!
[](https://i.sstatic.net/oD91kFA4.gif)
___
[ReadEra](https://readera.org/) tests failed when I zoom in a lot, Headers still get broken across pages. Due to the page-like scrolling action in the app, this really can't be prevented. The same goes for the books-app. I think it's not possible. Only if you use PDF.