Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Numbered Equations (and solve some math rendering issues) #335

Open
GitHunter0 opened this issue Oct 27, 2021 · 0 comments
Open

Comments

@GitHunter0
Copy link

Hey @yihui , how are you?

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

I was surprised Xaringan didn't have it natively since it is an indispensable feature in many fields.

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

If so, below I'm sharing my thoughts about the feature while also highlighting some math rendering issues I found:

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)

```{r "car", fig.cap = 'Title of the plot', fig.fullwidth=TRUE}
plot(cars)
```

## Image

See figure \@ref(fig:myfig)
```{r "myfig", fig.cap = 'Title of the plot', fig.fullwidth=TRUE}
knitr::include_graphics("www/myfigure1.png")
```

See attached PDF with the output:
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 already did that as explained here.
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
---

```{r include=FALSE }
## Global Chunk settings ----
knitr::opts_chunk$set(warning=FALSE, message=FALSE)	 
```


# ___ 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)$$
- 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.

---

# A Single Equation that works

- Labeled Equation
$$
\begin{align} 
y = \frac{(x-\mu)}{(max-min)} \label{eq:eqnameY} \\
\end{align}
$$
- See equation \eqref{eq:eqnameY} above.

---

# Block of Equations

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

- 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} \nonumber \\
\frac{\partial f}{\partial x}(x=3) &= y^2  \label{eq:eqname1}  \\
y &= \frac{(x-\mu)}{(max-min)} \nonumber \\
\end{align}
$$
- See system of equations \eqref{eq:eqname1} above. 
- 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)
```

---

# Latex Tables

- This works in LaTeX and ideally should work in `xaringan` too

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

- This works in LaTeX and ideally should work in `xaringan` too

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:

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    TeX: { equationNumbers: { autoNumber: "AMS" } },
  });
</script>
<style>
.mjx-mrow a {
  color: black;
  pointer-events: none;
  cursor: default;
}
</style>

We can see in attached PDF with the output below that many things failed, either by xaringan's math rendering issues or by the limitation of in_header: "www/mathjax-equation-numbers.html" solution.
Xaringan.Cross-Reference.pdf

It would be really great if xaringan could solve those issues and also avoid the necessity to declare external files in YAML metadata.

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant