Display LaTeX using MathJax. The package also provides a custom LaTeX DSL, which enables you write LaTeX the way you write SwiftUI Views.
A View called LaTeXView
is avaiable to display formulas in LaTeX.
import LaTeX
import SwiftUI
struct ContentView: View {
var body: some View {
LaTeXView(formula: "\\pi")
}
}
As a javascript package was used to render LaTeX, the Outgoing network Connections needs to be enabled in Project Sandbox.
This part was designed with the goal of writing LaTeX the way you write SwiftUI.
let formula = LaTeXBuilder.build {
Sum("x", from: 1, to: 10) { x in
x
.colored(.blue)
.squared()
}
}
Note that the Components
are translated to LaTeX only when .latexExpression
is called.
print(formula)
// RangedLargeOperator<...>(...)
print(formula.latexExpression)
// \sum_{x = 1}^{10} {{{\color{3a81f6} x}}^2}
print(formula.evaluated())
// numeric(385.0)
Equals {
Subtraction {
Multiplication {
nabla
} by: {
"B".font(.mathbf).overRightArrowed()
}
} by: {
Fraction(1, "C")
.colored(.blue)
}
Fraction {
delta
"E".font(.mathbf).overRightArrowed()
} denominator: {
delta
"t"
}
} with: {
4
pi
rho
}
Integral("x", from: "-N", to: "N") { variable in
Power {
"e"
} with: {
variable
}
}
Equals {
Subscript {
"R"
} with: {
theta
}
} with: {
LaTeXMatrix {
[
[cos(theta), -sin(theta)],
[sin(theta), cos(theta)]
]
}
}
If you are working on a Swift Package, use
// inside Package.swift
dependencies: [
.package(url: "https://github.com/Vaida12345/Swift-LaTeX.git")
],
Otherwise, use the project link to find this package in Xcode.