Skip to content

About

Quarto extension: colour matching brackets by nesting depth in HTML math and Lisp-family code

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

10 Commits

Folders and files

Repository files navigation

rainbow-parens

A Quarto extension that colors each pair of matching brackets by its nesting depth in HTML output, so a reader can see at a glance which ( closes which ). It colors brackets in math and in code blocks of every language Quarto highlights (R, Python, Racket and the rest). PDF and other non-HTML output is unchanged. It works in revealjs slides as well as HTML pages (with Quarto 1.11; see Limits).

Installation

quarto add Morrison-Lab/rainbow-parens

This installs the extension in _extensions/Morrison-Lab/rainbow-parens/.

Usage

Add the filter to the document or project metadata:

filters:
  - Morrison-Lab/rainbow-parens

That colors brackets in all math and all highlighted code. The optional settings narrow it:

rainbow-parens:
  math: true          # color brackets in math (default true)
  code: [r, racket]   # only these code languages; true (default) for all, false for none
  innermost: plain    # plain (default): a pair holding no other pair keeps the text color; color: color it too

rainbow-parens: false turns the extension off for one page. A value that is none of these stops the render with an error.

What gets colored

  • Math. The round, square and curly brackets in MathJax math, including stretchy ones from \left( ... \right). An opener and its closer share a color even when their shapes differ, so the interval $[0, 1)$ pairs its [ with its ). Brackets inside an explicit \color{...} keep the author's color.
  • Code. The same brackets in highlighted code blocks, except inside strings, comments and character literals. Plain output blocks, whose brackets need not balance, are left alone.

A closer with no opener before it keeps the text color. By default an innermost pair, one that holds no other pair, keeps the text color too, so in f(g(h(x))) the parentheses of h(x) are plain, a lone (x) is plain, and only the pairs that hold another pair are colored. Colors still count depth from the outside, so the outermost pair is always the first color. Set innermost: color to color every pair. After the fourth level the colors start again.

Colors

Each theme has its own palette of four colors, one per nesting level. Both palettes were checked on every pair of colors, not only neighbors:

Level Light theme Dark theme
1 #2757a8 blue #60a4ff blue
2 #84550f brown #ebb678 sand
3 #aa0970 magenta #fd7497 pink
4 #8e2dee violet #4cd9d2 aqua
  • Color vision deficiency. The worst pair under simulated protanopia or deuteranopia (Machado et al. 2009) is OKLab ΔE 10.6 (light) and 9.0 (dark), above the target of 8.
  • Normal vision. The closest pair is ΔE 19.5 (light) and 17.4 (dark), above the floor of 15.
  • Contrast. Every color reaches the WCAG text contrast of 4.5:1 on each background it is meant for: white, the off-white #f5f2ea and parchment #f1e8d0 page tints and the code backgrounds #f1f3f5, #f8f9fa, #f8f6f0 and #faf5e8 for the light palette (4.57 to 7.04), and Bootstrap's darkly #222222, the code backgrounds #303030 and #373737, and the slide backgrounds #191919 and #1c1e20 for the dark palette (4.60 to 10.18). These are the backgrounds the lds site and decks use in each of their themes, measured on the rendered pages.

The dark palette applies under Quarto's dark theme (body.quarto-dark) and the dark slide theme of the lab's qwt template (body.qwt-dark). On any other page, the script checks the page background whenever an attribute of the body or root changes (a class, or an attribute such as the data-palette that a slide theme picker sets), and adds rp-dark, which also selects the dark palette, while the background is dark (relative luminance below 0.179); so a site with a single dark theme such as theme: darkly gets the dark palette too. While the background is light it adds rp-light, which selects the light palette even under a dark-theme class, as on a deck whose theme picker switches to a light palette and leaves body.quarto-dark in place. Override a color by setting --rp-1 to --rp-4 in your own stylesheet. In print and in the forced-colors (high-contrast) mode, brackets keep the text color.

How it works

The Lua filter adds a script and a stylesheet to HTML pages, and a <meta> tag carrying the settings. The script registers a MathJax render action that runs after MathJax parses the TeX and before it draws the output. It walks the parsed math in order, keeps a stack of open brackets, and adds a class rp-d1 to rp-d4 to each bracket. MathJax copies that class onto the element it draws, so the stylesheet can switch colors with the theme. For code, the script wraps each bracket in a <span> with the same class once the page has loaded.

The script must run before MathJax loads, which Quarto's HTML template arranges: extension scripts come before its deferred MathJax script. A page that later replaces window.MathJax with its own configuration object would drop the render action.

Limits

  • MathJax 4 only. Quarto 1.11.9 loads MathJax 4 on HTML pages and in revealjs slides, so slide math is colored too. Older Quarto releases typeset slide math with MathJax 2.7.9, which has no render actions, and there brackets in slide math keep the text color. On a page that loads MathJax 2 or 3 itself, or KaTeX, math is not colored.
  • Unmatched brackets. A closer with no opener keeps the text color. An opener with no closer, such as the [ of the French interval ]0, 1[, leaves every later bracket in that formula or code block one level deeper. Brackets inside a token that Pandoc's highlighter does not class as a string, comment or character still count, such as the ( in the Racket symbol |a(b|.

See example.qmd for a demonstration. It names the filter as rainbow-parens, because it uses this repository's own _extensions/rainbow-parens/ copy; a document in another repository names it Morrison-Lab/rainbow-parens.

License

MIT; see LICENSE. A copy sits in the extension folder, so it travels with quarto add.

About

Quarto extension: colour matching brackets by nesting depth in HTML math and Lisp-family code

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages

Generated from Morrison-Lab/qet