From 8d32feb1bdbb5a6e9838ee44a49a682f2d37e880 Mon Sep 17 00:00:00 2001 From: Guille Gonzalez Date: Sun, 11 Jan 2026 06:53:55 +0100 Subject: [PATCH] Add documentation --- Sources/SwiftUIMath/Font.swift | 21 ++++++++++++++ Sources/SwiftUIMath/Math.swift | 33 ++++++++++++++++++++++ Sources/SwiftUIMath/RenderingMode.swift | 5 ++++ Sources/SwiftUIMath/TypesettingStyle.swift | 4 +++ 4 files changed, 63 insertions(+) diff --git a/Sources/SwiftUIMath/Font.swift b/Sources/SwiftUIMath/Font.swift index 6ccdb01..15be41b 100644 --- a/Sources/SwiftUIMath/Font.swift +++ b/Sources/SwiftUIMath/Font.swift @@ -1,22 +1,30 @@ import SwiftUI extension Math { + /// Identifies a math font and size used for typesetting. public struct Font: Hashable, Sendable { + /// Known math font names bundled with the package. public struct Name: Hashable, Sendable, RawRepresentable, ExpressibleByStringLiteral { + /// The raw font name used in the bundle. public let rawValue: String + /// Creates a name from a raw font string. public init(rawValue: String) { self.rawValue = rawValue } + /// Creates a name from a string literal. public init(stringLiteral value: StringLiteralType) { self.rawValue = value } } + /// The bundled font name. public let name: Name + /// The font size in points. public let size: CGFloat + /// Creates a font configuration. public init(name: Name, size: CGFloat) { self.name = name self.size = size @@ -25,21 +33,34 @@ extension Math { } extension Math.Font.Name { + /// Latin Modern Math. public static let latinModern: Self = "latinmodern-math" + /// KpMath Light. public static let kpMathLight: Self = "KpMath-Light" + /// KpMath Sans. public static let kpMathSans: Self = "KpMath-Sans" + /// XITS Math. public static let xits: Self = "xits-math" + /// TeX Gyre Termes Math. public static let termes: Self = "texgyretermes-math" + /// Asana Math. public static let asana: Self = "Asana-Math" + /// Euler Math. public static let euler: Self = "Euler-Math" + /// Fira Math. public static let fira: Self = "FiraMath-Regular" + /// Noto Sans Math. public static let notoSans: Self = "NotoSansMath-Regular" + /// Libertinus Math. public static let libertinus: Self = "LibertinusMath-Regular" + /// Garamond Math. public static let garamond: Self = "Garamond-Math" + /// Lete Sans Math. public static let leteSans: Self = "LeteSansMath" } extension View { + /// Sets the math font used by ``Math`` views in this hierarchy. public func mathFont(_ font: Math.Font) -> some View { environment(\.mathFont, font) } diff --git a/Sources/SwiftUIMath/Math.swift b/Sources/SwiftUIMath/Math.swift index c706f10..21dc722 100644 --- a/Sources/SwiftUIMath/Math.swift +++ b/Sources/SwiftUIMath/Math.swift @@ -1,5 +1,36 @@ import SwiftUI +/// Renders a LaTeX math expression using SwiftUI. +/// +/// You can create a view with a LaTeX string; this is a good default for short expressions: +/// +/// ```swift +/// Math("x^2 + y^2 = z^2") +/// ``` +/// +/// If you need inline vs display behavior, set the typesetting style: +/// +/// ```swift +/// Math("\\int_0^1 x^2\\,dx = \\frac{1}{3}") +/// .mathTypesettingStyle(.text) +/// +/// Math("\\frac{1}{2}+\\sqrt{2}") +/// .mathTypesettingStyle(.display) +/// ``` +/// +/// To customize the appearance, choose a bundled math font and size: +/// +/// ```swift +/// Math("\\sum_{i=1}^{n}x_i") +/// .mathFont(Math.Font(name: .latinModern, size: 24)) +/// ``` +/// +/// For multicolor expressions, enable multicolor mode and use hex colors: +/// +/// ```swift +/// Math("\\color{#cc0000}{a}+\\color{#00aa00}{b}+\\color{#0000cc}{c}") +/// .mathRenderingMode(.multicolor) +/// ``` public struct Math: View { @Environment(\.mathFont) private var font @Environment(\.mathTypesettingStyle) private var typesettingStyle @@ -7,6 +38,8 @@ public struct Math: View { private let latex: String + /// Creates a math view that renders the given LaTeX string. + /// - Parameter latex: A LaTeX math expression. public init(_ latex: String) { self.latex = latex } diff --git a/Sources/SwiftUIMath/RenderingMode.swift b/Sources/SwiftUIMath/RenderingMode.swift index 443c60d..79fedad 100644 --- a/Sources/SwiftUIMath/RenderingMode.swift +++ b/Sources/SwiftUIMath/RenderingMode.swift @@ -1,10 +1,14 @@ import SwiftUI extension Math { + /// Controls how colors are applied when rendering math. public enum RenderingMode: Sendable { + /// Draws all glyphs using the view's foreground style. case monochrome + /// Honors LaTeX color commands and uses the base color for uncolored glyphs. case multicolor(base: SwiftUI.Color) + /// Multicolor rendering using the view's primary color as the base. static var multicolor: Self { .multicolor(base: .primary) } @@ -12,6 +16,7 @@ extension Math { } extension View { + /// Sets the rendering mode for ``Math`` views in this hierarchy. public func mathRenderingMode(_ mathRenderingMode: Math.RenderingMode) -> some View { environment(\.mathRenderingMode, mathRenderingMode) } diff --git a/Sources/SwiftUIMath/TypesettingStyle.swift b/Sources/SwiftUIMath/TypesettingStyle.swift index fb7bd83..1c18e28 100644 --- a/Sources/SwiftUIMath/TypesettingStyle.swift +++ b/Sources/SwiftUIMath/TypesettingStyle.swift @@ -1,13 +1,17 @@ import SwiftUI extension Math { + /// Controls how math is typeset (display vs inline text). public enum TypesettingStyle: Sendable { + /// Display style for standalone equations. case display + /// Text style for inline math. case text } } extension View { + /// Sets the typesetting style for ``Math`` views in this hierarchy. public func mathTypesettingStyle(_ typesettingStyle: Math.TypesettingStyle) -> some View { environment(\.mathTypesettingStyle, typesettingStyle) }