{"id":13693770,"url":"https://github.com/mmcloughlin/mathfmt","last_synced_at":"2025-05-08T21:24:30.448Z","repository":{"id":42708989,"uuid":"237814080","full_name":"mmcloughlin/mathfmt","owner":"mmcloughlin","description":"Document mathematical Go code beautifully","archived":false,"fork":false,"pushed_at":"2024-02-09T19:21:19.000Z","size":114,"stargazers_count":200,"open_issues_count":4,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-02T03:53:29.423Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mmcloughlin.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":"2020-02-02T18:11:42.000Z","updated_at":"2025-01-20T19:12:59.000Z","dependencies_parsed_at":"2024-01-13T22:55:14.412Z","dependency_job_id":"97c99ab4-9eac-4fff-ad5e-8e3082fbd75c","html_url":"https://github.com/mmcloughlin/mathfmt","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mmcloughlin%2Fmathfmt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mmcloughlin%2Fmathfmt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mmcloughlin%2Fmathfmt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mmcloughlin%2Fmathfmt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mmcloughlin","download_url":"https://codeload.github.com/mmcloughlin/mathfmt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238044093,"owners_count":19407128,"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":[],"created_at":"2024-08-02T17:01:17.186Z","updated_at":"2025-02-10T02:09:16.629Z","avatar_url":"https://github.com/mmcloughlin.png","language":"Go","funding_links":[],"categories":["开源类库","Open source library"],"sub_categories":["数学计算","Mathematical Calculations"],"readme":"# mathfmt\n\nDocument mathematical Go code beautifully.\n\n* Write mathematical formulae in a [LaTeX](https://en.wikipedia.org/wiki/LaTeX)-ish syntax\n* Super/subscripts formatted with Unicode characters: `2^32` becomes `2³²` and `x_{i+1}` becomes `xᵢ₊₁`\n* Comprehensive [symbol library](symbols.md): `\\zeta(s) = \\sum 1/n^{s}` becomes `ζ(s) = ∑ 1/nˢ`\n\nInspired by [Filippo Valsorda](https://filippo.io/)'s [literate Go\nimplementation of\nPoly1305](https://blog.filippo.io/a-literate-go-implementation-of-poly1305/), which can be [reproduced using `mathfmt`](testdata/poly1305.in).\n\n## Usage\n\nInstall `mathfmt` with:\n\n```\ngo get -u github.com/mmcloughlin/mathfmt\n```\n\nApply to files just like you would with `gofmt`.\n\n```\nmathfmt -w file.go\n```\n\n## Example\n\nHere's our variance function in Go, documented with LaTeX-ish equations in comments.\n\n[embedmd]:# (testdata/stats.in go /\\/\\/ Variance/ /^}/)\n```go\n// Variance computes the population variance of the population x_{i} of size N.\n// Specifically, it computes \\sigma^2 where\n//\n//\t\t\\sigma^2 = \\sum (x_{i} - \\mu)^2 / N\n//\n// See also: https://en.wikipedia.org/wiki/Variance.\nfunc Variance(X []float64) float64 {\n\t// Compute the average \\mu.\n\tmu := Mean(X)\n\n\t// Compute the sum \\sum (x_{i} - \\mu)^2.\n\tss := 0.0\n\tfor _, x := range X {\n\t\tss += (x - mu) * (x - mu) // (x_{i} - \\mu)^2\n\t}\n\n\t// Final divide by N to produce \\sigma^2.\n\treturn ss / float64(len(X))\n}\n```\n\nRun it through `mathfmt` and voila!\n\n[embedmd]:# (testdata/stats.golden go /\\/\\/ Variance/ /^}/)\n```go\n// Variance computes the population variance of the population xᵢ of size N.\n// Specifically, it computes σ² where\n//\n//\tσ² = ∑ (xᵢ - μ)² / N\n//\n// See also: https://en.wikipedia.org/wiki/Variance.\nfunc Variance(X []float64) float64 {\n\t// Compute the average μ.\n\tmu := Mean(X)\n\n\t// Compute the sum ∑ (xᵢ - μ)².\n\tss := 0.0\n\tfor _, x := range X {\n\t\tss += (x - mu) * (x - mu) // (xᵢ - μ)²\n\t}\n\n\t// Final divide by N to produce σ².\n\treturn ss / float64(len(X))\n}\n```\n\n## Syntax\n\nFirst a warning: `mathfmt` does not have a rigorous grammar, it's a\ncombination of string replacement and regular expressions that appears to\nwork most of time. However you may run into some [thorny edge\ncases](https://github.com/mmcloughlin/mathfmt/issues/9).\n\n### Source\n\n`mathfmt` only works on Go source code. _Every_ comment in the\nfile is processed, both single- and multi-line.\n\n### Symbols\n\n`mathfmt` recognizes a [huge symbol table](symbols.md) that is\nalmost entirely borrowed from LaTeX packages. Every symbol macro in comment\ntext will be replaced with its corresponding Unicode character. In addition\nto LaTeX symbol macros, `mathfmt` supports a [limited set of\n\"aliases\"](symbols.md#aliases) for character combinations commonly used to\nrepresent mathematical symbols.\n\n### Super/subscripts\n\nLike LaTeX, superscripts use the `^` character and subscripts use `_`. If the\nsuper/subscript consists entirely of digits, then no braces are required: for\nexample `2^128` or `x_13`. Otherwise braces must be used to surround the\nsuper/subscript, for example `2^{i}` or `x_{i+j}`.\n\nNote that Unicode support for super/subscripts is limited, and in particular\ndoes not support the full alphabet. Therefore, if there is not a\ncorresponding super/subscript character available for any character in braces\n`{...}`, `mathfmt` will not perform any substition at all. For example there\nis no superscript `q`, so `mathfmt` will not be able to process `2^{q}`, and\nlikewise with `x_{K}`.\n\n## Credits\n\nThank you to Günter Milde for the exhaustive [`unimathsymbols`\ndatabase](http://milde.users.sourceforge.net/LUCR/Math/) of Unicode symbols\nand corresponding LaTeX math mode commands.\n\n## License\n\n`mathfmt` is available under the [BSD 3-Clause License](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmmcloughlin%2Fmathfmt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmmcloughlin%2Fmathfmt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmmcloughlin%2Fmathfmt/lists"}