Closed GitHunter0 closed 2 years ago
Do you have any ideas about how this would work or what the API would look like?
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)
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 |
See figure \@ref(fig:car)
plot(cars)
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]
lib_dir: libs
nature:
highlightStyle: arta
highlightLines: true
countIncrementalSlides: false
includes:
# this allows equation numbering:
in_header: "www/mathjax-equation-numbers.html"
## Global Chunk settings ----
knitr::opts_chunk$set(warning=FALSE, message=FALSE)
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)$$
NOTE: xaringan
renders (n n)/ (k k) denominator too small (considerably smaller than ioslides
for example). Is there a way to correct that?
Labeled equation $$ \mathrm{cov}(\epsilon_s, \epsilon_t) = \left\lbrace \begin{align} & \sigma^2 \quad \mathrm{if} \; s = t \nonumber\ & 0 \quad \mathrm{else} \label{eq:eqname0} \ \end{align} \right. $$
NOTE: xaringan
does not render equation \eqref{eq:eqname0} above correctly and also the equation number is not displayed.
NOTE: these sometimes render correctly and label/cross-reference works and sometimes they don't, I have no idea why.
Bookdown
default table caption only works with kable()
See table \ref{tab:tables-mtcars}
knitr::kable(iris[1:5, ], caption = 'A caption', longtable=TRUE)
xaringan
tooSee table \ref{tab:my-tab1} below.
xaringan
tooSee 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 .
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.
Thanks man, I appreciate the feedback. Yihui is usually extremely busy but I will try to open this issue there.
The issue was opened here https://github.com/yihui/xaringan/issues/335
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