Function Compatibility
This page describes how Julia expressions are translated to Desmos LaTeX format, and explains the design principles behind the conversion.
Design Principles
Desmos.jl converts Julia Expr objects to LaTeX strings that Desmos understands. The conversion follows these principles:
Natural Julia Syntax: Write mathematical expressions using standard Julia syntax, and let Desmos.jl handle the LaTeX conversion automatically.
Standard Library Mapping: Function names from Julia's standard library and common packages are mapped to their Desmos equivalents. For example:
log(x)→\ln(x)(natural logarithm)sum(xs)→\operatorname{total}(xs)length(xs)→\operatorname{count}(xs)
Subscript Convention: Multi-character identifiers are converted to subscripted notation:
xy→x_{y}(subscript, not multiplication)x1→x_{1}θ₁→\theta_{1}
This subscript behavior means that xy is treated as a single variable "x with subscript y", not as "x times y". Use x*y for multiplication.
Examples
Here's a practical example demonstrating how Julia expressions are automatically converted to Desmos notation:
using Desmos
# Create a graph with various mathematical expressions
@desmos begin
sin(x)
a1 = 2
gradient(sin(a1*x), x)
sum(n^2 for n in 1:5)
sum([n^2 for n in 1:5])
end
This example shows:
- Function mapping:
sin(x)→ Desmos trigonometric function\sin(x) - Multi-character variables:
a1→a_{1}(subscripted) - Derivative notation:
gradient(sin(a1*x), x)→\frac{d}{dx}\sin(a_{1}x) - Summation with generator:
sum(n^2 for n in 1:5)→\sum_{n=1}^{5}n^{2} - List operations:
sum([...])→\operatorname{total}([...])
The desmos_latexify Function
The desmos_latexify function is the core of the conversion system. It takes a Julia expression and returns a LaTeXString:
julia> using Desmosjulia> # Basic arithmetic desmos_latexify(:(x + y))"x+y"julia> desmos_latexify(:(x * y))"xy"julia> desmos_latexify(:(x / y))"\\frac{x}{y}"julia> desmos_latexify(:(x^2))"x^{2}"julia> # Functions desmos_latexify(:(sin(x)))"\\sin\\left(x\\right)"julia> desmos_latexify(:(log(x)))"\\ln\\left(x\\right)"julia> desmos_latexify(:(sqrt(x)))"\\sqrt{x}"julia> # Multi-character identifiers become subscripts desmos_latexify(:(x1))"x_{1}"julia> desmos_latexify(:(xy))"x_{y}"julia> desmos_latexify(:(x_max))"x_{max}"julia> # Greek letters desmos_latexify(:(α + β))"\\alpha+\\beta"julia> desmos_latexify(:(θ₁))"\\theta_{1}"
Common Function Mappings
Trigonometric Functions
Julia's standard trigonometric functions are mapped to Desmos's LaTeX notation:
julia> using Desmosjulia> desmos_latexify(:(sin(x)))"\\sin\\left(x\\right)"julia> desmos_latexify(:(asin(x))) # arcsin"\\arcsin\\left(x\\right)"julia> desmos_latexify(:(sinh(x)))"\\sinh\\left(x\\right)"
Logarithms and Exponents
julia> using Desmosjulia> desmos_latexify(:(log(x))) # Natural log: ln(x)"\\ln\\left(x\\right)"julia> desmos_latexify(:(log(2, x))) # Log base 2"\\log_{2}\\left(x\\right)"julia> desmos_latexify(:(log10(x))) # Log base 10"\\log\\left(x\\right)"julia> desmos_latexify(:(exp(x)))"\\exp\\left(x\\right)"
Statistical Functions
julia> using Desmosjulia> desmos_latexify(:(mean(xs)))"\\operatorname{mean}\\left(x_{s}\\right)"julia> desmos_latexify(:(sum(xs))) # → total(xs)"\\operatorname{total}\\left(x_{s}\\right)"julia> desmos_latexify(:(length(xs))) # → count(xs)"\\operatorname{count}\\left(x_{s}\\right)"
Special Desmos.jl Functions
Some functions are specific to Desmos.jl and don't exist in Julia's standard library:
Derivatives: gradient
The gradient(expr, var) function creates derivative notation:
julia> using Desmosjulia> desmos_latexify(:(gradient(f(x), x)))"\\frac{d}{dx}f\\left(x\\right)"julia> desmos_latexify(:(gradient(x^2 + y^2, x)))"\\frac{d}{dx}x^{2}+y^{2}"
Integrals: integrate
Use integrate with a generator expression for definite integrals:
julia> using Desmosjulia> desmos_latexify(:(integrate(x^2 for x in 1..5)))"\\int_{1}^{5}x^{2}dx"julia> desmos_latexify(:(integrate(sin(t) for t in 0..π)))"\\int_{0}^{\\pi}\\sin\\left(t\\right)dt"
Summation and Products
Generator expressions work with sum and prod:
julia> using Desmosjulia> desmos_latexify(:(sum(n^2 for n in 1:10)))"\\sum_{n=1}^{10}n^{2}"julia> desmos_latexify(:(prod(n for n in 1:5)))"\\prod_{n=1}^{5}n"
Probability Distributions
Desmos.jl provides distribution constructors that map to Desmos's distribution functions:
julia> using Desmosjulia> desmos_latexify(:(Normal(0, 1)))"\\operatorname{normaldist}\\left(0,1\\right)"julia> desmos_latexify(:(Uniform(0, 1)))"\\operatorname{uniformdist}\\left(0,1\\right)"julia> desmos_latexify(:(Binomial(10, 0.5)))"\\operatorname{binomialdist}\\left(10,0.5\\right)"
These constructors are designed to be compatible with Distributions.jl naming conventions, but no using Distributions is required—Desmos.jl defines its own constructors that generate the appropriate Desmos syntax.
Common Pitfalls
1. Multi-character Variables
julia> using Desmosjulia> # This is NOT x times y: desmos_latexify(:(xy))"x_{y}"julia> # For multiplication, use *: desmos_latexify(:(x * y))"xy"
2. Division as Fractions
Division is always converted to fraction notation:
julia> using Desmosjulia> desmos_latexify(:(a / b))"\\frac{a}{b}"
3. Unsupported Operators
Some operators are not supported by Desmos:
julia> using Desmosjulia> # This will throw an error: desmos_latexify(:(x != y)) # Error: \ne not supportedERROR: UnsupportedDesmosSyntaxError: The inequality operator '!=' (\ne) is not supported by Desmos. Use other comparison operators instead.
4. Greek Letters in Subscripts
Greek letters are not allowed in subscripts:
julia> using Desmosjulia> # This will throw an error: desmos_latexify(:(xα)) # Error: Greek letters not allowed in subscriptsERROR: UnsupportedDesmosSyntaxError: Greek letters are not allowed in subscripts. Use Latin letters or digits only.julia> # Use alphabets or digits for subscripts: desmos_latexify(:(x1))"x_{1}"
List Comprehensions
Julia's list comprehension syntax is converted to Desmos's list comprehension:
julia> using Desmosjulia> desmos_latexify(:([x^2 for x in xs]))"\\left[x^{2}\\ \\operatorname{for}\\ x=x_{s}\\right]"julia> desmos_latexify(:([sin(t) for t in 0:10]))"\\left[\\sin\\left(t\\right)\\ \\operatorname{for}\\ t=\\left[0,...,10\\right]\\right]"
Vectors and Tuples
julia> using Desmosjulia> # Vectors use square brackets: desmos_latexify(:([1, 2, 3]))"\\left[1,2,3\\right]"julia> # Tuples use parentheses (useful for parametric expressions): desmos_latexify(:((cos(t), sin(t))))"\\left(\\cos\\left(t\\right),\\sin\\left(t\\right)\\right)"
Conditional Expressions
Use ifelse for conditional expressions:
julia> using Desmosjulia> desmos_latexify(:(ifelse(x > 0, x, -x)))"\\left\\{x>0:x,-x\\right\\}"
This generates Desmos's piecewise notation.
Custom Functions
For functions not in the standard mapping, Desmos.jl generates generic function notation:
julia> using Desmosjulia> # Custom function definition: desmos_latexify(:(f(x) = x^2 + 1))"f\\left(x\\right)=x^{2}+1"julia> # Custom function call: desmos_latexify(:(myFunc(a, b)))"m_{yFunc}\\left(a,b\\right)"
Tips for Effective Usage
Use explicit multiplication: Write
x * yinstead of relying on implicit multiplication, especially when working with single-letter variables.Leverage Unicode: Julia's Unicode support works great with Desmos. Use
α,β,θ, etc., and Unicode subscripts like₁,₂.Check conversions: Use
desmos_latexifydirectly to verify how your expressions will be converted.Standard library functions: Prefer standard Julia functions (e.g.,
sum,mean) over custom implementations, as they have proper mappings to Desmos functions.
Using LaTeX Strings Directly
If you need to input LaTeX expressions directly without conversion, you can use LaTeXStrings.jl:
using Desmos
using LaTeXStrings
# Direct LaTeX input using the L"..." string macro
myexpression = L"\sin(x)"
@desmos begin
@expression L"\cos(x)"
@expression $myexpression
end
This is useful when:
- You need specific LaTeX formatting that differs from the automatic conversion
- You're working with complex expressions that are easier to write directly in LaTeX
- You want to ensure exact control over the rendered output
Note that desmos_latexify also accepts LaTeXString inputs and will pass them through without modification (after removing the $ delimiters if present).