Skip to content

Commit a8da160

Browse files
committed
Add new modifiers and update README.
1 parent 186e176 commit a8da160

File tree

7 files changed

+329
-21
lines changed

7 files changed

+329
-21
lines changed

README.md

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ A SwiftUI view that renders LaTeX equations.
44

55
![Swift Version](https://img.shields.io/badge/Swift-5.7-orange?logo=swift) ![iOS Version](https://img.shields.io/badge/iOS-16-informational) ![macOS Version](https://img.shields.io/badge/macOS-13-informational)
66

7-
<center><img src="./assets/images/device.png" width="362" height="707"></center>
7+
<center><img src="./assets/images/device.png" height="362"></center>
88

99
## 📖 Contents
1010

@@ -16,8 +16,12 @@ A SwiftUI view that renders LaTeX equations.
1616
- [Image Rendering Mode](#🌄-image-rendering-mode)
1717
- [Error Mode](#🚨-error-mode)
1818
- [Block Rendering Mode](#🧱-block-rendering-mode)
19+
- [Numbered Block Equations](#🔢-numbered-block-equations)
20+
- [Equation Number Mode](#equation-number-mode)
21+
- [Equation Number Start](#equation-number-start)
22+
- [Equation Number Offset](#equation-number-offset)
1923
- [Unencode HTML](#🔗-unencode-html)
20-
- [TeX Options](#♾️-tex-options)
24+
- [TeX Options](#♾️-tex-options (deprecated))
2125
- [Caching](#🗄️-caching)
2226
- [Preloading](#🏃‍♀️-preloading)
2327

@@ -39,7 +43,7 @@ It won't
3943
Add the dependency to your package manifest file.
4044

4145
```swift
42-
.package(url: "https://github.com/colinc86/LaTeXSwiftUI", from: "1.0.4")
46+
.package(url: "https://github.com/colinc86/LaTeXSwiftUI", from: "1.1.0")
4347
```
4448

4549
## ⌨️ Usage
@@ -121,6 +125,8 @@ LaTeX("Hello, ${\\color{red} \\LaTeX}$!")
121125

122126
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`.
123127

128+
> Note: when the `rendered` mode is used, MathJax is instructed to load the `noerrors` and `noundefined` packages. In the other two modes, `original` and `error`, these packages are not loaded by MathJax and errors are either displayed in the view, or caught and replaced with the original text.
129+
124130
```swift
125131
// Display the original text instead of the equation
126132
LaTeX("$\\asdf$")
@@ -163,6 +169,41 @@ LaTeX("The quadratic formula is $$x=\\frac{-b\\pm\\sqrt{b^2-4ac}}{2a}$$ and it h
163169

164170
> <img src="./assets/images/blocks.png" width="430" height="350">
165171
172+
#### 🔢 Numbered Block Equations
173+
174+
The `LaTeX` view can do simple numbering of block equations with the `blockViews` block mode.
175+
176+
##### Equation Number Mode
177+
178+
Use the `equationNumberMode` modifier to change between `left`, `right` and `none`.
179+
180+
##### Equation Number Start
181+
182+
The default starting number is `1`, but if you need to start at a different value, you can specify it with the `equationNumberStart` modifier.
183+
184+
##### Equation Number Offset
185+
186+
To change the left or right offset of the equation number, use the `equationNumberOffset` modifier.
187+
188+
```swift
189+
// Don't number block equations (default)
190+
LaTeX("$$a + b = c$$")
191+
.equationNumberMode(.none)
192+
193+
// Add left numbers and a leading offset
194+
LaTeX("$$d + e = f$$")
195+
.equationNumberMode(.left)
196+
.equationNumberOffset(10)
197+
198+
// Add right numbers, a leading offset, and start at 2
199+
LaTeX("$$h + i = j$$ $$k + l = m$$")
200+
.equationNumberMode(.right)
201+
.equationNumberStart(2)
202+
.equationNumberOffset(20)
203+
```
204+
205+
> <img src="./assets/images/numbers.png">
206+
166207
#### 🔗 Unencode HTML
167208

168209
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.
@@ -178,7 +219,7 @@ LaTeX("$x^2&lt;1$")
178219

179220
> <img src="./assets/images/unencoded.png" width="72.5" height="34">
180221
181-
#### ♾️ TeX Options
222+
#### ♾️ TeX Options (deprecated)
182223

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

@@ -215,3 +256,5 @@ VStack {
215256
}
216257
}
217258
```
259+
260+
Keep in mind that SVG data and images are rendered as a result of the view's environment, so it is important to call the `preload` method using the same values that will be used when drawing the view.

Sources/LaTeXSwiftUI/Extensions/EnvironmentValues+Extensions.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ private struct TeXInputProcessorOptionsKey: EnvironmentKey {
5050
static let defaultValue: TeXInputProcessorOptions = TeXInputProcessorOptions(loadPackages: TeXInputProcessorOptions.Packages.all)
5151
}
5252

53+
private struct ProcessEscapesKey: EnvironmentKey {
54+
static let defaultValue: Bool = false
55+
}
56+
57+
private struct EquationNumberModeKey: EnvironmentKey {
58+
static let defaultValue: LaTeX.EquationNumberMode = .none
59+
}
60+
61+
private struct EquationNumberStartKey: EnvironmentKey {
62+
static let defaultValue: Int = 1
63+
}
64+
65+
private struct EquationNumberOffsetKey: EnvironmentKey {
66+
static let defaultValue: CGFloat = 0.0
67+
}
68+
5369
extension EnvironmentValues {
5470

5571
/// The image rendering mode of this environment.
@@ -88,4 +104,24 @@ extension EnvironmentValues {
88104
set { self[TeXInputProcessorOptionsKey.self] = newValue }
89105
}
90106

107+
var processEscapes: Bool {
108+
get { self[ProcessEscapesKey.self] }
109+
set { self[ProcessEscapesKey.self] = newValue }
110+
}
111+
112+
var equationNumberMode: LaTeX.EquationNumberMode {
113+
get { self[EquationNumberModeKey.self] }
114+
set { self[EquationNumberModeKey.self] = newValue }
115+
}
116+
117+
var equationNumberStart: Int {
118+
get { self[EquationNumberStartKey.self] }
119+
set { self[EquationNumberStartKey.self] = newValue }
120+
}
121+
122+
var equationNumberOffset: CGFloat {
123+
get { self[EquationNumberOffsetKey.self] }
124+
set { self[EquationNumberOffsetKey.self] = newValue }
125+
}
126+
91127
}

Sources/LaTeXSwiftUI/Extensions/View+Extensions.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ public extension View {
4747

4848
/// Unencodes HTML input text.
4949
///
50+
/// - Parameter unencode: Whether the variable should be set to true or false.
5051
/// - Returns: A view that displays unencoded text.
51-
func unencoded() -> some View {
52-
environment(\.unencodeHTML, true)
52+
func unencoded(_ unencode: Bool = true) -> some View {
53+
environment(\.unencodeHTML, unencode)
5354
}
5455

5556
/// Sets the parsing mode to use when parsing LaTeX input.
@@ -71,11 +72,34 @@ public extension View {
7172
/// Sets the TeX options for any images rendered with MathJax in this
7273
/// environment.
7374
///
75+
/// - Note: The environment values `processEscapes`, `equationTags`,
76+
/// `equationTagSide`, and `equationTagIndent` take precedence.
77+
///
7478
/// - Parameter options: The TeX input processor options.
7579
/// - Returns: A view that uses the given TeX input processor options to
7680
/// render images in its view.
81+
@available(*, deprecated, message: """
82+
This method will be unavailable in the next version. Use the `processEscapes`,
83+
`equationTags`, `equationTagSide`, and `equationTagIndent` methods instead.
84+
""")
7785
func texOptions(_ options: TeXInputProcessorOptions) -> some View {
7886
environment(\.texOptions, options)
7987
}
8088

89+
func processEscapes(_ process: Bool = true) -> some View {
90+
environment(\.processEscapes, process)
91+
}
92+
93+
func equationNumberMode(_ mode: LaTeX.EquationNumberMode) -> some View {
94+
environment(\.equationNumberMode, mode)
95+
}
96+
97+
func equationNumberStart(_ value: Int) -> some View {
98+
environment(\.equationNumberStart, value)
99+
}
100+
101+
func equationNumberOffset(_ offset: CGFloat) -> some View {
102+
environment(\.equationNumberOffset, offset)
103+
}
104+
81105
}

0 commit comments

Comments
 (0)