termtex

package module
v0.0.0-...-9a8c79c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

README

termtex mascot

termtex

Go Reference test Go Report Card License

Render LaTeX math as Unicode text in the terminal.

$ termtex '\frac{-b \pm \sqrt{b^2 - 4ac}}{2a}'

-b ± √(b² - 4ac)
────────────────
       2a

Install

go install github.com/doug/termtex/cmd/termtex@latest

CLI Usage

# from argument
termtex '\sum_{i=1}^{n} i^2'

# from stdin
echo 'e^{i\pi} + 1 = 0' | termtex

# with color and italic Unicode
termtex -color -italic '\int_{0}^{\infty} e^{-x^2} dx'

# pure 7-bit ASCII output (for code comments, CI logs, terminals without good Unicode)
termtex -ascii '\frac{-b \pm \sqrt{b^2 - 4ac}}{2a}'

# markdown mode: rewrite $...$ and $$...$$ in a document and pipe into glow
cat doc.md | termtex -md | glow -

Run ./demo.sh for a full showcase.

Library Usage

import "github.com/doug/termtex"

// Simple render (Style{} is the package default)
out, err := termtex.Render(`\frac{1}{2}`, termtex.Style{})

// With style options
out, err := termtex.Render(`\frac{1}{2}`, termtex.Style{
    Color:  true,
    Italic: true,
})

// Pure ASCII output
out, err := termtex.Render(`\sqrt{\pi}`, termtex.Style{ASCII: true})
Markdown Integration

termtex can expand $...$ (inline) and $$...$$ (display) math in markdown, making it easy to integrate with terminal markdown renderers like glamour. This is the recommended path for glamour users — glamour doesn't expose a goldmark-extension hook, so preprocessing is the only way to get math into its pipeline.

// Expand math delimiters, then pass to glamour.
processed := termtex.Expand(markdownString, termtex.Style{})
out, _ := glamour.Render(processed, "auto")

See example/main.go for the full glamour pipeline.

Goldmark Extension

If you're building your own terminal renderer on top of goldmark (rather than using glamour), the termtex/goldmark subpackage provides a goldmark Extender that adds MathInline and MathBlock AST nodes plus a renderer that calls termtex.

import (
    "github.com/yuin/goldmark"
    ttgoldmark "github.com/doug/termtex/goldmark"
)

md := goldmark.New(
    goldmark.WithExtensions(ttgoldmark.NewMathExtension()),
)

Pair it with your own renderer.Renderer to control the surrounding markdown styling. (Goldmark's default renderer emits HTML, which mixes oddly with terminal-Unicode math — use this path for terminal pipelines where you control the renderer, not for HTML output. For HTML math, use KaTeX or MathJax client-side.)

Supported LaTeX

Category Commands
Fractions \frac{a}{b}
Superscripts / Subscripts x^2, x_{i}, x_{i}^{2}
Square roots \sqrt{x}, \sqrt[3]{x}
Big operators \sum, \prod, \int, \oint with limits
Limits \lim_{x \to 0}
Greek letters \alpha through \omega, \Gamma through \Omega
Math fonts \mathbb (ℕℤℚℝℂ), \mathcal (ℒℳℛ), \mathbf (𝐱), \mathfrak (𝔤), \mathsf (𝖠), \mathit
Delimiters \left( \right), \left[ \right], \left\{ \right\}
Matrices pmatrix, bmatrix, vmatrix, Bmatrix, Vmatrix
Accents \hat, \bar, \vec, \dot, \ddot, \tilde, \overline, \underline
Wide accents \widehat, \widetilde
Annotations \overbrace{X}^{label}, \underbrace{X}_{label}
Operators \pm, \times, \div, \cdot, \leq, \geq, \neq, \approx, \equiv
Arrows \to, \rightarrow, \leftarrow, \Rightarrow, \Leftarrow
Sets \in, \notin, \subset, \subseteq, \cup, \cap
Logic \forall, \exists
Calculus \partial, \nabla, \infty, \hbar
Brackets \langle, \rangle, \mid
Functions \sin, \cos, \log, \ln, \exp, \det, \max, \min, ...
Text \text{...}
Spacing \,, \;, \!, \quad, \qquad
Dots \ldots, \cdots, \vdots, \ddots

100 famous equations rendered by termtex. This section is auto-generated by demo.sh.

Foundations

1. Euler's Identity e^{i\pi} + 1 = 0

 iπ
e   + 1 = 0

2. Pythagorean Theorem a^2 + b^2 = c^2

a² + b² = c²

3. Quadratic Formula \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}

-b ± √(b² - 4ac)
────────────────
       2a

4. Binomial Theorem (x + y)^n = \sum_{k=0}^{n} \frac{n!}{k!(n-k)!} x^k y^{n-k}

            n      n!
(x + y)ⁿ =  ∑  ──────────xᵏyⁿ⁻ᵏ
           k=0 k!(n - k)!

5. Power Rule \frac{d}{dx} x^n = nx^{n-1}

 d
────xⁿ = nxⁿ⁻¹
 dx

6. Definition of Derivative \frac{df}{dx} = \lim_{h \to 0} \frac{f(x+h) - f(x)}{h}

 df          f(x + h) - f(x)
──── = lim   ───────────────
 dx    h→0          h

7. Fundamental Theorem of Calculus \int_{a}^{b} f(x) dx = F(b) - F(a)

b
∫ f(x)dx = F(b) - F(a)
a

8. Chain Rule \frac{dy}{dx} = \frac{dy}{du} \cdot \frac{du}{dx}

 dy     dy   du
──── = ────·────
 dx     du   dx

9. Product Rule (fg)^{\prime} = f^{\prime}g + fg^{\prime}

(fg)′ = f′g + fg′

10. Integration by Parts \int u \, dv = uv - \int v \, du

∫u dv = uv - ∫v du
Algebra and Number Theory

11. Sum of Natural Numbers \sum_{i=1}^{n} i = \frac{n(n+1)}{2}

 n      n(n + 1)
 ∑  i = ────────
i=1        2

12. Sum of Squares \sum_{i=1}^{n} i^2 = \frac{n(n+1)(2n+1)}{6}

 n       n(n + 1)(2n + 1)
 ∑  i² = ────────────────
i=1             6

13. Geometric Series \sum_{k=0}^{n} r^k = \frac{1 - r^{n+1}}{1 - r}

 n       1 - rⁿ⁺¹
 ∑  rᵏ = ────────
k=0       1 - r

14. Infinite Geometric Series \sum_{k=0}^{\infty} r^k = \frac{1}{1 - r}

 ∞         1
 ∑  rᵏ = ─────
k=0      1 - r

15. Difference of Squares a^2 - b^2 = (a + b)(a - b)

a² - b² = (a + b)(a - b)

16. Cubic Formula (Depressed) x = \sqrt[3]{-\frac{q}{2} + \sqrt{\frac{q^2}{4} + \frac{p^3}{27}}} + \sqrt[3]{-\frac{q}{2} - \sqrt{\frac{q^2}{4} + \frac{p^3}{27}}}

      ⎛   q     ⎛ q²     p³ ⎞⎞     ⎛   q     ⎛ q²     p³ ⎞⎞
x = ³√⎜- ─── + √⎜──── + ────⎟⎟ + ³√⎜- ─── - √⎜──── + ────⎟⎟
      ⎝   2     ⎝ 4      27 ⎠⎠     ⎝   2     ⎝ 4      27 ⎠⎠

17. Fraction Addition \frac{a}{b} + \frac{c}{d} = \frac{ad + bc}{bd}

 a     c    ad + bc
─── + ─── = ───────
 b     d      bd

18. Logarithm Change of Base \log_a b = \frac{\ln b}{\ln a}

         ln b
logₐ b = ────
         ln a

19. Exponential-Log Inverse e^{\ln x} = x

 ln x
e     = x

20. Euler's Totient Product \phi(n) = n \prod_{p | n} \left(1 - \frac{1}{p}\right)

            ⎛     1 ⎞
φ(n) = n ∏  ⎜1 - ───⎟
        p|n ⎝     p ⎠
Calculus and Analysis

21. Gaussian Integral \int_{0}^{\infty} e^{-x^2} dx = \frac{\sqrt{\pi}}{2}

∞          √(π)
∫ e⁻ˣ²dx = ────
0           2

22. Taylor Series f(x) = \sum_{n=0}^{\infty} \frac{f^{(n)}(a)}{n!} (x - a)^n

            (n)
        ∞  f   (a)
f(x) =  ∑  ───────(x - a)ⁿ
       n=0   n!

23. Maclaurin Series for e^x e^x = \sum_{n=0}^{\infty} \frac{x^n}{n!}

      ∞   xⁿ
eˣ =  ∑  ────
     n=0  n!

24. Basel Problem \sum_{n=1}^{\infty} \frac{1}{n^2} = \frac{\pi^2}{6}

 ∞   1      π²
 ∑  ──── = ────
n=1  n²     6

25. Leibniz Formula for Pi \frac{\pi}{4} = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \cdots

 π         1     1     1
─── = 1 - ─── + ─── - ─── + ⋯
 4         3     5     7

26. Cauchy-Schwarz Inequality \left(\sum a_i b_i\right)^2 \leq \left(\sum a_i^2\right)\left(\sum b_i^2\right)

(∑aᵢbᵢ)² ≤ (∑a²ᵢ)(∑b²ᵢ)

27. Mean Value Theorem f(b) - f(a) = f^{\prime}(c)(b - a)

f(b) - f(a) = f′(c)(b - a)

28. L'Hopital's Rule \lim_{x \to c} \frac{f(x)}{g(x)} = \lim_{x \to c} \frac{f^{\prime}(x)}{g^{\prime}(x)}

      f(x)         f′(x)
lim   ──── = lim   ─────
x→c   g(x)   x→c   g′(x)

29. Euler-Mascheroni Constant \gamma = \lim_{n \to \infty} \left(\sum_{k=1}^{n} \frac{1}{k} - \ln n\right)

          ⎛ n   1        ⎞
γ = lim   ⎜ ∑  ─── - ln n⎟
    n→∞   ⎝k=1  k        ⎠

30. Stirling's Approximation n! \approx \sqrt{2\pi n} \left(\frac{n}{e}\right)^n

                n
           ⎛ n ⎞
n! ≈ √(2πn)⎜───⎟
           ⎝ e ⎠
Trigonometry

31. Pythagorean Identity \sin^2 \theta + \cos^2 \theta = 1

sin² θ + cos² θ = 1

32. Sine Addition \sin(\alpha + \beta) = \sin \alpha \cos \beta + \cos \alpha \sin \beta

sin (α + β) = sin α cos β + cos α sin β

33. Cosine Addition \cos(\alpha + \beta) = \cos \alpha \cos \beta - \sin \alpha \sin \beta

cos (α + β) = cos α cos β - sin α sin β

34. Euler's Formula e^{i\theta} = \cos \theta + i \sin \theta

eⁱᶿ = cos θ + i sin θ

35. Double Angle Sine \sin 2\theta = 2 \sin \theta \cos \theta

sin 2θ = 2 sin θ cos θ

36. Double Angle Cosine \cos 2\theta = \cos^2 \theta - \sin^2 \theta

cos 2θ = cos² θ - sin² θ

37. Tangent Definition \tan \theta = \frac{\sin \theta}{\cos \theta}

        sin θ
tan θ = ─────
        cos θ

38. Law of Sines \frac{a}{\sin A} = \frac{b}{\sin B} = \frac{c}{\sin C}

  a       b       c
───── = ───── = ─────
sin A   sin B   sin C

39. Law of Cosines c^2 = a^2 + b^2 - 2ab \cos C

c² = a² + b² - 2ab cos C

40. Half-Angle Formula \sin \frac{\theta}{2} = \pm \sqrt{\frac{1 - \cos \theta}{2}}

     θ      ⎛1 - cos θ⎞
sin ─── = ±√⎜─────────⎟
     2      ⎝    2    ⎠
Linear Algebra

41. 2x2 Determinant \det \begin{bmatrix} a & b \\ c & d \end{bmatrix} = ad - bc

    ⎡ a  b ⎤
det ⎣ c  d ⎦ = ad - bc

42. Kronecker Delta \delta_{ij} = \begin{cases} 1 & \text{if } i = j \\ 0 & \text{if } i \neq j \end{cases}

      ⎧ 1  if  i = j
δᵢⱼ = ⎩ 0  if  i ≠ j

43. Matrix Inverse (2x2) A^{-1} = \frac{1}{ad - bc} \begin{bmatrix} d & -b \\ -c & a \end{bmatrix}

         1   ⎡ d   -b ⎤
A⁻¹ = ───────⎣ -c  a  ⎦
      ad - bc

44. Eigenvalue Equation Av = \lambda v

Av = λv

45. Characteristic Polynomial \det(A - \lambda I) = 0

det (A - λI) = 0

46. Dot Product a \cdot b = \sum_{i=1}^{n} a_i b_i

       n
a·b =  ∑  aᵢbᵢ
      i=1

47. Cross Product a \times b = \begin{vmatrix} i & j & k \\ a_1 & a_2 & a_3 \\ b_1 & b_2 & b_3 \end{vmatrix}

        │ i   j   k  │
a × b = │ a₁  a₂  a₃ │
        │ b₁  b₂  b₃ │

48. Matrix Transpose (AB)^T = B^T A^T

(AB)ᵀ = BᵀAᵀ

49. Trace \text{tr}(A) = \sum_{i=1}^{n} a_{ii}

          n
tr (A) =  ∑  aᵢᵢ
         i=1

50. Rotation Matrix R(\theta) = \begin{bmatrix} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{bmatrix}

       ⎡ cos θ  -sin θ ⎤
R(θ) = ⎣ sin θ  cos θ  ⎦
Classical Mechanics

51. Newton's Second Law F = ma

F = ma

52. E = mc^2 E = mc^2

E = mc²

53. Kinetic Energy E_k = \frac{1}{2}mv^2

      1
Eₖ = ───mv²
      2

54. Gravitational Force F = G\frac{m_1 m_2}{r^2}

     m₁m₂
F = G────
      r²

55. Gravitational Potential Energy U = -\frac{Gm_1 m_2}{r}

      Gm₁m₂
U = - ─────
        r

56. Escape Velocity v_e = \sqrt{\frac{2GM}{r}}

      ⎛2GM⎞
vₑ = √⎜───⎟
      ⎝ r ⎠

57. Simple Harmonic Motion x(t) = A\cos(\omega t + \phi)

x(t) = A cos (ωt + φ)

58. Euler-Lagrange Equation \frac{d}{dt}\frac{\partial L}{\partial v} - \frac{\partial L}{\partial q} = 0

 d   ∂L     ∂L
──────── - ──── = 0
 dt  ∂v     ∂q

59. Hamilton's Equation \frac{dq}{dt} = \frac{\partial H}{\partial p}

 dq     ∂H
──── = ────
 dt     ∂p

60. Centripetal Acceleration a = \frac{v^2}{r}

     v²
a = ────
     r
Electromagnetism

61. Gauss's Law \nabla \cdot E = \frac{\rho}{\epsilon_0}

       ρ
∇·E = ────
       ε₀

62. Gauss's Law (Magnetism) \nabla \cdot B = 0

∇·B = 0

63. Faraday's Law \nabla \times E = -\frac{\partial B}{\partial t}

           ∂B
∇ × E = - ────
           ∂t

64. Ampere's Law \nabla \times B = \mu_0 J + \mu_0 \epsilon_0 \frac{\partial E}{\partial t}

                   ∂E
∇ × B = μ₀J + μ₀ε₀────
                   ∂t

65. Coulomb's Law F = \frac{1}{4\pi\epsilon_0} \cdot \frac{q_1 q_2}{r^2}

     1   q₁q₂
F = ────·────
    4πε₀  r²

66. Lorentz Force F = q(E + v \times B)

F = q(E + v × B)

67. Ohm's Law V = IR

V = IR

68. Capacitor Energy E = \frac{1}{2}CV^2

     1
E = ───CV²
     2

69. Biot-Savart Law dB = \frac{\mu_0}{4\pi} \frac{I \, dl \times \hat{r}}{r^2}

      μ₀ I dl × r̂
dB = ────────────
      4π    r²

70. Poynting Vector S = \frac{1}{\mu_0} E \times B

     1
S = ────E × B
     μ₀
Quantum Mechanics and Relativity

71. Schrodinger Equation i\hbar\frac{\partial}{\partial t}\Psi = \hat{H}\Psi

   ∂
iℏ────Ψ = ĤΨ
   ∂t

72. Heisenberg Uncertainty \Delta x \, \Delta p \geq \frac{\hbar}{2}

         ℏ
Δx Δp ≥ ───
         2

73. de Broglie Wavelength \lambda = \frac{h}{p}

     h
λ = ───
     p

74. Planck-Einstein Relation E = h\nu

E = hν

75. Time Dilation \Delta t^{\prime} = \frac{\Delta t}{\sqrt{1 - \frac{v^2}{c^2}}}

          Δt
Δt′ = ───────────
       ⎛     v² ⎞
      √⎜1 - ────⎟
       ⎝     c² ⎠

76. Length Contraction L = L_0 \sqrt{1 - \frac{v^2}{c^2}}

       ⎛     v² ⎞
L = L₀√⎜1 - ────⎟
       ⎝     c² ⎠

77. Relativistic Energy-Momentum E^2 = (pc)^2 + (mc^2)^2

E² = (pc)² + (mc²)²

78. Schwarzschild Radius r_s = \frac{2GM}{c^2}

     2GM
rₛ = ───
     c²

79. Photoelectric Effect E_k = h\nu - \phi

Eₖ = hν - φ

80. Rydberg Formula \frac{1}{\lambda} = R\left(\frac{1}{{n_1}^2} - \frac{1}{{n_2}^2}\right)

 1     ⎛ 1     1 ⎞
─── = R⎜─── - ───⎟
 λ     ⎝n₁²   n₂²⎠
Thermodynamics

81. Ideal Gas Law PV = nRT

PV = nRT

82. Boltzmann Entropy S = k_B \ln \Omega

S = k  ln Ω
     B

83. First Law of Thermodynamics \Delta U = Q - W

ΔU = Q - W

84. Carnot Efficiency \eta = 1 - \frac{T_c}{T_h}

         T
          c
η = 1 - ────
         T
          h

85. Stefan-Boltzmann Law P = \sigma A T^4

P = σAT⁴

86. Maxwell-Boltzmann Distribution f(v) = 4\pi n \left(\frac{m}{2\pi kT}\right)^{3/2} v^2 e^{-mv^2/2kT}

                3/2      2
          ⎛ m  ⎞    2 -mv /2kT
f(v) = 4πn⎜────⎟   v e
          ⎝2πkT⎠

87. Planck's Law B(\nu) = \frac{2h\nu^3}{c^2} \cdot \frac{1}{e^{h\nu/kT} - 1}

          3
       2hν      1
B(ν) = ────·──────────
         2   hν/kT
        c   e      - 1

88. Gibbs Free Energy G = H - TS

G = H - TS

89. Clausius Inequality \oint \frac{dQ}{T} \leq 0

  dQ
∮──── ≤ 0
  T

90. Equipartition Theorem \langle E \rangle = \frac{f}{2} k_B T

       f
⟨E⟩ = ───k T
       2  B
Probability and Information

91. Bayes' Theorem P(A \mid B) = \frac{P(B \mid A) \, P(A)}{P(B)}

         P(B|A) P(A)
P(A|B) = ───────────
            P(B)

92. Normal Distribution f(x) = \frac{1}{\sigma\sqrt{2\pi}} e^{-\frac{(x - \mu)^2}{2\sigma^2}}

                (x-μ)²
              - ──────
         1       2σ²
f(x) = ──────e
       σ√(2π)

93. Expected Value E[X] = \sum_{i} x_i \, P(x_i)

E[X] = ∑ xᵢ P(xᵢ)
       i

94. Variance \text{Var}(X) = E[X^2] - (E[X])^2

Var (X) = E[X²] - (E[X])²

95. Shannon Entropy H = -\sum_{i} p_i \log_2 p_i

H = -∑ pᵢ log₂ pᵢ
     i

96. Bernoulli Trial P(k) = \frac{n!}{k!(n-k)!} p^k (1-p)^{n-k}

           n!
P(k) = ──────────pᵏ(1 - p)ⁿ⁻ᵏ
       k!(n - k)!
Famous Constants and Identities

97. Golden Ratio \phi = \frac{1 + \sqrt{5}}{2}

    1 + √(5)
φ = ────────
       2

98. Euler Product (Riemann Zeta) \zeta(s) = \sum_{n=1}^{\infty} \frac{1}{n^s} = \prod_{p} \frac{1}{1 - p^{-s}}

        ∞   1          1
ζ(s) =  ∑  ──── = ∏ ───────
       n=1  nˢ    p 1 - p⁻ˢ

99. Wallis Product \frac{\pi}{2} = \prod_{n=1}^{\infty} \frac{4n^2}{4n^2 - 1}

 π     ∞    4n²
─── =  ∏  ───────
 2    n=1 4n² - 1

100. Euler's Reflection Formula \Gamma(z)\Gamma(1-z) = \frac{\pi}{\sin(\pi z)}

                  π
Γ(z)Γ(1 - z) = ────────
               sin (πz)

Documentation

Overview

Package termtex renders LaTeX math expressions as Unicode text suitable for terminal display.

It parses a subset of LaTeX math syntax and typesets it on a character grid using Unicode box-drawing characters, mathematical symbols, and (optionally) italic letter forms or ANSI color.

Quick start

out, err := termtex.Render(`\frac{-b \pm \sqrt{b^2 - 4ac}}{2a}`, termtex.Style{})
fmt.Println(out)

Style

Style toggles italic letters, ANSI color, and a strict 7-bit ASCII fallback for environments without full Unicode support. Its zero value is the package default (plain Unicode, no italic, no color).

out, err := termtex.Render(input, termtex.Style{
    Italic: true,
    Color:  true,
})

Markdown integration

Expand rewrites $...$ and $$...$$ in a markdown string to pre-rendered termtex output. The result feeds cleanly into terminal markdown renderers like glamour. For custom goldmark pipelines, see the goldmark subpackage.

Supported LaTeX

Fractions, super/subscripts, square and nth roots, big operators (\sum, \prod, \int, \oint, \lim), Greek letters, math fonts (\mathbb, \mathcal, \mathbf, \mathfrak, \mathsf, \mathit), tall delimiters, matrix environments, accents (\hat, \tilde, \dot, \ddot, \vec) using combining marks, \overbrace / \underbrace, and the common operator and arrow set. See README.md for the full table.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Expand

func Expand(md string, style Style) string

Expand scans markdown text for math delimiters and replaces them with termtex-rendered output. Display math ($$...$$) becomes fenced code blocks that glamour preserves verbatim. Inline math ($...$) is rendered inline. Pass Style{} for the package default.

The result can be passed to glamour or any other terminal markdown renderer.

Within a single Expand call, the same math expression is only rendered once — repeated occurrences hit a per-call cache. This matters in practice: a typical AI-conversation transcript or paper uses the same handful of expressions (\alpha, x_i, f(x), ...) many times.

func Render

func Render(input string, style Style) (string, error)

Render parses a LaTeX math string and returns a multi-line Unicode string suitable for terminal display. Pass Style{} for the package default.

Returns an error if the input is malformed.

Types

type Style

type Style struct {
	// Italic uses the Mathematical Italic Unicode block (U+1D434…) for
	// variable letters. Requires a font with coverage of that range —
	// most stock monospace fonts do not include it.
	Italic bool
	// Color emits ANSI 24-bit color escapes for variables, numbers,
	// operators, delimiters, and big operators.
	Color bool
	// ASCII restricts output to 7-bit ASCII, falling back to plain
	// characters for box drawing, fraction bars, sqrt, accents, etc.
	// Useful for code comments, CI logs, or terminals lacking Unicode.
	ASCII bool
}

Style controls rendering options. The zero value is the package default: plain Unicode, no italic, no color.

Directories

Path Synopsis
cmd
termtex command
Package goldmark provides a goldmark extension for rendering LaTeX math in the terminal using termtex.
Package goldmark provides a goldmark extension for rendering LaTeX math in the terminal using termtex.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL