Html 网络上的数学方程式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/775168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Math equations on the web
提问by Justin R.
How can I render Math equations on the web? I am already familiar with LaTeX's Math mode.
如何在网络上呈现数学方程?我已经熟悉了 LaTeX 的数学模式。
采纳答案by simon
It turns out this is a bit of a pain.
事实证明这有点痛苦。
You can use MathML, but browser support is still iffy. If you are starting with latex you've got a few options for converting to html, but they'll all typically end up rendering the actual equations to images and inlining those.
您可以使用MathML,但浏览器支持仍然不确定。如果您从 Latex 开始,您有一些转换为 html 的选项,但它们通常最终都会将实际方程渲染为图像并内联这些。
Nothings all that pretty (unless you resort to pdf or something). What's best will depend a bit on what sort of content, how many equations, and how complicated the equations are.
没什么那么漂亮(除非你求助于pdf或其他东西)。什么是最好的将在一定程度上取决于内容的种类、方程式的数量以及方程式的复杂程度。
Hereis a decent summary.
这是一个体面的总结。
回答by Colonel Panic
The other answers are out-of-date. As of 2012, beautiful math is easyto write and render. The technology is called MathJax. You can see it in quiet action on MathOverflowand hundreds of math blogs.
其他答案已过时。截至 2012 年,漂亮的数学很容易编写和呈现。该技术称为MathJax。您可以在MathOverflow和数百个数学博客上以安静的方式看到它。
MathJax is an open source JavaScript display engine for mathematics that works in all modern browsers. No more setup for readers. No more browser plugins. No more font installations… It just works.
MathJax 是一个开源的 JavaScript 数学显示引擎,适用于所有现代浏览器。没有更多的读者设置。没有更多的浏览器插件。没有更多的字体安装......它只是有效。
Mathjax is reliable and unobtrusive, so you just need to write the math. You do so in Tex (Latex), a concise syntax with which most scientists and mathematicians are familiar (and have shared decades of good tutorials). For Mathjax, you simply write Tex code in-line in your HTML between double dollar signs, eg.
Mathjax 可靠且不引人注目,因此您只需要编写数学即可。您在 Tex (Latex) 中这样做,这是大多数科学家和数学家都熟悉的简洁语法(并且分享了数十年的优秀教程)。对于 Mathjax,您只需在 HTML 中的双美元符号之间编写 Tex 代码,例如。
When $$a \ne 0$$, there are two solutions to $$ax^2 + bx + c = 0$$ and they are $$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$
当 $$a \ne 0$$ 时,$$ax^2 + bx + c = 0$$ 有两种解,它们是 $$x = {-b \pm \sqrt{b^2-4ac} \超过 2a}.$$
To use Mathjax to render your math, put a Javascript line in your HTML header:
要使用 Mathjax 来呈现您的数学,请在您的 HTML 标题中放置一行 Javascript:
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js"></script>
If you publish on a platform such as Wordpress, Tumblr or Blogger there are plug-ins in their galleries to do this (Wordpress).
如果您在 Wordpress、Tumblr 或 Blogger 等平台上发布,则它们的图库中有插件可以执行此操作 ( Wordpress)。
How does Mathjax render math? With Javascript it renders your math to beautiful HTML and CSS (remarkably resembling Latex) in a fraction of a second. If a browser supports MathML, it can render math through that too, but that's not important. It's a popular success because the end-user workflow is easy, not because of the technology behind it.
Mathjax 如何渲染数学?使用 Javascript,它可以在几分之一秒内将您的数学呈现为漂亮的 HTML 和 CSS(非常类似于 Latex)。如果浏览器支持 MathML,它也可以通过它呈现数学,但这并不重要。这是一个受欢迎的成功,因为最终用户的工作流程很简单,而不是因为它背后的技术。
You can choose to use Mathjax (over png images) on Wikipedia if you have an account. Look for Special:Preferences
/ Appearance.
如果你有一个帐户,你可以选择在维基百科上使用 Mathjax(在 png 图像上)。寻找Special:Preferences
/外观。
MathML is ridiculous. It's neither human-readable nor human-writable (the quadratic equation takes 800 characters- it's 50 in Tex). It's just another pointless XML language. Thankfully, it's obsolete before most browsers support it. It doesn't even lookas good as Tex or Mathjax's HTML-CSS!
MathML 很荒谬。它既不是人类可读的,也不是人类可写的(二次方程需要800 个字符- 在 Tex 中是 50 个)。它只是另一种毫无意义的 XML 语言。值得庆幸的是,在大多数浏览器支持它之前它已经过时了。它甚至没有看起来那么好tex或Mathjax的HTML,CSS!
回答by jmbr
My two favorite approaches:
我最喜欢的两种方法:
Client-side: MathJax. See some examples here. It is very easy to use and install and its development is backed by the AMS and SIAMamong other scientific institutions. I expect this to become the defacto standard for displaying math on the Web.
Server-side: LaTeXML. This is used for producing the NIST Digital Library of Mathematical Functions. It tends to hiccup if you have custom macros in your TeX sources but in general it does give very good results.
客户端:MathJax。请参阅此处的一些示例。它非常易于使用和安装,其开发得到了 AMS 和 SIAM以及其他科学机构的支持。我希望这将成为在 Web 上显示数学的事实标准。
服务器端:LaTeXML。这用于生成NIST 数学函数数字库。如果您的 TeX 源中有自定义宏,它往往会打嗝,但总的来说,它确实提供了非常好的结果。
回答by Fanfan
The jsMathpackage is another option that uses LaTeX markup and native fonts. Quoting from their webpage http://www.math.union.edu/~dpvc/jsMath/:
该jsMath包是另一种选择是使用乳胶标记和本地字体。引用他们的网页http://www.math.union.edu/~dpvc/jsMath/:
The jsMath package provides a method of including mathematics in HTML pages that works across multiple browsers under Windows, Macintosh OS X, Linux and other flavors of unix. It overcomes a number of the shortcomings of the traditional method of using images to represent mathematics: jsMath uses native fonts, so they resize when you change the size of the text in your browser, they print at the full resolution of your printer, and you don't have to wait for dozens of images to be downloaded in order to see the mathematics in a web page. There are also advantages for web-page authors, as there is no need to preprocess your web pages to generate any images, and the mathematics is entered in TeX form, so it is easy to create and maintain your web pages.
jsMath 包提供了一种在 HTML 页面中包含数学的方法,该方法可在 Windows、Macintosh OS X、Linux 和其他类型的 unix 下的多个浏览器中工作。它克服了使用图像表示数学的传统方法的许多缺点:jsMath 使用本机字体,因此当您在浏览器中更改文本大小时,它们会调整大小,以打印机的全分辨率打印,并且您不必等待下载数十张图像才能在网页中看到数学。网页作者也有优势,因为不需要预处理你的网页来生成任何图像,而且数学是以 TeX 形式输入的,所以很容易创建和维护你的网页。
回答by Joshua Pinter
回答by John D. Cook
You can do more math directly in HTML than most people realize. See these notes.
您可以直接在 HTML 中进行比大多数人意识到的更多的数学运算。请参阅这些说明。
The only safe way to render LaTeX is to save the output as an image. Some sites try to use tools to do this on the fly, and they never work reliably. For example, on some blogs, this works if you visit the web page directly but not if you go through Feedburner/Google Reader.
渲染 LaTeX 的唯一安全方法是将输出保存为图像。一些站点尝试使用工具来即时执行此操作,但它们永远无法可靠地工作。例如,在某些博客上,如果您直接访问网页,则此方法有效,但如果您通过 Feedburner/Google 阅读器访问,则此方法无效。
I've had terrible experience with MathML browser support, both in Firefox and IE. Don't even try it. Not yet. Maybe in a few years.
我在 Firefox 和 IE 中对 MathML 浏览器的支持有过糟糕的经历。甚至不要尝试。还没有。也许几年后。
Here's the siteI use to compile LaTeX to gifs.
这是我用来将 LaTeX 编译为 gif的站点。
If you're willing to use PDF instead of HTML, things get much easier. Just create your LaTeX document and use pdflatex
to compile it to PDF. If you do go the PDF route, you may be interested in how to include PDF propertiessuch as author, keywords, etc. in your LaTeX file. Also, this pageexplains how to mark up the LaTeX to make links in your PDF.
如果您愿意使用 PDF 而不是 HTML,事情会变得容易得多。只需创建您的 LaTeX 文档并用于pdflatex
将其编译为 PDF。如果您确实采用 PDF 路线,您可能会对如何在 LaTeX 文件中包含 PDF 属性(如作者、关键字等)感兴趣。此外,本页还解释了如何标记 LaTeX 以在 PDF 中建立链接。
回答by Stefano Borini
I do render LaTeX formulas "on demand" in my wiki. Basically, I extract the latex code from each wiki section and put it into a .tex file (whose filename is an md5sum of the latex, so if the same code is used again, the same tex and therefore the same image will be used). The tex file is then latex compiled by a cron task every minute, to produce first a .ps, then with the convert program a .png (named again with the original md5). The wiki entry replaces the latex text with an img tag referring to this png (with the original latex code as an alt, for text readers).
我确实在我的 wiki 中“按需”渲染 LaTeX 公式。基本上,我从每个 wiki 部分提取乳胶代码并将其放入一个 .tex 文件(其文件名是乳胶的 md5sum,因此如果再次使用相同的代码,将使用相同的 tex,因此将使用相同的图像) . 然后每分钟由 cron 任务对 tex 文件进行乳胶编译,首先生成一个 .ps,然后使用转换程序生成一个 .png(再次使用原始 md5 命名)。wiki 条目将乳胶文本替换为引用此 png 的 img 标签(对于文本阅读器,使用原始乳胶代码作为 alt)。
If you want to go this way, be verycareful to sanitize your latex as much as you can. there are commands in latex, like \input, that you definitely do not want to let go through, as anybody able to use them would be able to include any readable file in your server disk and include it in the resulting latex output.
如果你想要走这条路,要非常小心地净化你的乳胶不亚于您可以。Latex 中有一些命令,例如 \input,您绝对不想让它通过,因为任何能够使用它们的人都可以在您的服务器磁盘中包含任何可读文件,并将其包含在生成的 Latex 输出中。
To solve this issue, Mediawiki (of wikipedia fame) has a special plugin which sanitizes the latex input, but I didn't want to use it for two reasons: first I did not use mediawiki, second it's written in OCaml and I didn't want to mess with a language I don't know.
为了解决这个问题,Mediawiki(因维基百科而闻名)有一个特殊的插件可以清理乳胶输入,但我不想使用它有两个原因:首先我没有使用 mediawiki,其次它是用 OCaml 编写的,我没有不想惹我不懂的语言。
回答by jfclavette
texvc can convert LaTeX math equations to png or HTML.
texvc 可以将 LaTeX 数学方程转换为 png 或 HTML。
回答by Jeremy
LaTeX and MathMLare the only "right" ways to do this. However each has severe limitations. The other options are images (not really optimal if you need to edit the equations later) or complex HTML(requires some training but can be done).
LaTeX 和MathML是唯一“正确”的方法。然而,每个都有严重的局限性。其他选项是图像(如果您稍后需要编辑方程,则不是最佳选择)或复杂的 HTML(需要一些培训但可以完成)。
回答by Dave Barton
I've written an open source javascript module to do this, named jqmath. See http://mathscribe.com/author/jqmath.html. You type equations in a simplified TeX-like syntax, and jqmath converts them to MathML or simple HTML and CSS, depending on the browser. This is more efficient and accessible than using images.
我已经编写了一个开源 javascript 模块来执行此操作,名为 jqmath。请参阅http://mathscribe.com/author/jqmath.html。您以类似 TeX 的简化语法键入方程,jqmath 将它们转换为 MathML 或简单的 HTML 和 CSS,具体取决于浏览器。这比使用图像更有效、更易于访问。
By the way, some of the summaries and notes mentioned in the other answers here are pretty outdated now. Also, Firefox supports MathML now, and webkit (Chrome and Safari) have it in their nightly builds, though they haven't released it yet. Internet Explorer renders MathML if you have the MathPlayer plugin. Opera fakes MathML with a stylesheet. MathML is part of the HTML 5 standard, so presumably all these browsers will natively support it sooner rather than later. It's true that until then, jqmath's output will not look as good as TeX's, but it's certainly readable, and is definitely a better solution for web pages going forward.
顺便说一句,这里其他答案中提到的一些摘要和注释现在已经过时了。此外,Firefox 现在支持 MathML,而 webkit(Chrome 和 Safari)在他们的夜间构建中已经有了它,尽管他们还没有发布它。如果您有 MathPlayer 插件,Internet Explorer 会呈现 MathML。Opera 使用样式表伪造 MathML。MathML 是 HTML 5 标准的一部分,所以想必所有这些浏览器迟早都会原生支持它。诚然,在那之前,jqmath 的输出看起来不会像 TeX 那样好,但它肯定是可读的,并且对于未来的网页来说绝对是一个更好的解决方案。