Skip to content

Commit f247de5

Browse files
committed
Remove tex options.
1 parent 785bdfd commit f247de5

File tree

4 files changed

+60
-141
lines changed

4 files changed

+60
-141
lines changed

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@ A SwiftUI view that renders LaTeX equations.
2121
- [Equation Number Start](#equation-number-start)
2222
- [Equation Number Offset](#equation-number-offset)
2323
- [Unencode HTML](#🔗-unencode-html)
24-
- [TeX Options](#♾️-tex-options (deprecated))
2524
- [Caching](#🗄️-caching)
2625
- [Preloading](#🏃‍♀️-preloading)
2726

2827
## ℹ️ About
2928

30-
`LaTexSwiftUI` is a package that exposes a view named `LaTeX` that can parse and render TeX and LaTeX equations that contain math-mode marcos.
29+
`LaTexSwiftUI` is a package that exposes a view named `LaTeX` that can parse and render TeX and LaTeX equations which contain math-mode marcos.
3130

3231
The view utilizes the [MathJaxSwift](https://www.github.com/colinc86/MathJaxSwift) package to render equations with [MathJax](https://www.mathjax.org). Thus, the limitations of the view are heavily influenced by the [limitations](https://docs.mathjax.org/en/v2.7-latest/tex.html#differences) of MathJax.
3332

3433
It will
3534
- render TeX and LaTeX equations (math-mode macros),
36-
- and render the `\text{}` macro within equations.
35+
- render the `\text{}` macro within equations,
36+
- attempt to render block equations as a Tex or LaTeX engine would,
37+
- and number block equations (if desired).
3738

3839
It won't
3940
- render TeX and LaTeX documents (text-mode macros, with the exception of the rule above).
@@ -248,13 +249,18 @@ LaTeX.imageCache.removeAll()
248249

249250
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.
250251

252+
SVGs and images are rendered as a result of the view's environment, so it is important to call the `preload` method last in the view's modifier chain if you use it.
253+
251254
```swift
252255
VStack {
253256
ForEach(expressions, id: \.self) { expression in
254257
LaTeX(expression)
258+
.font(.caption2)
259+
.foregroundColor(.green)
260+
.unencoded()
261+
.errorMode(.error)
262+
.processEscapes()
255263
.preload()
256264
}
257265
}
258266
```
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: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ private struct BlockModeKey: EnvironmentKey {
4646
static let defaultValue: LaTeX.BlockMode = .blockViews
4747
}
4848

49-
private struct TeXInputProcessorOptionsKey: EnvironmentKey {
50-
static let defaultValue: TeXInputProcessorOptions = TeXInputProcessorOptions(loadPackages: TeXInputProcessorOptions.Packages.all)
51-
}
52-
5349
private struct ProcessEscapesKey: EnvironmentKey {
5450
static let defaultValue: Bool = false
5551
}
@@ -98,12 +94,6 @@ extension EnvironmentValues {
9894
set { self[BlockModeKey.self] = newValue }
9995
}
10096

101-
/// The TeX options of this environment.
102-
var texOptions: TeXInputProcessorOptions {
103-
get { self[TeXInputProcessorOptionsKey.self] }
104-
set { self[TeXInputProcessorOptionsKey.self] = newValue }
105-
}
106-
10797
/// The processEscapes value of this environment.
10898
var processEscapes: Bool {
10999
get { self[ProcessEscapesKey.self] }

Sources/LaTeXSwiftUI/Extensions/View+Extensions.swift

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,31 @@ import SwiftUI
2828

2929
public extension View {
3030

31+
/// Preloads a `LaTeX` view's SVG and image data.
32+
///
33+
/// This method should be called last in the view's modifier chain (if at
34+
/// all).
35+
///
36+
/// - Example:
37+
///
38+
/// ```
39+
/// LaTeX("Hello, $\\LaTeX$!")
40+
/// .font(.title)
41+
/// .processEscapes()
42+
/// .preload()
43+
/// ```
44+
///
45+
/// - Note: If the receiver isn't a `LaTeX` view, then this method does
46+
/// nothing.
47+
///
48+
/// - Returns: A preloaded view.
49+
@MainActor func preload() -> some View {
50+
if let latex = self as? LaTeX {
51+
latex.preload()
52+
}
53+
return self
54+
}
55+
3156
/// Sets the image rendering mode for images rendered by MathJax.
3257
///
3358
/// - Parameter mode: The template rendering mode.
@@ -69,24 +94,6 @@ public extension View {
6994
environment(\.blockMode, mode)
7095
}
7196

72-
/// Sets the TeX options for any images rendered with MathJax in this
73-
/// environment.
74-
///
75-
/// - Note: The environment values `processEscapes`, `equationTags`,
76-
/// `equationTagSide`, and `equationTagIndent` take precedence.
77-
///
78-
/// - Parameter options: The TeX input processor options.
79-
/// - Returns: A view that uses the given TeX input processor options to
80-
/// render images in its view.
81-
@available(*, deprecated, message: """
82-
This method will be unavailable in the next version. Use the `processEscapes`,
83-
`equationNumberMode`, `equationNumberStart`, and `equationNumberOffset` methods
84-
instead.
85-
""")
86-
func texOptions(_ options: TeXInputProcessorOptions) -> some View {
87-
environment(\.texOptions, options)
88-
}
89-
9097
/// When set to `true`, you may use `\$` to represent a literal dollar sign,
9198
/// rather than using it as a math delimiter, and `\\` to represent a literal
9299
/// backslash (so that you can use `\\\$` to get a literal `\$` or `\\$...$`

Sources/LaTeXSwiftUI/LaTeX.swift

Lines changed: 24 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,6 @@ public struct LaTeX: View {
108108
/// The view's block rendering mode.
109109
@Environment(\.blockMode) private var blockMode
110110

111-
/// The TeX options to pass to MathJax.
112-
@Environment(\.texOptions) private var texOptions
113-
114111
/// Whether the view should process escapes.
115112
@Environment(\.processEscapes) private var processEscapes
116113

@@ -132,9 +129,11 @@ public struct LaTeX: View {
132129
/// The text's line spacing.
133130
@Environment(\.lineSpacing) private var lineSpacing
134131

135-
/// The blocks to render.
136-
private var blocks: [ComponentBlock] {
137-
let options = texOptions
132+
/// The TeX options to use when submitting requests to the renderer.
133+
private var texOptions: TeXInputProcessorOptions {
134+
let options = TeXInputProcessorOptions()
135+
options.processEscapes = processEscapes
136+
138137
var packages = TeXInputProcessorOptions.Packages.all
139138
if errorMode != .rendered {
140139
if let noErrorsIndex = packages.firstIndex(of: TeXInputProcessorOptions.Packages.noerrors) {
@@ -144,15 +143,17 @@ public struct LaTeX: View {
144143
packages.remove(at: noUndefinedIndex)
145144
}
146145
}
147-
148146
options.loadPackages = packages
149-
options.processEscapes = processEscapes
150-
151-
return Renderer.shared.render(
147+
return options
148+
}
149+
150+
/// The blocks to render.
151+
internal var blocks: [ComponentBlock] {
152+
Renderer.shared.render(
152153
blocks: Parser.parse(unencodeHTML ? latex.htmlUnescape() : latex, mode: parsingMode),
153154
font: font ?? .body,
154155
displayScale: displayScale,
155-
texOptions: options)
156+
texOptions: texOptions)
156157
}
157158

158159
// MARK: Initializers
@@ -183,102 +184,16 @@ public struct LaTeX: View {
183184

184185
extension LaTeX {
185186

186-
@MainActor public func preload(
187-
unencodeHTML: Bool = false,
188-
parsingMode: ParsingMode = .onlyEquations,
189-
errorMode: ErrorMode = .rendered,
190-
imageRenderingMode: Image.TemplateRenderingMode = .template,
191-
font: Font = .body,
192-
displayScale: CGFloat = 1.0,
193-
processEscapes: Bool
194-
) -> LaTeX {
195-
var packages = TeXInputProcessorOptions.Packages.all
196-
if errorMode != .rendered {
197-
if let noErrorsIndex = packages.firstIndex(of: TeXInputProcessorOptions.Packages.noerrors) {
198-
packages.remove(at: noErrorsIndex)
199-
}
200-
if let noUndefinedIndex = packages.firstIndex(of: TeXInputProcessorOptions.Packages.noundefined) {
201-
packages.remove(at: noUndefinedIndex)
202-
}
203-
}
204-
205-
let options = TeXInputProcessorOptions(loadPackages: packages)
206-
options.processEscapes = processEscapes
207-
208-
// Render the blocks
209-
let preloadedBlocks = Renderer.shared.render(
210-
blocks: Parser.parse(unencodeHTML ? latex.htmlUnescape() : latex, mode: parsingMode),
211-
font: font,
212-
displayScale: displayScale,
213-
texOptions: options)
214-
215-
// Render the images
216-
for block in preloadedBlocks {
217-
for component in block.components where component.type.isEquation {
218-
guard let svg = component.svg else { continue }
219-
_ = Renderer.shared.convertToImage(
220-
svg: svg,
221-
font: font,
222-
displayScale: displayScale,
223-
renderingMode: imageRenderingMode)
224-
}
225-
}
226-
return self
227-
}
228-
229-
/// Preloads the view's components.
230-
///
231-
/// - Note: You must set this method's parameters the same as its environment
232-
/// or call it is ineffective and adds additional computational overhead.
233-
///
234-
/// - Returns: A LaTeX view whose components have been preloaded.
235-
@available(*, deprecated, message: "This method will be unavailable in the next version.")
236-
@MainActor public func preload(
237-
unencodeHTML: Bool = false,
238-
parsingMode: ParsingMode = .onlyEquations,
239-
imageRenderingMode: Image.TemplateRenderingMode = .template,
240-
font: Font = .body,
241-
displayScale: CGFloat = 1.0,
242-
texOptions: TeXInputProcessorOptions? = nil
243-
) -> LaTeX {
244-
let options: TeXInputProcessorOptions
245-
if let texOptions {
246-
options = texOptions
247-
}
248-
else {
249-
var packages = TeXInputProcessorOptions.Packages.all
250-
if errorMode != .rendered {
251-
if let noErrorsIndex = packages.firstIndex(of: TeXInputProcessorOptions.Packages.noerrors) {
252-
packages.remove(at: noErrorsIndex)
253-
}
254-
if let noUndefinedIndex = packages.firstIndex(of: TeXInputProcessorOptions.Packages.noundefined) {
255-
packages.remove(at: noUndefinedIndex)
256-
}
257-
}
258-
259-
options = TeXInputProcessorOptions(loadPackages: packages)
260-
options.processEscapes = processEscapes
261-
}
262-
263-
// Render the blocks
264-
let preloadedBlocks = Renderer.shared.render(
265-
blocks: Parser.parse(unencodeHTML ? latex.htmlUnescape() : latex, mode: parsingMode),
266-
font: font,
267-
displayScale: displayScale,
268-
texOptions: options)
269-
270-
// Render the images
271-
for block in preloadedBlocks {
272-
for component in block.components where component.type.isEquation {
273-
guard let svg = component.svg else { continue }
274-
_ = Renderer.shared.convertToImage(
275-
svg: svg,
276-
font: font,
277-
displayScale: displayScale,
278-
renderingMode: imageRenderingMode)
279-
}
187+
/// Preloads the view's SVG and image data.
188+
@MainActor public func preload() {
189+
switch blockMode {
190+
case .alwaysInline:
191+
blocksAsText(blocks, forceInline: true)
192+
case .blockText:
193+
blocksAsText(blocks)
194+
case .blockViews:
195+
blocksAsStack(blocks)
280196
}
281-
return self
282197
}
283198

284199
}
@@ -298,8 +213,8 @@ extension LaTeX {
298213
blocks.map { block in
299214
let text = text(for: block)
300215
return block.isEquationBlock && !forceInline ?
301-
Text("\n") + text + Text("\n") :
302-
text
216+
Text("\n") + text + Text("\n") :
217+
text
303218
}.reduce(Text(""), +)
304219
}
305220

@@ -414,6 +329,7 @@ struct LaTeX_Previews: PreviewProvider {
414329
LaTeX("Hello, $\\LaTeX$!")
415330
.font(.caption2)
416331
.foregroundColor(.purple)
332+
.preload()
417333
}
418334
.fontDesign(.serif)
419335
.previewLayout(.sizeThatFits)

0 commit comments

Comments
 (0)