Swiftpack.co - colinc86/LaTeXSwiftUI as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by colinc86.
colinc86/LaTeXSwiftUI v1.0.4
A SwiftUI view that renders LaTeX.
⭐️ 2
πŸ•“ 1 week ago
iOS macOS
.package(url: "https://github.com/colinc86/LaTeXSwiftUI.git", from: "v1.0.4")


A SwiftUI view that renders LaTeX.

Swift Version iOS Version macOS Version

πŸ“– Contents

πŸ“¦ Installation

Add the dependency to your package manifest src="https://raw.github.com/colinc86/LaTeXSwiftUI/main/

.package(url: "https://github.com/colinc86/LaTeXSwiftUI", from: "1.0.4")

⌨️ Usage

Import the package and use the view.

import LaTeXSwiftUI

struct MyView: View {

  var body: some View {
    LaTeX("Hello, $\\LaTeX$!")


βš™οΈ Modifiers

The LaTeX view's body is built up of Text views so feel free to use any of the supported modifiers.

LaTeX("Hello, $\\LaTeX$!")

Along with supporting the built-in SwiftUI modifies, LaTeXSwiftUI defines more to let you configure the view.

πŸ”€ Parsing Mode

LaTexSwiftUI can parse and render equations (aside frsrc="https://raw.github.com/colinc86/LaTeXSwiftUI/main/e entire input string) defined with the following terminators.


Text input can either be completely rendered, or LaTeXSwiftUI can search for top-level equations. The default behavior is to only render equations with onlyEquations. Use the parsingMode modifier to change the default behavior.

// Parse the entire input

#src="https://raw.github.com/colinc86/LaTeXSwiftUI/main/πŸŒ„ Image Rendering Mode

You can specify the rendering mode of the rendered equations so that they either take on the style of the surrounding text or display the style rendered by MathJax. The default behavior is to use the template rendering mode so that images match surrounding text.

// Render images to match the surrounding text
LaTeX("Hello, $\\color{red}\\LaTeX$!")

// Display the original rendered image
LaTeX("Hello, ${\\color{red} \\LaTeX}$!")

> <img style="max-width:100%;" src="./assets/images/rendering_mode.png" width="84.5" height="43">

#### 🚨 Error Mode

When an error occurs while parsing the input the view will display the original LaTeX. You can change this behavior by modifying the view's `errorMode`.

// Display the original text instead of the equation

// Display the error text instead of the equation

// Display the rendered image (if available)

🧱 Block Rendering Mode

The typical "LaTeX-ish" way to render the input is with blockViews. This mode renders text as usual, and block equations as... blocks; on their own line and centered. MathJax 3 does not support line breaking, so the view places block equations in horizontal scroll views in case the width of the equation is more than the width of the view.

In the case that you want to force block equations as inline, you can use the alwaysInline mode. You can also keep block styling with blockText, but the blocks will not be centered in their views. These modes can be helpful if you have a lengthy input string and need to only display it on a single or few lines.

/// The default block mode 
LaTeX("The quadratic formusrc="https://raw.github.com/colinc86/LaTeXSwiftUI/main/ $$x=\\frac{-b\\pm\\sqrt{b^2-4ac}}{2a}$$ and it has zeros at the roots of $f(x)=ax^2+bx+c$.")


/// Force blocks to render as inline
LaTeX("The quadratic formula is $$x=\\frac{-b\\pm\\sqrt{b^2-4ac}}{2a}$$ and it has zeros at the roots of $f(x)=ax^2+bx+c$.")


/// Force blocks to rendersrc="https://raw.github.com/colinc86/LaTeXSwiftUI/main/ext with newlines
LaTeX("The quadratic formula is $$x=\\frac{-b\\pm\\sqrt{b^2-4ac}}{2a}$$ and it has zeros at the roots of $f(x)=ax^2+bx+c$.")

πŸ”— Unencode HTML

Input may contain HTML entities such as &lt; which will not be parsed by LaTeX as anything meaningful. In this case, you may use the unencoded modifier.


// Replace "lt;" with "<"

♾️ TeX Options

For more control over the MathJax rendering, you can pass a TeXInputProcessorOptions object to the view.

LaTeX("Hello, $\\LaTeX$!")
  .texOptions(TeXInputProcessorOptions(loadPackages: [TeXInputProcessorOptions.Packages.base]))

πŸ—„οΈ Caching

LaTeXSwiftUI caches its SVG responses from MathJax and the images rendered as a result of the view's environment. If you want to control the cache, then you can access the static cache property.

The caches are managed automatically, but if, for example, you wanted to clear the cache manually you may do so.

// Clear the SVG data cache.

// Clear the rendered image cache.

LaTeXSwiftUI uses the caching components of the Nuke package.

πŸƒβ€β™€οΈ Preloading

SVGs and images are rendered and cached on demand, but there may be situations where you want to preload the data so that there is minimal lag when the view appears.

VStack {
  ForEach(expressions, id: \.self) { expression in


Stars: 2
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics