colinc86 / LaTeXSwiftUI

A SwiftUI view that renders LaTeX.
MIT License
186 stars 30 forks source link

How do I scale the rendered equation to match the surrounding text? #23

Open OskarPersson opened 1 year ago

OskarPersson commented 1 year ago

Changing the font size doesn't affect the rendered equation:

import SwiftUI
import LaTeXSwiftUI

@main
struct iOSApp: App {
    var body: some Scene {
        WindowGroup {
            LaTeX("Solve the following: $(9-2)^2\\cdot9^2$")
            LaTeX("Solve the following: $(9-2)^2\\cdot9^2$").font(.system(size: 8))
        }
    }
}

Simulator Screenshot - iPhone 14 Pro - 2023-08-29 at 20 50 10

rrrpdx commented 9 months ago

Did you have any luck with this? I was able to change the font size within latex by, for example, doing $\\Huge\\pi$ ... I tried $\\fontsize{30}{36}\\pi$, but that didnt' seem to work.

colinc86 commented 6 months ago

The view needs to be able to measure/calculate the current font's x-height to correctly size the characters inside of the rendered LaTeX SVG. To do that, the view must use the UIFont/NSFont classes and do its best to convert SwiftUI's Font structure into the correct UIFont instance. Right now the only way I know how to do that is by limiting the functionality to SwiftUI's static largeTitle, title, title1, headline, etc fonts.

Long story short - if you use the statically declared fonts and not the .system(...) font initializers, then the rendered equation text should match.

I'm going to leave this issue open though because I think it's still worth investigating to come up with a better solution.

m-yeroshenko commented 5 months ago

Hi everyone! Are there any updates on this matter?