gadenbuie / xaringanExtra

:ferris_wheel: A playground of enhancements and extensions for xaringan slides.
https://pkg.garrickadenbuie.com/xaringanExtra
Other
448 stars 36 forks source link

Support Numbered Equations #141

Closed GitHunter0 closed 2 years ago

GitHunter0 commented 2 years ago

Hi @gadenbuie ,

Please, would you consider adding support for numbered equations & cross-reference?

Xaringan for some reason does not have it natively but it is an indispensable feature in many fields...

That would be a great contribution to the package in my opinion.

Thank you

gadenbuie commented 2 years ago

Do you have any ideas about how this would work or what the API would look like?

GitHunter0 commented 2 years ago

Hey @gadenbuie , yes I have.

I believe there are 2 important compatibilities the API should have: bookdown and plain Latex.

The bookdown package introduced cross-reference capabilities to R Markdown and it would be really desirable the syntax used by it to be accepted in xaringan too. Here is a MWE:

---
title: "Bookdown HTML Article - Cross-Reference"
output:
  bookdown::html_document2:
    toc: true
    toc_depth: 5
    number_sections: true
    toc_collapsed: false
    toc_float:
      collapsed: false
      smooth_scroll: false
    anchor_sections: TRUE
---

# Equations Label & Cross-Reference

## Single Equation

- Unlabeled equation
$$\int\limits_a^b f(x)dx = \frac{F(x)}{\frac{\binom{n}{n}}{\binom{k}{k}}} \Biggr|^b_a = F(b)-F(a)$$
- Labeled equation
$$
\mathrm{cov}(\epsilon_s, \epsilon_t) =
\left\lbrace
\begin{align}
 & \sigma^2  \quad \mathrm{if}  \; s = t \\
 & 0         \quad \mathrm{else} \\
\end{align}
\right. 
(\#eq:eqname0) 
$$

- See equation \@ref(eq:eqname0) above.

## Block of Equations

- Labeled system of equations
$$
\begin{align}
\left( \sum_{i=1}^{n}{i} \right)^2 &= \left( \frac{n(n-1)}{2}\right)^2 = \frac{n^2(n-1)^2}{4} (\#eq:eqname1) \\
\frac{\partial f}{\partial x}(x=3) &= y^2  \\
y &= \frac{(x-\mu)}{(max-min)} \\
\end{align}
$$
- See system of equations \@ref(eq:eqname1) above.

# Tables Caption & Cross-Reference

## kable() Table

`Bookdown` default table caption only works with `kable()`

See table \@ref(tab:tables-mtcars)

```{r tables-mtcars}
knitr::kable(iris[1:5, ], caption = 'A caption', longtable=TRUE)

Custom Tables

See Table \@ref(tab:mytab).

Table: (#tab:mytab) Your table caption.

Sepal.Length Sepal.Width Petal.Length
5.1 3.5 1.4
5.2 3.6 1.5

Figures Caption & Cross-Reference

Plot

See figure \@ref(fig:car)

plot(cars)

Image

See figure \@ref(fig:myfig)

knitr::include_graphics("www/myfigure1.png")
See attached PDF with the output:
[Bookdown HTML Article - Cross-Reference.pdf](https://github.com/gadenbuie/xaringanExtra/files/7226834/Bookdown.HTML.Article.-.Cross-Reference.pdf)

It should also ideally allow the standard Latex API for cross-referencing. As a matter of fact, this [repository](https://github.com/garthtarr/sydney_xaringan) already did that as explained [here](https://garthtarr.github.io/sydney_xaringan/#48).
However, its cross-reference system just works for equations and not for tables and figures.

Here is a MWE:

title: "Xaringan Cross-Reference" output: xaringan::moon_reader: self_contained: false css: [default, metropolis, metropolis-fonts]

css: [default, shinobi, metropolis-fonts] # custom themes

lib_dir: libs
nature:
  highlightStyle: arta
  highlightLines: true
  countIncrementalSlides: false
includes:
  # this allows equation numbering:
  in_header: "www/mathjax-equation-numbers.html"

editor_options: chunk_output_type: console

## Global Chunk settings ----
knitr::opts_chunk$set(warning=FALSE, message=FALSE)  

Equations Label & Cross-Reference


Single Equation


A Single Equation that works


Block of Equations

NOTE: these sometimes render correctly and label/cross-reference works and sometimes they don't, I have no idea why.


Tables Caption & Cross-Reference


kable() Table

Bookdown default table caption only works with kable()

See table \ref{tab:tables-mtcars}

knitr::kable(iris[1:5, ], caption = 'A caption', longtable=TRUE)

Latex Tables

See table \ref{tab:my-tab1} below.

\begin{table}[htbp!] \centering \caption{\label{tab:my-tab1} Caption} \begin{tabular}{ccccc}
1 & 2 & 3 & 4 & 5 \
6 & 7 & 8 & 9 & 10 \ \end{tabular} \end{table}

Figures Caption & Cross-Reference


Image

See Figure \ref{fig:image1} below:

\begin{figure}[h!]
\includegraphics[scale=0.5]{www/myfigure1.png} \caption{Figure Title}\label{fig:image1} \end{figure}

Where the file `mathjax-equation-numbers.html` content is:


We can see in attached PDF with the output below that many things failed, either by `xaringan`'s math rendering issues (which might need to be corrected in the original package) or by the limitation of `in_header: "www/mathjax-equation-numbers.html"` solution. 
[Xaringan Cross-Reference.pdf](https://github.com/gadenbuie/xaringanExtra/files/7226837/Xaringan.Cross-Reference.pdf)

It would be really great if `xaringanExtra` could solve many of those issues and also avoid the necessity to declare external files in YAML metadata .
gadenbuie commented 2 years ago

Thanks for writing this up! Unfortunately, I don't think this feature is a good fit for xaringanExtra. There's a lot of lower-level things that are needed to make this work, and xaringanExtra tends towards features that are possible mostly in the browser or with a small amount of messing with knitr hooks.

I do think that the setting up the mathjax configuration to enable cross-referencing might be something xaringan itself would consider. You could try describing the issue at https://github.com/yihui/xaringan to see if they're open to it there.

GitHunter0 commented 2 years ago

Thanks man, I appreciate the feedback. Yihui is usually extremely busy but I will try to open this issue there.

GitHunter0 commented 2 years ago

The issue was opened here https://github.com/yihui/xaringan/issues/335