friendly / matlib

Matrix Functions for Teaching and Learning Linear Algebra and Multivariate Statistics
http://friendly.github.io/matlib/
65 stars 16 forks source link

Equation numbers and xrefs don't work in HTML output #52

Closed friendly closed 3 months ago

friendly commented 3 months ago

I copied dev/Eqn_test.Rmd to vignettes/latex-equations.Rmd to make it into a vignette, using HTML format as is usual.

In the YAML header, I have

output: 
  bookdown::html_document2:
    base_format: rmarkdown::html_vignette

which is what I've used in other vignettes to give figure numbers that can be cross-referenced. However, for equations, I get no numbers and xrefs don't work. This may be an issue with bookdown, but maybe there's something I'm missing.

philchalmers commented 3 months ago

This is a MathJax problem. If you add this to the top of the script it will work.

<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  TeX: { equationNumbers: { autoNumber: "AMS" } }
});
</script>
philchalmers commented 3 months ago

I see that the markdown fix is easy enough though; \label{eq:myeqn} ->(\#eq:myeqn), which I can add to Eqn() to try and detect the correct output without introducing hacks.

This is the detection approach I'm currently thinking, but I'm not sure how sound it is (what if one is exporting to Word?)

getOutputType <- function(){
    type <- 'latex'   # for interactive/non-interactive source()
    # if(knitr::is_latex_output()) type <- 'latex'
    if(knitr::is_html_output()) type <- 'html'
    type
}

if(getOutputType() == 'html') print_other_label()
philchalmers commented 3 months ago

... rmarkdown drives me crazy sometimes. I couldn't even get this to render in the output.

\begin{equation} 
(\#eq:binom)
  f\left(k\right) = \binom{n}{k} p^k\left(1-p\right)^{n-k}
\end{equation} 

The equation \@ref(eq:binom) ...

and it's because you need

output: bookdown::html_document2

instead of rmarkdown::html_vignette. So now we're at an impasse; force the bookdown approach whenever the output should be HTML, in which case the equation labels and how they are reference should be changed, or try the MathJaX header configuration for the more generic HTML solution. The latter is more tempting.