r-lib / pkgdown

Generate static html documentation for an R package
https://pkgdown.r-lib.org/
Other
721 stars 336 forks source link

Math rendering for LaTeX arrays and equation number in vignettes #2772

Open friendly opened 2 months ago

friendly commented 2 months ago

[My env: R version 4.4.1, RStudio 2023.06.1, pkgdown 2.1.0]

This issue is similar to https://github.com/r-lib/pkgdown/issues/2739 in that it uses bookdown::html_document2

I'm writing a vignette for the matlib package on our new facilities for creating LaTeX matrices & matrix equations in R. It creates more complex LaTeX than usual cases, but these should be handled by pkgdown.

The vignette in question has this YAML header (trying to use mathjax rather than mathml.

---
title: "LaTeX Equations with latexMatrix, Eqn and matrix2latex"
author: Phil Chalmers, John Fox, Michael Friendly
date: "`r Sys.Date()`"
output: 
  bookdown::html_document2:
    base_format: rmarkdown::html_vignette
    number_sections: false
    toc: true
  pdf_document:
    toc: true
    keep_tex: true
template:
  math-rendering: mathjax
bibliography: "references.bib"
vignette: >
  %\VignetteIndexEntry{LaTeX Equations with latexMatrix, Eqn and matrix2latex}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
---

There are two problems with what pkgdown produces compared to what I get when I just use knitr on the vignette .Rmd file:

(1) Equation numbers don't appear and cannot be referenced

One example uses our functions as so to produce two equations with \label{}s

Eqn("\\mathbf{X} = \\mathbf{U} \\mathbf{\\Lambda} \\mathbf{V}^\\top", label='eq:svd')
Eqn(latexMatrix("u", "n", "k"),
    latexMatrix("\\lambda", "k", "k", diag=TRUE),
    latexMatrix("v", "k", "p", transpose = TRUE), label='eq:svdmats')

This produces the LaTeX code:

\begin{equation}
\label{eq:svdmats}
\begin{pmatrix} 
  u_{11} & u_{12} & \cdots & u_{1k} \\ 
  u_{21} & u_{22} & \cdots & u_{2k} \\ 
  \vdots & \vdots &        & \vdots \\ 
  u_{n1} & u_{n2} & \cdots & u_{nk} \\ 
\end{pmatrix}
\begin{pmatrix} 
  \lambda_{1} & 0           & \cdots      & 0           \\ 
  0           & \lambda_{2} & \cdots      & 0           \\ 
  \vdots      & \vdots      & \ddots      & \vdots      \\ 
  0           & 0           & \cdots      & \lambda_{k} \\ 
\end{pmatrix}
\begin{pmatrix} 
  v_{11} & v_{12} & \cdots & v_{1p} \\ 
  v_{21} & v_{22} & \cdots & v_{2p} \\ 
  \vdots & \vdots &        & \vdots \\ 
  v_{k1} & v_{k2} & \cdots & v_{kp} \\ 
\end{pmatrix}^\top
\end{equation}

From knitr, this appears correctly as below, and the equations can be referenced with @ref(eq:svd)

image

From pkgdown, I get:

image

(2) We introduce methods for partitioned matrices:

M <- latexMatrix("m", 4, 4)
partition(M, rows=2, columns=2)

This generates the LaTeX code that uses a \begin{array} ... \end{array} environment with vertical & horizontal rules.

\begin{pmatrix}  
\begin{array}{c c | c c}
m_{11} & m_{12} & m_{13} & m_{14}\\ 
m_{21} & m_{22} & m_{23} & m_{24}\\ 
\hline m_{31} & m_{32} & m_{33} & m_{34}\\ 
m_{41} & m_{42} & m_{43} & m_{44}\\ 
\end{array}
\end{pmatrix}

In knitr, this works fine:

image

But from pkgdown all I get is this:

image

Are there any pkgdown settings I could try to change that might make these work?

jayhesselberth commented 2 months ago

What's in your _pkgdown.yml? This bit should be there, not in the vignette header.

template:
  math-rendering: mathjax
jayhesselberth commented 2 months ago

also see @hadley's suggestion in one of these mathy issues to move to quarto, it will be the supported path moving forward (over bookdown, at least).

jayhesselberth commented 2 months ago

The strategy here https://github.com/r-lib/pkgdown/issues/2704#issuecomment-2307055568 gives you the nicest math rendering. I submitted a PR: https://github.com/friendly/matlib/pull/61.

Unfortunately equation cross-references don't appear to work yet.

image
friendly commented 2 months ago

Thanks so very much for the PR to use katex. This solves the problem with partitioned matrices, but not as you note for equation numbers.

We have quite a few vignettes here, and it seems like a lot of work to convert to Quarto, even though that might be the way for the future.