Quarto: Test LaTeX newcommands in HTML doc

In a Quarto document, I want to be able to use more extensive math LaTeX features than are typical for simple equations, using some extensions presumably provided by mathjax, particularly for matrix expressions. (I think, but am not sure, that the latest Quarto uses mathjax v 3.2. )

As a simiple example, here I use a straightforward math equation with no special stuff, with \mathbf{} used for matrices and vectors. This works well and the equation appears nicely in the RStudio editor window. The source code is:

$$\mathbf{y} = \mathbf{X} \; \mathbf{\beta} + \mathbf{u}, 
\quad\mbox{   with   }\quad 
\mathbf{u} \sim \mathcal{N} (0, \sigma^2 \mathbf{I}) ,
$$

\[\mathbf{y} = \mathbf{X} \; \mathbf{\beta} + \mathbf{u}, \quad\mbox{ with }\quad \mathbf{u} \sim \mathcal{N} (0, \sigma^2 \mathbf{I}) , \]

However, in another version, I’d like to show the size of matrices using subscripts.
This also works, but it fails if I try to include an equation number (eq-mlm).

$$
\mathbf{Y}_{n \times p} = 
\mathbf{X}_{n \times q} \, \mathbf{B}_{q \times p} + \mathbf{U}_{n \times p} \:\: ,
$$

\[ \mathbf{Y}_{n \times p} = \mathbf{X}_{n \times q} \, \mathbf{B}_{q \times p} + \mathbf{U}_{n \times p} \:\: , \]

\newcommand

Now, try to define some abbreviated latex commands for these math expressions. The Quarto authoring doc says these should work

For HTML math processed using MathJax (the default) you can use the \def, \newcommand, \renewcommand, \newenvironment, \renewenvironment, and \let commands to create your own macros and environments.

$$
\renewcommand*{\vec}[1]{\mathbf{#1}} 
\newcommand*{\mat}[1]{\mathbf{#1}} 
\newcommand{\trans}{^\mathsf{T}} 
\newcommand*{\diag}[1]{\mathrm{diag}\, #1}}
\newcommand{\sizedmat}[2]{\underset{\mathbf{#1}}{(#2)}}
$$

The docs say to include this in a ::: {.hidden} div:

Then, I try to use them:

$$
{\sizedmat{\mat{Y}}{n \times p}} = 
{\sizedmat{\mat{X}}{n \times q}} \, 
{\sizedmat{\mat{B}}{q \times p}} + 
{\sizedmat{\mat{U}}{n \times p}} ,
$$

This doesn’t work, perhaps because the \newcommand block wasn’t interpreted as equations. (Or, could this be because of the \underset ?)

\[ {\sizedmat{\mat{Y}}{n \times p}} = {\sizedmat{\mat{X}}{n \times q}} \, {\sizedmat{\mat{B}}{q \times p}} + {\sizedmat{\mat{U}}{n \times p}} , \]

Other mathjax extensions

Let’s try Andrew Heiss’ nice equations using the mathjax mathtools extension. This LaTeX defines some colors:

$$
\require{mathtools}
\definecolor{bayesred}{RGB}{147, 30, 24}
\definecolor{bayesblue}{RGB}{32, 35, 91}
\definecolor{bayesorange}{RGB}{218, 120, 1}
\definecolor{grey}{RGB}{128, 128, 128}
{\color{bayesorange} P (\text{H} \mid \text{E})} = \frac
{{\color{bayesred} P(\text{H})} \times 
{\color{bayesblue}P(\text{E} \mid \text{H})}}
{\color{grey} {P(\text{E})}}
$$

This uses these colors:

$$
{\color{grey} \overbracket[0.25pt]{\color{bayesorange} P (\text{Unknown} \mid \text{Data})}^{\text{Posterior}}} = \frac
{{\color{grey} \overbracket[0.25pt]{\color{bayesred} P (\text{Unknown})}^{\text{Prior}}} \times 
{\color{grey} \overbracket[0.25pt]{\color{bayesblue} P (\text{Data} \mid \text{Unknown})}^{\text{Likelihood}}}}
{{\color{grey} \underbracket[0.25pt]{{\color{grey} P(\text{E})}}_{\text{Average likelihood}}}}
$$

The result does not appear nicely in the RStudio editor window, but works fine in the output.

\[ {\color{grey} \overbracket[0.25pt]{\color{bayesorange} P (\text{Unknown} \mid \text{Data})}^{\text{Posterior}}} = \frac {{\color{grey} \overbracket[0.25pt]{\color{bayesred} P (\text{Unknown})}^{\text{Prior}}} \times {\color{grey} \overbracket[0.25pt]{\color{bayesblue} P (\text{Data} \mid \text{Unknown})}^{\text{Likelihood}}}} {{\color{grey} \underbracket[0.25pt]{{\color{grey} P(\text{E})}}_{\text{Average likelihood}}}} \]