Noah Rahbek Bigum Hansen

Noah Rahbek Bigum Hansen

Mechanical Engineering student at Aarhus University. Developer and maintainer of Tungsten. Top-15 national Tetr.io player.

Syntax Reference

Tungsten uses a custom parser that supports a mix of standard LaTeX syntax and intuitive shorthand common in programming or handwritten math.

Variables and Constant## Quick Reference Cheatsheet

CategoryInput ExampleNotes
Arithmetic1 + 2 * 3Standard order of operations applies
Exponentsx^2, e^{x+1}Use {} to group complex exponents.
Fractions\frac{a}{b}, a/bBoth LaTeX and inline division are supported.
Grouping(a + b)cParentheses () define scope.
Functions\sin(x), f(x), \ln xStandard function calls.
Variablesx, vel, x1Must start with a letter.
Greek\alpha, \beta, \piStandard LaTeX commands.
Matrices\begin{pmatrix}...\end{pmatrix}Supports p, b, and v matrix types.
Vectors\vec{v}, \mathbf{v}, \begin{pmatrix}...\end{pmatrix}Symbolic vector notation or as row or column matrix.
Derivatives\frac{d}{dx} x^2, f', \dot{f}Multiple notations for differentiation.
Integrals\int_{0}^{\infty} e^{-x} dxDefinite and indefinite integrals.

1. Variables and Constants

Tungsten uses strict tokenization rules to distinguish between variables, numbers, and commands.

Variable Naming

  • Structure: Variable names must begin with a letter (A-Z, a-z).
  • Alphanumeric: After the first letter, variables may contain digits or underscores.
    • ✅ Valid: x, y_2, velocity, Area51
    • ❌ Invalid: 1x (starts with digit), _x (starts with underscore)
  • Case Sensitivity: x and X are treated as distinct variables.

Greek Letters

Tungsten recognizes standard LaTeX commands for Greek letters. These are treated as symbolic entities distinct from standard variables.

  • Syntax: \alpha, \beta, \Gamma, \omega, etc.
  • Usage: They can be used anywhere a variable is used: 2\pi, \sin(\theta).

Constants

Certain symbols are reserved or automatically recognized as mathematical constants depending on the backend:

  • \pi or pi
  • e (Euler's number)
  • \infty (Infinity)

2. Operators and Precedence

The parser adheres to standard mathematical precedence rules. When operators share the same precedence, associativity determines the order of evaluation.

PrecedenceOperatorDescriptionAssociativitySyntax Variants
3 (Highest)^ExponentiationRightx^2, x^{y+1}
2*MultiplicationLeft*, \cdot, \times
2/DivisionLeft/, \frac{num}{den}
1+AdditionLeft+
1-SubtractionLeft-
0 (Lowest)=EqualityNone=, ==, &=

Important Notes on Operators:

  • Right Associativity (^): 2^3^4 is parsed as 2^(3^4), not (2^3)^4.
  • Synonyms: The parser treats \cdot (dot) and \times (cross) identically to * in scalar arithmetic contexts. However, in Linear Algebra contexts, they denote Dot Product and Cross Product respectively.

Implicit Multiplication

You can often omit the multiplication operator, just like in handwritten math. The parser infers multiplication in the following cases:

  • Number + Variable: 2x becomes 2 * x
  • Variable + Variable: x y becomes x * y (Note: xy is parsed as a single variable named "xy". Use a space to separate them.)
  • Group + Variable: (a+b)x becomes (a+b) * x
  • Variable + Group: x(x+1) becomes x * (x+1) unless x is a known function name.
  • Number + Greek: 2\pi becomes 2 * \pi
  • Function Chains: \sin(x)\cos(y) becomes \sin(x) * \cos(y)

3. Functions and Grouping

Function Syntax

  • Standard Calls: name(arg1, arg2)
    • Example: f(x), sin(x)
    • Built-in functions, evaluated function definitions, and names in symbolic_functions are recognized as calls.
    • The default symbolic names are f, g, h, and r, so symbolic mathematics and plotting can use them without a formula.
    • Other names before parentheses mean multiplication. Configure symbolic_functions for additional undefined symbolic functions; an empty list disables the default symbolic names.
    • Derivative notation locally identifies dependent functions, preserving initial conditions such as y(0) in an ODE containing y'.
  • LaTeX Style Calls: \command{arg}
    • Example: \sqrt{x}, \frac{1}{2}
  • Parenthesis-less Calls: For common log/trig functions, parentheses can sometimes be omitted if the argument is a single token.
    • \ln x is valid.
    • \sin \theta is valid.
    • \cos(x + 1) is valid

Session-local Function Definitions

Select f(x) = x^2 and run :TungstenEvaluate to define f. Then select f(5) and evaluate it to obtain 25. You can also use := or select the definition and run :TungstenDefinePersistentVariable.

Definitions accept Latin identifier names and distinct Latin identifier parameters, for example difference(x,y) = x-y. Both square(t) = t(t+1) and subsequent calls such as square(5) are supported; t(t+1) is multiplication. Redefining a function replaces its previous formula. Function calls are expanded as AST expressions before evaluation by either backend, so Wolfram persistence is not required.

Definitions last for the current Neovim session and are cleared by :TungstenClearPersistentVars. They currently support algebraic bodies, including built-in and other defined function calls. Recursive definitions and bodies containing bound-variable calculus notation such as integrals or summations are rejected. An equation such as f(5) = 25 is not a definition.

Modified Bessel Functions

Tungsten supports the modified Bessel functions of the first and second kinds using their conventional uppercase notation:

  • I_0(x) and I_{0}(x) denote the modified Bessel function of the first kind.
  • K_0(x) and K_{0}(x) denote the modified Bessel function of the second kind.
  • The order may be symbolic or an arbitrary braced expression, for example I_\nu(x), K_n(x), or I_{n+1}(x).
  • The argument must be parenthesized.
  • Only uppercase I and K are recognized as modified Bessel notation.
  • A bare expression such as I_0 remains an ordinary subscripted symbol; only I_0(...) is treated as a function call.

Internally these are represented as besseli(order, argument) and besselk(order, argument). Wolfram maps them to BesselI and BesselK, while the Python backend maps them to SymPy's besseli and besselk. Python plotting still requires SciPy for these special functions and will ask you to use the Wolfram plotting backend until SciPy support is added.

Grouping Symbols

  • Parentheses (): Used for standard mathematical grouping and function arguments.
  • Braces {}: Used strictly for LaTeX command arguments (e.g., x^{2}, \frac{a}{b}).
  • Brackets []: Often used for defining lists or specific matrix notations, though () is preferred for arithmetic precedence.

4. Syntax Limitations and Edge Cases

To ensure the parser behaves predictably, certain inputs that might look "okay" to a human are strictly invalid.

1. Decimal Formatting

Numbers must have a leading digit before the decimal point.

  • ✅ Correct: 0.5
  • ❌ Incorrect: .5 (This will fail to parse as a number)

2. Whitespace and Implicit Multiplication

As noted in section 3, whitespace is semantically significant for variables.

  • xy →\rightarrow Variable named "xy"
  • x y →\rightarrow Multiplication x * y

3. LaTeX Environment Closure

All LaTeX environments must be perfectly balanced.

  • ❌ \begin{pmatrix} 1 & 2 (Missing end tag)
  • ❌ \frac{1}{2 (Unmatched brace)

4. Argument Bracing

LaTeX commands like \frac and \sqrt expect arguments in braces {}.

  • ✅ x^{10}
  • ⚠️ x^10 (Parses as (x^1) * 0 like it would in standard LaTeX)
  • ❌ \frac 1 2 (Arguments must be braced: \frac{1}{2})

5. Multi-line Inputs

For systems of equations or similar you are able to use the standard LaTeX-construction of &= and \\. &= is parsed as = and \\ is parsed as beginning a new expression.

x + y &= 1 \\
x - y &= 2

You are also able to use semi-colons ; between expressions:

x + y = 1;
x - y = 2

Found syntax that should work but doesn't? The world of mathematical notation is vast. If you encounter valid mathematical notation that Tungsten fails to parse, please file a bug report or feature request.