Desmos.jl API
Public
Desmos.DesmosState — Type
DesmosStateRepresents the complete state of a Desmos graph, including all expressions, viewport settings, and configuration options.
DesmosState is typically created using the @desmos macro rather than constructed directly. The state object can be serialized to JSON format compatible with the Desmos API, and can be displayed directly in Jupyter notebooks or VSCode.
Fields
version::Int: Desmos API version (default:11)random_seed::String: Random seed for reproducibility (default:"00000000000000000000000000000000")graph::DesmosGraph: Graph configuration including viewport settingsexpressions::DesmosExpressions: Collection of all expressions, text, images, etc.
Examples
using Desmos
# Create using @desmos macro (recommended)
state = @desmos begin
@text "Example graph"
sin(x)
cos(x)
end
# Serialize to JSON
using JSON
json_string = JSON.json(state)
# Display in Jupyter/VSCode
display(state) # Shows interactive Desmos graphJSON I/O
DesmosState can be parsed from JSON files exported from Desmos:
using JSON
# Read from file
json_data = read("graph.json", String)
state = JSON.parse(json_data, DesmosState)
# Write to file
write("graph.json", JSON.json(state, 4))See also
@desmos: Macro for creatingDesmosStateobjects
Desmos.UnsupportedDesmosSyntaxError — Type
UnsupportedDesmosSyntaxErrorException thrown when attempting to use Julia syntax that cannot be converted to Desmos-compatible LaTeX.
Example
# This will throw UnsupportedDesmosSyntaxError because Desmos doesn't support \ne
desmos_latexify(:(a != b))Desmos.clipboard_desmos_state — Method
clipboard_desmos_state()Import a Desmos graph state from clipboard.
This function reads JSON data from the system clipboard and parses it into a DesmosState object. Useful for importing graphs that were edited in the Desmos calculator and exported using the "Export State to Clipboard" button (available when clipboard=true).
Examples
# Enable clipboard mode
Desmos.set_display_options(clipboard=true)
# Display a graph
state = @desmos begin
y = x^2
end
# Edit the graph in the plot pane and click "Export State to Clipboard"
# Then import the modified state
modified_state = Desmos.clipboard_desmos_state()Notes
- Requires clipboard access (uses
clipboard()function) - The clipboard must contain valid Desmos state JSON
- Throws an error if the JSON is invalid or cannot be parsed
Desmos.desmos_latexify — Method
desmos_latexify(ex) -> LaTeXStringConvert Julia expressions to LaTeX strings suitable for Desmos. This is a simpler, Desmos-focused alternative to Latexify.latexify.
Desmos.get_desmos_display_config — Method
get_desmos_display_config()Get current display options for Desmos graphs.
Examples
Desmos.get_desmos_display_config()Desmos.set_desmos_display_config — Method
set_desmos_display_config(; width=nothing, height=nothing, clipboard=nothing, api_version=nothing, api_key=nothing)Set display options for Desmos graphs.
Arguments
width::Int: Width of the Desmos container in pixels. Set to 0 for automatic width (100%)height::Int: Height of the Desmos container in pixelsclipboard::Bool: Enable clipboard export buttonapi_version::Int: Desmos API v1 minor version (e.g., 10 for v1.10)api_key::String: Desmos API key
Examples
# Set clipboard mode with custom size
Desmos.set_desmos_display_config(width=800, height=600, clipboard=true)
# Enable automatic width (responsive)
Desmos.set_desmos_display_config(width=0)
# Enable only clipboard mode
Desmos.set_desmos_display_config(clipboard=true)
# Set custom API version and key (e.g., v1.11)
Desmos.set_desmos_display_config(api_version=11, api_key="your-api-key-here")Desmos.@desmos — Macro
@desmos begin ... endCreate a DesmosState object from a block of mathematical expressions.
The @desmos macro provides a convenient DSL for creating Desmos graphs programmatically. Each line in the block is converted to a Desmos expression, and the macro returns a DesmosState object that can be serialized to JSON or displayed in Jupyter/VSCode.
Supported syntax
- Mathematical expressions:
sin(x),x^2 + y^2, etc. - Text annotations:
@text "Description" - Styled expressions:
@expression cos(x) color=RGB(1,0,0) - Parametric curves:
@expression (cos(t), sin(t)) parametric_domain=0..2π - Variable assignments:
a = 5 - Sliders:
@expression b = 3 slider=1..10 - Domain restrictions:
@expression f(x) domain=-5..5 - Images:
@image image_url="..." width=10 height=10 - Dollar interpolation: Use
$(expr)to evaluate Julia expressions at macro expansion time
Examples
# Basic trigonometric functions
state = @desmos begin
@text "Trigonometric functions"
sin(x)
cos(x)
tan(x)
end
# With colors and styling
state = @desmos begin
@expression sin(x) color=RGB(1, 0, 0)
@expression cos(x) color="#0000ff"
end
# Parametric curve
state = @desmos begin
@expression (cosh(t), sinh(t)) parametric_domain=-2..3
end
# Variables and sliders
b = 3
state = @desmos begin
a = 4
@expression c = 5 slider=2..6
sin($(2b) * a - c * x)
end
# With domain restrictions
state = @desmos begin
@expression x^2 domain=-5..5
endSee also
DesmosState: The state object returned by this macro@expression: Macro for creating expressions with options@text: Macro for adding text annotations@image: Macro for adding images
Private
Desmos.DESMOS_DISPLAY_CONFIG — Constant
Global display configuration for Desmos graphs.
Desmos.DesmosDisplayConfig — Type
DesmosDisplayConfigConfiguration for Desmos graph display in plot panes.
Fields
width::Int: Width of the Desmos container in pixels (default:600). Set to 0 for automatic width (100%)height::Int: Height of the Desmos container in pixels (default:400)clipboard::Bool: Enable clipboard export button (default:false)api_version::Int: Desmos API v1 minor version (default:10for v1.10)api_key::String: Desmos API key (default:"dcb31709b452b1cf9dc26972add0fda6". See https://www.desmos.com/api/v1.10/docs/index.html)
Desmos._latexify — Function
desmos_latexify(ex) -> StringConvert a Julia expression to LaTeX string. Main dispatcher for different expression types.
Desmos.generate_table — Function
generate_table(data; id, color)Convert table data to a DesmosTable using multiple dispatch.
This is the main entry point for table generation. Different methods handle:
- Vector{Symbol} and Vector: Column specifications from
@table (x_1=[1,2,3], y_1=[4,5,6]) - NamedTuple: Tables from NamedTuples
- DataFrame: Tables from DataFrames.jl (via package extension)
- Other table-like objects can add their own methods
Arguments
data: Table data (Vector pair, NamedTuple, DataFrame, or other table-like object)id: ID for the tablecolor: Color string for the table columns (default: "#000000")
Desmos.generate_table — Method
generate_table(nt::NamedTuple; id, color)Convert a NamedTuple to a DesmosTable.
Each field in the NamedTuple becomes a DesmosColumn with the field name as the LaTeX label.
Arguments
nt: NamedTuple with vectors as valuesid: Table IDcolor: Color for the table columns
Example
nt = (x_1=[1,2,3], y_1=[4,5,6])
@desmos begin
@table $nt
endDesmos.generate_table — Method
generate_table(column_names::Vector{Symbol}, column_values::Vector; id, color)Convert column names and values to a DesmosTable.
Arguments
column_names: Vector of column name symbolscolumn_values: Vector of column value arrays (ornothingfor columns without values)id: Table IDcolor: Color for the table columns
Example
generate_table([:x_1, :y_1], [[1,2,3], [4,5,6]]; id="1", color="#FF0000")
generate_table([:x_1, :y_2], [[1,2,3], nothing]; id="1", color="#000000")Desmos.@expression — Macro
@expression expr [options...]Create a DesmosExpression with additional styling and behavior options.
This macro is used within a @desmos block to create expressions with customization options such as color, sliders, domains, and visibility settings.
Options
color: Set the color usingRGB(r,g,b)or hex string like"#ff0000"slider: Create a slider from a range (e.g.,1..10or1:2:10)domain: Restrict the domain using an interval (e.g.,-5..5)parametric_domain: Set domain for parametric curves (e.g.,0..2π)lines: Boolean to show/hide connecting lineshidden: Boolean to hide the expression initiallyid: Custom ID string for the expression
Examples
julia> Desmos.@expression sin(x) color=RGB(1, 0, 0)
(Desmos.DesmosExpression("expression", "0", "#FF0000", nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, L"\sin\left(x\right)", nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing), 1)
julia> Desmos.@expression cos(x) color="#0000ff"
(Desmos.DesmosExpression("expression", "0", "#0000ff", nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, L"\cos\left(x\right)", nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing), 1)
julia> Desmos.@expression a = 3 slider=1..10
(Desmos.DesmosExpression("expression", "0", "#000000", nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, L"a=3", Desmos.DesmosSlider(true, true, nothing, nothing, nothing, "1", "10", nothing), nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing), 1)
julia> Desmos.@expression b = 5 slider=1:0.5:10
(Desmos.DesmosExpression("expression", "0", "#000000", nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, L"b=5", Desmos.DesmosSlider(true, true, nothing, nothing, nothing, "1.0", "10.0", "0.5"), nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing), 1)
julia> Desmos.@expression (cos(t), sin(t)) parametric_domain=0..2π
(Desmos.DesmosExpression("expression", "0", "#000000", nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, L"\left(\cos\left(t\right),\sin\left(t\right)\right)", nothing, nothing, Desmos.DesmosParametricDomain("0.0", "6.283185307179586"), nothing, nothing, nothing, nothing, nothing, nothing), 1)
julia> Desmos.@expression x^2 domain=-5..5
(Desmos.DesmosExpression("expression", "0", "#000000", nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, L"x^{2}", nothing, Desmos.DesmosDomain(L"-5", L"5"), nothing, nothing, nothing, nothing, nothing, nothing, nothing), 1)
julia> Desmos.@expression f(x) = x^3 hidden=true
(Desmos.DesmosExpression("expression", "0", "#000000", nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, true, L"f\left(x\right)=x^{3}", nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing), 1)
julia> Desmos.@expression L"\\frac{x^2}{4} + \\frac{y^2}{9} = 1" color=RGB(0, 0.5, 1)
(Desmos.DesmosExpression("expression", "0", "#0080FF", nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, L"\\frac{x^2}{4} + \\frac{y^2}{9} = 1", nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing), 1)See also
Desmos.@image — Macro
@image options...Add an image to the Desmos graph.
This macro creates a DesmosImage object that can be positioned and sized on the graph. Images are specified via URL and can be customized with various display options.
Options
image_url: URL of the image (required)width: Width of the image in graph unitsheight: Height of the image in graph unitsname: Display name for the imagehidden: Boolean to hide the image initiallyid: Custom ID string for the image
Examples
julia> Desmos.@image image_url="https://example.com/plot.png" name="Background" width=20 height=15
Desmos.DesmosImage("image", "0", "https://example.com/plot.png", nothing, "Background", "20", "15", nothing, nothing, nothing, nothing, nothing)See also
@desmos: Main macro for creating Desmos graphs@expression: Add mathematical expressions@text: Add text annotations
Desmos.@table — Macro
@table data [kwargs...]Add a table to the Desmos graph.
This macro creates a DesmosTable object for displaying tabular data. The first argument specifies the data (DataFrame or column tuple), and remaining arguments are keyword arguments for table attributes.
Arguments
data: Table data - either a DataFrame/NamedTuple/table object or a tuple of column specifications- DataFrame:
$df - NamedTuple:
$nt - Column tuple:
(x_1=[1,2,3], y_1=[4,5,6])or(x_1=[1,2,3], y_2)for symbol-only columns
- DataFrame:
Keyword Arguments (must come after data)
id: Custom ID string for the table elementcolor: Set the color usingRGB(r,g,b)or hex string like"#ff0000"
Examples
# Using a DataFrame
using DataFrames
df = DataFrame(x_1=[1,2,3], y_1=[3,4,4])
@desmos begin
@table $df
@table $df color=RGB(1,0,0)
end
# Using a NamedTuple
nt = (x_1=[1,2,3], y_1=[3,4,4])
@desmos begin
@table $nt
@table $nt color=RGB(1,0,0)
end
# Using column tuple
@desmos begin
@table (x_1=[1,2,5], y_1=[4,7,4])
@table (x_1=[1,2,5], y_1=[4,7,4]) color="#FF0000"
end
# Using symbols in tuple (for columns without values)
@desmos begin
y_2 = [5,4,8]
@table (x_1=[1,2,5], y_1=[4,7,4], y_2)
endSee also
@desmos: Main macro for creating Desmos graphs@expression: Add mathematical expressions@text: Add text annotations
Desmos.@text — Macro
@text "text" [options...]Add a text annotation to the Desmos graph.
This macro creates a DesmosText object for displaying textual information on the graph, such as titles, labels, or descriptions.
Options
id: Custom ID string for the text element
Examples
julia> Desmos.@text "Sample text"
(Desmos.DesmosText("text", "0", "Sample text", nothing), 1)See also
@desmos: Main macro for creating Desmos graphs@expression: Add mathematical expressions@image: Add images to the graph