How to create a consistent `syntax highlight` in math-mode

I want to create a consistent way to colorize math symbols and operators, and (hopefully) create a package with such functionality.

#let color_palette = (
  greek: rgb("#cc517a"),
  letters: rgb("#3f83a6"),
  brackets: rgb("#2d539e"),
  overline: rgb("#668e3d"),
  operator: rgb("#c57339"),
  logic: rgb("#7759b4"),
  palette7: rgb("#33374c"),
  palette8: rgb("#8389a3"),
  integrals: rgb("#cc3768"),
  palette10: rgb("#598030"),
  palette11: rgb("#b6662d"),
  variables: rgb("#22478e"),
  palette13: rgb("#6845ad"),
  palette14: rgb("#327698"),
  palette15: rgb("#262a3f"),
)

I tried to

  • redefine existing functions:
    #let alpha = text(color_palette.greek, $alpha$)
    #let integral = text(color_palette.greek, $integral$)
    #let dif = text(color_palette.greek, $dif$)
    #let sup = $op(text(fill: #color_palette.operator, "sup"),   limits: #true)$
    
  • Use show:
    #show math.equation: it => {
      show sym.lt.eq: set text(color_palette.logic)
      show math.in: set text(color_palette.logic)
      show math.forall: set text(color_palette.logic)
      show sym.arrow.double.l.r.long: set   text(color_palette.logic)
      show math.lt: set text(color_palette.logic)
      show "t": set text(color_palette.letters)
      show "x": set text(color_palette.variables)
      show "y": set text(color_palette.variables)
      show "a": set text(color_palette.palette14)
      show "b": set text(color_palette.palette14)
      set overline(stroke: color_palette.overline)
      it
    }
    

which produces something like this

How to make max operator not accept the show a: ..., show x: ... etc.? And how to properly colorize symbols and operators without writing 50+ let statements?

Right now I can only think of a workaround for this:

#let color-palette = (
  greek: rgb("#cc517a"),
  letters: rgb("#3f83a6"),
  brackets: rgb("#2d539e"),
  overline: rgb("#668e3d"),
  operator: rgb("#c57339"),
  logic: rgb("#7759b4"),
  palette7: rgb("#33374c"),
  palette8: rgb("#8389a3"),
  integrals: rgb("#cc3768"),
  palette10: rgb("#598030"),
  palette11: rgb("#b6662d"),
  variables: rgb("#22478e"),
  palette13: rgb("#6845ad"),
  palette14: rgb("#327698"),
  palette15: rgb("#262a3f"),
)

#let alpha = text(color-palette.greek, $alpha$)
#let integral = text(color-palette.greek, $integral$)
#let dif = text(color-palette.greek, $dif$)
#let sup = $op(text(fill: #color-palette.operator, "sup"), limits: #true)$

#let single-var(v) = regex("\\b" + v + "\\b")

#show math.equation: it => {
  show sym.lt.eq: set text(color-palette.logic)
  show math.in: set text(color-palette.logic)
  show math.forall: set text(color-palette.logic)
  show sym.arrow.double.l.r.long: set text(color-palette.logic)
  show math.lt: set text(color-palette.logic)
  show single-var("t"): set text(color-palette.letters)
  show single-var("x"): set text(color-palette.variables)
  show single-var("y"): set text(color-palette.variables)
  show single-var("a"): set text(color-palette.palette14)
  show single-var("b"): set text(color-palette.palette14)
  set overline(stroke: color-palette.overline)
  it
}

$dif integral sup max |alpha x(t)|$

Нарешті нехай $y(t) in C^1 ([a, b])$ є довільною функцією, маємо

1 Like

Thank you, this will do for now.

1 Like