{"id":18762061,"url":"https://github.com/mgriebling/swiftmath","last_synced_at":"2025-04-13T03:32:05.460Z","repository":{"id":67642181,"uuid":"586241296","full_name":"mgriebling/SwiftMath","owner":"mgriebling","description":"SwiftMath provides a full Swift implementation of iosMath for displaying beautifully rendered math equations in iOS and MacOS applications. It typesets formulae written using LaTeX in a UILabel equivalent class.","archived":false,"fork":false,"pushed_at":"2024-01-25T12:34:54.000Z","size":2529,"stargazers_count":121,"open_issues_count":1,"forks_count":19,"subscribers_count":6,"default_branch":"main","last_synced_at":"2024-04-25T01:08:20.007Z","etag":null,"topics":["ios","iosmath","latex","mac","math","swift","typeset"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mgriebling.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-07T12:53:12.000Z","updated_at":"2024-05-28T15:06:34.942Z","dependencies_parsed_at":"2023-03-15T01:05:32.175Z","dependency_job_id":"a3b9e7e0-f94e-4c90-bad0-113a9ce87e1b","html_url":"https://github.com/mgriebling/SwiftMath","commit_stats":null,"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgriebling%2FSwiftMath","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgriebling%2FSwiftMath/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgriebling%2FSwiftMath/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgriebling%2FSwiftMath/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mgriebling","download_url":"https://codeload.github.com/mgriebling/SwiftMath/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248659687,"owners_count":21141159,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ios","iosmath","latex","mac","math","swift","typeset"],"created_at":"2024-11-07T18:19:05.624Z","updated_at":"2025-04-13T03:32:04.822Z","avatar_url":"https://github.com/mgriebling.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SwiftMath\n\n`SwiftMath` provides a full Swift implementation of [iosMath](https://travis-ci.org/kostub/iosMath) \nfor displaying beautifully rendered math equations in iOS and MacOS applications. It typesets formulae written \nusing LaTeX in a `UILabel` equivalent class. It uses the same typesetting rules as LaTeX and\nso the equations are rendered exactly as LaTeX would render them.\n\nPlease also check out [SwiftMathDemo](https://github.com/mgriebling/SwiftMathDemo.git) for examples of how to use `SwiftMath`\nfrom SwiftUI.  \n\n`SwiftMath` is similar to [MathJax](https://www.mathjax.org) or\n[KaTeX](https://github.com/Khan/KaTeX) for the web but for native iOS or MacOS\napplications without having to use a `UIWebView` and Javascript. More\nimportantly, it is significantly faster than using a `UIWebView`.\n\n`SwiftMath` is a Swift translation of the latest `iosMath` v0.9.5 release but includes bug fixes\nand enhancements like a new \\lbar (lambda bar) character and cyrillic alphabet support.\nThe original `iosMath` test suites have also been translated to Swift and run without errors.\nNote: Error test conditions are ignored to avoid tagging everything with silly `throw`s.\nPlease let me know of any bugs or bug fixes that you find. \n\n`SwiftMath` prepackages everything needed for direct access via the Swift Package Manager.\n\n## Examples\nHere are screenshots of some formulae that were rendered with this library:\n\n```LaTeX\nx = \\frac{-b \\pm \\sqrt{b^2-4ac}}{2a}\n```\n\n![Quadratic Formula](img/quadratic-light.png#gh-light-mode-only) \n![Quadratic Formula](img/quadratic-dark.png#gh-dark-mode-only) \n\n```LaTeX\nf(x) = \\int\\limits_{-\\infty}^\\infty\\!\\hat f(\\xi)\\,e^{2 \\pi i \\xi x}\\,\\mathrm{d}\\xi\n```\n\n![Calculus](img/calculus-light.png#gh-light-mode-only) \n![Calculus](img/calculus-dark.png#gh-dark-mode-only) \n\n```LaTeX\n\\frac{1}{n}\\sum_{i=1}^{n}x_i \\geq \\sqrt[n]{\\prod_{i=1}^{n}x_i}\n```\n\n![AM-GM](img/amgm-light.png#gh-light-mode-only) \n![AM-GM](img/amgm-dark.png#gh-dark-mode-only) \n\n```LaTex\n\\frac{1}{\\left(\\sqrt{\\phi \\sqrt{5}}-\\phi\\\\right) e^{\\frac25 \\pi}}\n= 1+\\frac{e^{-2\\pi}} {1 +\\frac{e^{-4\\pi}} {1+\\frac{e^{-6\\pi}} {1+\\frac{e^{-8\\pi}} {1+\\cdots} } } }\n```\n\n![Ramanujan Identity](img/ramanujan-light.png#gh-light-mode-only) \n![Ramanujan Identity](img/ramanujan-dark.png#gh-dark-mode-only) \n\nMore examples are included in [EXAMPLES](EXAMPLES.md)\n\n## Fonts\nHere are previews of the included fonts:\n\n![](img/FontsPreview.png#gh-dark-mode-only) \n![](img/FontsPreviewLight.png#gh-light-mode-only) \n \n## Requirements\n`SwiftMath` works on iOS 11+ or MacOS 12+. It depends\non the following Apple frameworks:\n\n* Foundation.framework\n* CoreGraphics.framework\n* QuartzCore.framework\n* CoreText.framework\n\nAdditionally for iOS it requires:\n* UIKit.framework\n\nAdditionally for MacOS it requires:\n* AppKit.framework\n\n## Installation\n\n### Swift Package\n\n`SwiftMath` is available from [SwiftMath](https://github.com/mgriebling/SwiftMath.git). \nTo use it in your code, just add the https://github.com/mgriebling/SwiftMath.git path to\nXCode's package manager.\n\n## Usage\n\nThe library provides a class `MTMathUILabel` which is a `UIView` that\nsupports rendering math equations. To display an equation simply create\nan `MTMathUILabel` as follows:\n\n```swift\n\nimport SwiftMath\n\nlet label = MTMathUILabel()\nlabel.latex = \"x = \\\\frac{-b \\\\pm \\\\sqrt{b^2-4ac}}{2a}\"\n\n```\nAdding `MTMathUILabel` as a sub-view of your `UIView` will render the\nquadratic formula example shown above.\n\nThe following code creates a SwiftUI component called `MathView` encapsulating the MTMathUILabel:\n\n```swift\nimport SwiftUI\nimport SwiftMath\n\nstruct MathView: UIViewRepresentable {\n    var equation: String\n    var font: MathFont = .latinModernFont\n    var textAlignment: MTTextAlignment = .center\n    var fontSize: CGFloat = 30\n    var labelMode: MTMathUILabelMode = .text\n    var insets: MTEdgeInsets = MTEdgeInsets()\n    \n    func makeUIView(context: Context) -\u003e MTMathUILabel {\n        let view = MTMathUILabel()\n        return view\n    }\n    func updateUIView(_ view: MTMathUILabel, context: Context) {\n        view.latex = equation\n        view.font = MTFontManager().font(withName: font.rawValue, size: fontSize)\n        view.textAlignment = textAlignment\n        view.labelMode = labelMode\n        view.textColor = MTColor(Color.primary)\n        view.contentInsets = insets\n    }\n}\n```\n\nFor code that works with SwiftUI running natively under MacOS use the following:\n\n```swift\nimport SwiftUI\nimport SwiftMath\n\nstruct MathView: NSViewRepresentable {\n    var equation: String\n    var font: MathFont = .latinModernFont\n    var textAlignment: MTTextAlignment = .center\n    var fontSize: CGFloat = 30\n    var labelMode: MTMathUILabelMode = .text\n    var insets: MTEdgeInsets = MTEdgeInsets()\n    \n    func makeNSView(context: Context) -\u003e MTMathUILabel {\n        let view = MTMathUILabel()\n        return view\n    }\n    \n    func updateNSView(_ view: MTMathUILabel, context: Context) {\n        view.latex = equation\n        view.font = MTFontManager().font(withName: font.rawValue, size: fontSize)\n        view.textAlignment = textAlignment\n        view.labelMode = labelMode\n        view.textColor = MTColor(Color.primary)\n        view.contentInsets = insets\n    }\n}\n```\n\n### Included Features\nThis is a list of formula types that the library currently supports:\n\n* Simple algebraic equations\n* Fractions and continued fractions\n* Exponents and subscripts\n* Trigonometric formulae\n* Square roots and n-th roots\n* Calculus symbos - limits, derivatives, integrals\n* Big operators (e.g. product, sum)\n* Big delimiters (using \\\\left and \\\\right)\n* Greek alphabet\n* Combinatorics (\\\\binom, \\\\choose etc.)\n* Geometry symbols (e.g. angle, congruence etc.)\n* Ratios, proportions, percentages\n* Math spacing\n* Overline and underline\n* Math accents\n* Matrices\n* Equation alignment\n* Change bold, roman, caligraphic and other font styles (\\\\bf, \\\\text, etc.)\n* Most commonly used math symbols\n* Colors for both text and background\n\nNote: SwiftMath only supports the commands in LaTeX's math mode. There is\nalso no language support for other than west European langugages and some\nCyrillic characters. There would be two ways to support more languages:\n\n1) Find a math font compatible with `SwiftMath` that contains all the glyphs\nfor that language.\n2) Add support to `SwiftMath` for standard Unicode fonts that contain all\nlangauge glyphs.\n\nOf these two, the first is much easier.  However, if you want a challenge,\ntry to tackle the second option.\n\n### Example\n\nThe [SwiftMathDemo](https://github.com/mgriebling/SwiftMathDemo) is a SwiftUI version\nof the Objective-C demo included in `iosMath` that uses `SwiftMath` as a Swift package dependency.\n\n### Advanced configuration\n\n`MTMathUILabel` supports some advanced configuration options:\n\n##### Math mode\n\nYou can change the mode of the `MTMathUILabel` between Display Mode\n(equivalent to `$$` or `\\[` in LaTeX) and Text Mode (equivalent to `$`\nor `\\(` in LaTeX). The default style is Display. To switch to Text\nsimply:\n\n```swift\nlabel.labelMode = .text\n```\n\n##### Text Alignment\nThe default alignment of the equations is left. This can be changed to\ncenter or right as follows:\n\n```swift\nlabel.textAlignment = .center\n```\n\n##### Font size\nThe default font-size is 30pt. You can change it as follows:\n\n```swift\nlabel.fontSize = 25\n```\n##### Font\nThe default font is *Latin Modern Math*. This can be changed as:\n\n```swift\nlabel.font = MTFontManager.fontmanager.termesFont(withSize:20)\n```\n\nThis project has 12 fonts bundled with it, but you can use any OTF math\nfont. A python script is included that generates the `.plist` files \nrequired for an `.otf` font to work with `SwiftMath`.  If you generate\n(and test) any other fonts please contribute them back to this project for\nothers to benefit.\n\n\n\nNote: The `KpMath-Light`, `KpMath-Sans`, `Asana` fonts currently incorrectly\nrender very large radicals. It appears that the font files do\nnot properly define the offsets required to typeset these glyphs.  If\nanyone can fix this, it would be greatly appreciated.\n\n##### Text Color\nThe default color of the rendered equation is black. You can change\nit to any other color as follows:\n\n```swift\nlabel.textColor = .red\n```\n\nIt is also possible to set different colors for different parts of the\nequation. Just access the `displayList` field and set the `textColor`\nof the underlying displays of which you want to change the color. \n\n##### Custom Commands\nYou can define your own commands that are not already predefined. This is\nsimilar to macros is LaTeX. To define your own command use:\n\n```swift\nMTMathAtomFactory.addLatexSymbol(\"lcm\", value: MTMathAtomFactory.operator(withName: \"lcm\", limits: false))\n```\n\nThis creates an `\\lcm` command that can be used in the LaTeX.\n\n##### Content Insets\nThe `MTMathUILabel` has `contentInsets` for finer control of placement of the\nequation in relation to the view.\n\nIf you need to set it you can do as follows:\n\n```swift\nlabel.contentInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 20)\n```\n\n##### Error handling\n\nIf the LaTeX text given to `MTMathUILabel` is\ninvalid or if it contains commands that aren't currently supported then\nan error message will be displayed instead of the label.\n\nThis error can be programmatically retrieved as `label.error`. If you\nprefer not to display anything then set:\n\n```swift\nlabel.displayErrorInline = true\n```\n\n## Future Enhancements\n\nNote this is not a complete implementation of LaTeX math mode. There are\nsome important pieces that are missing and will be included in future\nupdates. This includes:\n\n* Support for explicit big delimiters (bigl, bigr etc.)\n* Addition of missing plain TeX commands \n\n## License\n\n`SwiftMath` is available under the MIT license. See the [LICENSE](./LICENSE)\nfile for more info.\n\n### Fonts\nThis distribution contains the following fonts. These fonts are\nlicensed as follows:\n* Latin Modern Math: \n    [GUST Font License](GUST-FONT-LICENSE.txt)\n* Tex Gyre Termes:\n    [GUST Font License](GUST-FONT-LICENSE.txt)\n* [XITS Math](https://github.com/khaledhosny/xits-math):\n    [Open Font License](OFL.txt)\n* [KpMath Light/KpMath Sans](http://scripts.sil.org/OFL):\n    [SIL Open Font License](OFL.txt)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmgriebling%2Fswiftmath","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmgriebling%2Fswiftmath","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmgriebling%2Fswiftmath/lists"}