mathjaxquartoreveal.js

How to add MathJax macros in a Quarto Revealjs presentation?


I am trying to define a new operator in Quarto presentation (revealjs), but I either fail or it adds a white empty slide. Here are two examples:

Fails

This code fails in the sense that \N is not understood and litteraly written.

---
title: "Test title"
date: today
author: Test author
date-format: iso
keep-tex: true
format:
  revealjs: default
---

::: {.content-hidden}

\newcommand{\test}{f_{\text{test}}}
\renewcommand{\phi}{\varphi}

$$
\DeclareMathOperator{\N}{\mathcal{N}}
$$

:::

## First slide

$$
\test = \N(\phi)
$$

Works but add a white slide

This time \N is written because the math operator is declared in the html (see after qmd block code for the html output)

---
title: "Test title"
date: today
author: Test author
date-format: iso
keep-tex: true
format:
  revealjs: default
---

::: {.hidden}

\newcommand{\test}{f_{\text{test}}}
\renewcommand{\phi}{\varphi}

$$
\DeclareMathOperator{\N}{\mathcal{N}}
$$

:::

## First slide

$$
\test = \N(\phi)
$$

Opening the html output file, I found:

<section class="slide level2">

<div class="hidden">
<p><span class="math display">\[
\DeclareMathOperator{\N}{\mathcal{N}}
\]</span></p>
</div>
</section>

So, how to add latex macros in quarto revealjs? I also tried to hide the slide itself rather than the content, as shown in this post, but it did not work out.


Solution

  • This is because if you put anything before the first header, it will be regarded as content which has to be put on a slide. The solution is that you define things afterwards:

    ---
    title: "Test title"
    date: today
    author: Test author
    date-format: iso
    keep-tex: true
    format:
      revealjs: default
    ---
    
    ## First slide
    
    ::: {.hidden}
    
    \newcommand{\test}{f_{\text{test}}}
    \renewcommand{\phi}{\varphi}
    
    $$
    \DeclareMathOperator{\N}{\mathcal{N}}
    $$
    
    :::
    
    $$
    \test = \N(\phi)
    $$