colinc86 / LaTeXSwiftUI

A SwiftUI view that renders LaTeX.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

LaTeX view not updating when string input updated

grdleo opened this issue · comments

Title is self explanatory. Take this code for example:

import SwiftUI
import LaTeXSwiftUI

struct TestView: View {
    @State var varLatex = "$a$"
    
    var body: some View {
        VStack {
            Text(varLatex)
            LaTeX(varLatex)
            Button("Switch") {
                varLatex = "$b$"
            }
        }
    }
}

#Preview {
    TestView()
}

When the button is clicked, varLatex is updated and so is the Text view. But the LaTeX view is not updated... I have tried on multiple examples: whenever the input is updated, the view does not. And there is no workaround...

It feels like the LaTeX only works with constant strings, since iterating with ForEach over a constant list of strings works perfectly.

My knowledge in Swift is too little for me to open a PR... I hope this can be fixed soon!

have same issue. Fix is highly demanded and appreciated

Same experience here.

However, @colinc86 I've figured out that this problem was introduced at some point after version 1.1.0 based on my having created a project in early June 2023 that used LaTeXSwiftUI, and having everything work as expected.

For example, the code below works with version 1.1.0:

import SwiftUI
import LaTeXSwiftUI

struct ContentView: View {
    
    @State var radius = 0.0
    
    var body: some View {
        VStack {
            Text("\(radius)")
            Slider(value: $radius, in: 0...100)
            LaTeX("$$A=\\pi ^2$$")
            LaTeX("$$A=\\pi (\(radius))^2$$")
        }
        .padding()
    }
}

With the latest version, 1.3.0, when you drag the slider, the view is not updated.

I hope this helps. Thank-you for developing this package – it's really useful for myself as a Computer Science and Mathematics teacher, and, for my students.

Follow-up: it is version 1.2.3 where the code in my comment above breaks. Any version lower than that and the LaTeX view updates as expected.

The past few months have been very busy for me, so I apologize for not keeping up with this as much as I would have liked. I've updated the package to version 1.3.1, and it should work now with the examples you all have provided (which I very much appreciate). Thank you for the feedback!

Completely understand about being busy – no need to apologize. Can confirm on my end as well that 1.3.1 works as expected, trying out the example I provided earlier. My students and I very much appreciate your efforts to build and maintain this package. Thanks!

No need to apologise indeed, thank you for this great package!