Skip to content

Instantly share code, notes, and snippets.

@rgchris
Last active August 15, 2026 23:20
Show Gist options
  • Select an option

  • Save rgchris/d7b31eee47efc6c86d87fcc21d6b45a4 to your computer and use it in GitHub Desktop.

Select an option

Save rgchris/d7b31eee47efc6c86d87fcc21d6b45a4 to your computer and use it in GitHub Desktop.
LaTeX Equation Notation Generator

This is something of a noodling around that may be of use/interest to someone. It loosely uses Rebol's operator semantics to somewhat intuitively generate LaTeX equation notation. It is by no means comprehensive, I had a use for it and it serves that purpose.

Of interest is the redefinition of math operators as they are known in Rebol and while they are still largely semantically compatible with their common function, they accumulate values as LaTeX notation instead of evaluating them.

Note the handling of blocks and groups (parens). To create intentionally parenthesized content, blocks are required; to manage Rebol's semantics without creating parenthesized content, use groups (parens).

e ^ 1 + x    ; => {e}^{1}+{x}
e ^ (1 + x)  ; => {e}^{1+{x}}
e ^ [1 + x]  ; => {e}^{\left(1+{x}\right)}

To view the resultant equations, just drop them into a LaTeX document and compile (a quick way to do this without complex installation is within an online editor such as Overleaf).

Sample output (from test file below):

Sample Output from the Rebol LaTeX Notation Generator
Rebol [
Title: "LaTeX Equation Notation Generator"
Author: "Christopher Ross-Gill"
Date: 14-Aug-2026
Type: module
Name: rgchris.latex-math
Exports: [
latex-math
]
]
latex-math: context [
echo: func [
out [string!]
value [scalar!]
][
append out value
]
prep: func [
value [block! paren! scalar! word!]
/soft
][
switch/default type-of value [
#(paren!) [
to block! value
]
#(block!) [
; either soft [
; to block! expand value
; ][
compose [
"\left("
(to block! expand value)
"\right)"
]
; ]
]
#(word!) [
rejoin [
#"{" form value #"}"
]
]
#(none!) [
copy []
]
][
value
]
]
squeeze: func [
spec [block!]
][
to paren! compose spec
]
api: context [
sup: func [
value [block! paren! scalar! word!]
][
squeeze [
"^^{" (prep value) "}"
]
]
sub: func [
value [block! paren! scalar! word!]
][
squeeze [
"_{" (prep value) "}"
]
]
bar: func [
value [block! paren! scalar! word!]
][
squeeze [
"_{" (prep value) "}"
]
]
enclose: func [
value [none! block! paren! scalar! word!]
/square /curly
][
case [
square [
squeeze [
"\left["
(prep value)
"\right]"
]
]
curly [
squeeze [
"\left{"
(prep value)
"\right}"
]
]
/else [
squeeze [
"\left("
(prep value)
"\right)"
]
]
]
]
concat: func [
value1 [paren! block! scalar! word!]
value2 [paren! block! scalar! word!]
][
squeeze [
(prep value1) (prep value2)
]
]
equal: func [
value1 [block! paren! scalar! word!]
value2 [block! paren! scalar! word!]
][
squeeze [
(prep value1) "=" (prep value2)
]
]
not-equal: func [
value1 [block! paren! scalar! word!]
value2 [block! paren! scalar! word!]
][
squeeze [
(prep value1) "\neq" (prep value2)
]
]
approx: func [
value1 [block! paren! scalar! word!]
value2 [block! paren! scalar! word!]
][
squeeze [
(prep value1) "\approx" (prep value2)
]
]
sum: func [
upper [none! block! paren! scalar! word!]
lower [none! block! paren! scalar! word!]
value [block! paren! scalar! word!]
][
squeeze [
"\sum"
(
either none? upper [
upper: []
][
upper: to-block sup upper
]
)
(
either none? lower [
lower: []
][
lower: to-block sub lower
]
)
(prep value)
]
]
product: func [
upper [none! block! paren! scalar! word!]
lower [none! block! paren! scalar! word!]
value [block! paren! scalar! word!]
][
squeeze [
"\prod"
(
either none? upper [
upper: []
][
upper: to-block sup upper
]
)
(
either none? lower [
lower: []
][
lower: to-block sub lower
]
)
(prep value)
]
]
integrate: func [
upper [none! block! paren! scalar! word!]
lower [none! block! paren! scalar! word!]
value [block! paren! scalar! word!]
][
squeeze [
"\int"
(
either none? upper [
upper: []
][
upper: to-block sup upper
]
)
(
either none? lower [
lower: []
][
lower: to-block sub lower
]
)
(prep value)
]
]
limit: func [
variable [word!]
value [block! paren! scalar! word!]
expression [block! paren! scalar! word!]
/left /right
][
squeeze [
"\lim_{"
(prep variable) "\to" (prep value)
(case [left ["^^-"] right ["^^+"] /else ""])
"}" (prep expression)
]
]
root: func [
base [none! block! paren! scalar! word!]
value [block! paren! scalar! word!]
][
either base [
squeeze [
"\sqrt[" (prep base) "]{" (prep value) "}"
]
][
squeeze [
"\sqrt{" (prep value) "}"
]
]
]
add: func [
value1 [block! paren! scalar! word!]
value2 [block! paren! scalar! word!]
][
squeeze [
(prep value1) "+" (prep value2)
]
]
subtract: func [
value1 [block! paren! scalar! word!]
value2 [block! paren! scalar! word!]
][
squeeze [
(prep value1) "-" (prep value2)
]
]
multiply: func [
value1 [block! paren! scalar! word!]
value2 [block! paren! scalar! word!]
][
squeeze [
(prep value1) "\times" (prep value2)
]
]
divide: func [
value1 [block! paren! scalar! word!]
value2 [block! paren! scalar! word!]
][
squeeze [
(prep value1) "\div" (prep value2)
]
]
over: func [
value1 [block! paren! scalar! word!]
value2 [block! paren! scalar! word!]
][
squeeze [
"\frac{" (prep value1) "}{" (prep value2) "}"
]
]
power-of: func [
value1 [block! paren! scalar! word!]
value2 [block! paren! scalar! word!]
][
squeeze [
(prep value1) "^^{" (prep value2) "}"
]
]
ln: func [
value [paren! block! scalar! word!]
][
squeeze [
"\ln" (prep value)
]
]
negate: func [
value [paren! block! scalar! word!]
][
squeeze [
#"-" (prep value)
]
]
abs: func [
value [paren! block! scalar! word!]
][
squeeze [
#"|" (prep value) #"|"
]
]
+-: func [
value [paren! block! scalar! word!]
][
squeeze [
"\pm" (prep value)
]
]
=: make op! func [
value1 [paren! block! scalar! word!]
value2 [paren! block! scalar! word!]
][
equal value1 value2
]
<>: make op! func [
value1 [paren! block! scalar! word!]
value2 [paren! block! scalar! word!]
][
not-equal value1 value2
]
=~: make op! func [
value1 [paren! block! scalar! word!]
value2 [paren! block! scalar! word!]
][
approx value1 value2
]
+: make op! func [
value1 [paren! block! scalar! word!]
value2 [paren! block! scalar! word!]
][
add value1 value2
]
-: make op! func [
value1 [paren! block! scalar! word!]
value2 [paren! block! scalar! word!]
][
subtract value1 value2
]
*: make op! func [
value1 [paren! block! scalar! word!]
value2 [paren! block! scalar! word!]
][
multiply value1 value2
]
/: make op! func [
value1 [paren! block! scalar! word!]
value2 [paren! block! scalar! word!]
][
over value1 value2
]
by: make op! func [
value1 [paren! block! scalar! word!]
value2 [paren! block! scalar! word!]
][
divide value1 value2
]
^: make op! func [
value1 [paren! block! scalar! word!]
value2 [paren! block! scalar! word!]
][
power-of value1 value2
]
.: make op! func [
value1 [paren! block! scalar! word!]
value2 [paren! block! scalar! word!]
][
concat value1 value2
]
a: 'a
b: 'b
c: 'c
d: 'd
e: 'e
f: 'f
g: 'g
h: 'h
i: 'i
j: 'j
k: 'k
l: 'l
m: 'm
n: 'n
o: 'o
p: 'p
q: 'q
r: 'r
s: 's
t: 't
u: 'u
v: 'v
w: 'w
x: 'x
y: 'y
z: 'z
dp: 'dp
dq: 'dq
dt: 'dt
du: 'du
dx: 'dx
dy: 'dy
pi: quote ("\pi")
sigma: quote ("\sigma")
theta: quote ("\theta")
uc: #[
a A
b B
c C
d D
e E
f F
g G
h H
i I
j J
k K
l L
m M
n N
o O
p P
q Q
r R
s S
t T
u U
v V
w W
x X
y Y
z Z
dp dP
dq dQ
dt dT
du dU
dx dX
dy dY
pi ("\Pi")
sigma ("\Sigma")
theta ("\Theta")
]
infinity: quote ("\infty")
...: quote ("\cdots")
|: quote ("{,\ }")
newline: lf: quote ("{\\}")
]
expand: func [
spec [block!]
/local
][
collect [
while [
not tail? spec
][
value: do/next spec 'spec
if :value [
keep value
]
]
]
]
render: func [
spec [block!]
/inline
][
bind spec api
either inline [
rejoin to block! compose [
#"$" (to block! expand spec) #"$"
]
][
rejoin to block! compose [
"\[" (to block! expand spec) "\]"
]
]
]
]
Rebol [
Title: "LaTeX Equation Notation Creator Samples"
Needs: %latex-math.reb
]
x: 123
ans: pi
print latex-math/render [
sum infinity (t = 1) x ^ t = (1 / (1 - x))
]
print latex-math/render [
pi =~ ans
|
enclose/square (sum _ _ 1 + 2)
|
sum n (i = 1) 3 + 4
|
sum n _ 3 + 4
|
sum _ (i = 1) 3 + 4
|
integrate _ _ 3 + 4 . x = 3 x + 4 . x ^ 2 + uc/c
]
print latex-math/render [
[1 / 3 * 2 by 3 * 5 . 'x] / [10 * [20 + 'a] ^ (5 . 'x)] + 100
]
print latex-math/render [
x + negate ln [3 * (4 / x) + 6 ^ (7 + 8)]
]
print latex-math/render [
enclose (pi . lf . pi)
|
'a = pi . r ^ 2
|
(root 3 (x + 2.15))
|
root _ (x + 5)
|
(dx / dy) + (dx / dt) = 100
]
print latex-math/render [
negate uc/a / (uc/a ^ 2) = negate (1 / uc/a) | limit/left x infinity (x + 1)
]
print latex-math/render [
(d / dx) . (root _ x) = (1 / (2 . root _ x))
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment