KatexUtils 0.3.4

KatexUtils 0.3.4

Maintained by Ni Kesu.



  • By
  • nikesu

KatexUtils

KatexUtils provides a UIView KatexView to render a LaTeX expression for iOS apps using KaTeX. It also provide a wrapper of KaTeX engine called KatexRenderer to access KaTeX's renderToString API.

Version License Platform

Note: KatexUtils is still in development, and the API is not guaranteed to be stable. It's subject to change without warning.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Usage

Use KatexView to create a UIView that renders a LaTeX formula:

var katexView = KatexView(latex: "a^2 + b^2 = c^2")

Use custom rendering options just like in KaTeX:

var katexView = KatexView(latex: #"c = \pm\sqrt{a^2 + b^2}\in\RR"#,
                          options: [
                            .displayMode: true,
                            .macros: [#"\RR"#: #"\mathbb{R}"#]
                          ])

KatexView will automaticlly make itself scrollable when it's frame can not contains the whole formula. You can also observe the status property to handle error or do some customize change when rendering is finished successfully:

cancellable = katexView.$status.sink { [weak self] status in
    let MAXWIDTH : CGFloat = 300.0
    let MAXHEIGHT : CGFloat = 100.0
    let contentSize = self?.katexView.intrinsicContentSize

    switch status {
    case .finished:
        self?.katexView.frame =
            CGRect(x: 0,
                   y: 0,
                   width: min(contentSize?.width ?? .infinity, MAXWIDTH),
                   height: min(contentSize?.height  ?? .infinity, MAXHEIGHT))
    case .error(let message):
        fatalError(message)
    default:
        return
    }
}

KatexRenderer use KaTeX to render a LaTeX formula to a HTML string:

let str = KatexRenderer.renderToString(latex: "a^2 + b^2 = c^2", options: [.displayMode : true])

For supported functions, check the document of KaTeX.

Requirements

  • iOS 13.0
  • Swift 5.3

Installation

CocoaPods

KatexUtils is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'KatexUtils', '~> 0.3.0'

Installation with Swift Package Manager

Once you have your Swift package set up, adding KatexUtils as a dependency is as easy as adding it to the dependencies value of your Package.swift:

dependencies: [
    .package(url: "https://github.com/nks5117/KatexUtils.git", .upToNextMinor(from: "0.3.0"))
]

Author

Ni Kesu, [email protected]

License

KatexUtils is available under the MIT license. See the LICENSE file for more info.