Skip to content

Render LaTeX formulas in md() when target is HTML (standalone/JS-free via katex) #1578

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

Merged
merged 18 commits into from
Feb 13, 2024

Conversation

rich-iannone
Copy link
Member

@rich-iannone rich-iannone commented Feb 12, 2024

This PR allows for the use of LaTeX formulas between $ and $$ delimiters when rendering an HTML table (but not in the Quarto rendering environment). This requires katex as an optional dependency, which makes the rendering of formula text dependency-free in the output (advantage is that formulas could work in environments where script tags aren't allowed, like email).

Here is an example demonstrating that fmt_markdown() and md() can handle LaTeX formulas:

library(gt)
library(tidyverse)

dplyr::tibble(
  idx = 1:5,
  l_time_domain =
    c(
      "$$1$$",
      "$${{\\bf{e}}^{a\\,t}}$$",
      "$${t^n},\\,\\,\\,\\,\\,n = 1,2,3, \\ldots$$",
      "$${t^p}, p > -1$$",
      "$$\\sqrt t$$"
    ),
  l_laplace_s_domain =
    c(
      "$$\\frac{1}{s}$$",
      "$$\\frac{1}{{s - a}}$$",
      "$$\\frac{{n!}}{{{s^{n + 1}}}}$$",
      "$$\\frac{{\\Gamma \\left( {p + 1} \\right)}}{{{s^{p + 1}}}}$$",
      "$$\\frac{{\\sqrt \\pi }}{{2{s^{\\frac{3}{2}}}}}$$"
    )
) |>
  gt(rowname_col = "idx") |>
  fmt_markdown() |>
  cols_label(
    l_time_domain = md(
      "Time Domain<br/>$\\small{f\\left( t \\right) =
      {\\mathcal{L}^{\\,\\, - 1}}\\left\\{ {F\\left( s \\right)} \\right\\}}$"
    ),
    l_laplace_s_domain = md(
      "$s$ Domain<br/>$\\small{F\\left( s \\right) =
      \\mathcal{L}\\left\\{ {f\\left( t \\right)} \\right\\}}$"
    )
  ) |>
  tab_header(
    title = md(
      "A (Small) Table of Laplace Transforms &mdash; $\\small{{\\mathcal{L}}}$"
    ),
    subtitle = md(
      "Five commonly used Laplace transforms and formulas.<br/><br/>"
    )
  ) |>
  cols_align(align = "center") |>
  opt_align_table_header(align = "left") |>
  cols_width(
    idx ~ px(50),
    l_time_domain ~ px(300),
    l_laplace_s_domain ~ px(600)
  ) |>
  opt_stylize(
    style = 2,
    color = "gray",
    add_row_striping = FALSE
  ) |>
  opt_table_outline(style = "invisible") %>%
  tab_style(
    style = cell_fill(color = "gray95"),
    locations = cells_body(columns = l_time_domain)
  ) |>
  tab_options(
    heading.title.font.size = px(32),
    heading.subtitle.font.size = px(18),
    heading.padding = px(0),
    footnotes.multiline = FALSE,
    column_labels.border.lr.style = "solid",
    column_labels.border.lr.width = px(1)
  )
man_fmt_markdown_2

A snapshot test was added to test the insertion of LaTeX formulas in more locations (e.g., footnotes, source notes, etc.). It also tests that multiple inline and display-mode formulas can be processed in a single piece of Markdown text.

Fixes: #375
Fixes: #616
Fixes: #1163

@rich-iannone rich-iannone marked this pull request as ready for review February 13, 2024 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant