TexToUnicode
A Ruby gem that converts TeX mathematical and other symbols to their Unicode character equivalents, enabling easier display of mathematical notation in plain text.
Installation
Install the gem locally:
gem build tex_to_unicode.gemspec
gem install tex_to_unicode-0.1.0.gemOr add it to your Gemfile:
gem 'tex_to_unicode'Usage
Command Line
The gem provides a tex_to_unicode executable that converts TeX expressions passed as arguments:
# Basic usage
tex_to_unicode '\alpha + \beta = \gamma'
# Output: α + β = γ
# Greek letters
tex_to_unicode '\pi \approx 3.14159'
# Output: π ≈ 3.14159
# Math operators
tex_to_unicode 'x \times y \leq z'
# Output: x × y ≤ z
# Arrows
tex_to_unicode 'A \rightarrow B \Rightarrow C'
# Output: A → B ⇒ C
# Integrals and sums
tex_to_unicode '\int_0^\infty x^2 dx'
# Output: ∫₀∞ x² dx
# Set notation
tex_to_unicode 'x \in \mathbb{R}, y \notin \emptyset'
# Output: x ∈ ℝ, y ∉ ∅
# Complex expressions
tex_to_unicode '\forall x \exists y : x \leq y'
# Output: ∀ x ∃ y : x ≤ yRuby API
You can also use the gem in your Ruby code:
require 'tex_to_unicode'
# Convert TeX to Unicode
result = TexToUnicode.convert('\alpha + \beta = \gamma')
puts result # => α + β = γ
# Use in string interpolation
formula = '\sum_{i=1}^n i'
puts "The formula is: #{TexToUnicode.convert(formula)}"
# Output: The formula is: ∑i=1ⁿ iSupported Symbols
The gem supports a wide range of TeX symbols including:
Greek Letters
- Lowercase:
\alpha,\beta,\gamma,\delta,\epsilon,\theta,\lambda,\mu,\pi,\sigma,\phi,\omega, etc. - Uppercase:
\Gamma,\Delta,\Theta,\Lambda,\Pi,\Sigma,\Phi,\Omega, etc.
Math Operators
-
\pm(±),\times(×),\div(÷),\cdot(·) -
\oplus(⊕),\otimes(⊗),\ast(∗)
Relations
-
\leq(≤),\geq(≥),\neq(≠),\approx(≈) -
\equiv(≡),\sim(∼),\propto(∝) -
\subset(⊂),\supset(⊃),\in(∈),\notin(∉)
Arrows
-
\rightarrowor\to(→),\leftarrow(←),\leftrightarrow(↔) -
\Rightarrow(⇒),\Leftarrow(⇐),\Leftrightarrow(⇔) -
\uparrow(↑),\downarrow(↓)
Calculus & Analysis
-
\int(∫),\sum(∑),\prod(∏) -
\partial(∂),\nabla(∇),\infty(∞)
Logic & Set Theory
-
\forall(∀),\exists(∃),\neg(¬) -
\land(∧),\lor(∨),\cap(∩),\cup(∪) -
\emptyset(∅),\therefore(∴),\because(∵)
Superscripts & Subscripts
- Superscripts:
^0through^9(⁰¹²³⁴⁵⁶⁷⁸⁹),^i(ⁱ),^n(ⁿ) - Superscript symbols:
^+(⁺),^-(⁻),^=(⁼),^((⁽),^)(⁾) - Subscripts:
_0through_9(₀₁₂₃₄₅₆₇₈₉) - Subscript symbols:
_+(₊),_-(₋),_=(₌),_((₍),_)(₎) - Note: Unicode has limited super/subscript characters; unsupported characters will display normally
Brackets
-
\langle,\rangle(⟨⟩) -
\lceil,\rceil(⌈⌉) -
\lfloor,\rfloor(⌊⌋)
Miscellaneous
-
\checkmark(✓),\degree(°),\copyright(©) -
\dots(…),\cdots(⋯) - Musical symbols:
\sharp(♯),\flat(♭),\natural(♮)
Examples
# Quadratic formula
tex_to_unicode 'x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}'
# Output: x = -b ± √(b² - 4ac)/2a
# Euler's identity
tex_to_unicode 'e^{i\pi} + 1 = 0'
# Output: eⁱπ + 1 = 0
# Set theory
tex_to_unicode 'A \cup B = \{x : x \in A \lor x \in B\}'
# Output: A ∪ B = {x : x ∈ A ∨ x ∈ B}
# Calculus
tex_to_unicode '\int_a^b f(x)dx = F(b) - F(a)'
# Output: ∫ₐᵇ f(x)dx = F(b) - F(a)Limitations
While Unicode provides many mathematical symbols, some TeX constructs cannot be perfectly represented:
- Complex fractions are approximated
- Matrices and arrays have limited support
- Some accents and diacritics are approximated
-
Subscripts and superscripts support limited characters due to Unicode constraints:
- Supported superscripts: digits (⁰¹²³⁴⁵⁶⁷⁸⁹), letters i and n (ⁱⁿ), and symbols ⁺⁻⁼⁽⁾
- Supported subscripts: digits (₀₁₂₃₄₅₆₇₈₉) and symbols ₊₋₌₍₎
- Unsupported characters (like
^\infty,^\alpha,_\beta) will have the^or_marker removed and display as regular characters - Example:
\int_0^\inftybecomes∫₀∞(not∫₀^∞with superscript infinity, as Unicode has no superscript ∞)
Development
To build and test locally:
# Build the gem
gem build tex_to_unicode.gemspec
# Install locally
gem install tex_to_unicode-0.1.0.gem
# Test the executable
tex_to_unicode '\alpha + \beta'Contributing
Bug reports and pull requests are welcome!
License
This gem is available as open source under the terms of the MIT License.