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

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

Closed
friendly opened this issue Aug 31, 2024 · 5 comments
Closed

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

friendly opened this issue Aug 31, 2024 · 5 comments

Comments

@friendly
Copy link

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

This issue is similar to #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
Copy link
Collaborator

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

template:
  math-rendering: mathjax

@jayhesselberth
Copy link
Collaborator

jayhesselberth commented Aug 31, 2024

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
Copy link
Collaborator

The strategy here #2704 (comment) gives you the nicest math rendering. I submitted a PR: friendly/matlib#61.

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

image

@friendly
Copy link
Author

friendly commented Sep 1, 2024

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.

@jayhesselberth
Copy link
Collaborator

Closing this issue as you have a good workaround. We'll hopefully improve math support in pkgdown in the future.

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

No branches or pull requests

2 participants