将 LaTeX 嵌入网页的最佳方式是什么?
我不是在问将 LaTeX 文档转换为 html。
我希望能够做的是有某种方法在 html 文档中使用 LaTeX 数学命令,并使其在浏览器中正确显示。 这可以在服务器端或客户端完成。
I'm not asking about converting a LaTeX document to html.
What I'd like to be able to do is have some way to use LaTeX math commands in an html document, and have it appear correctly in a browser. This could be done server or client side.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
MediaWiki 可以满足您的需求。 它使用 Texvc (http://en.wikipedia.org/wiki/Texvc) 来“验证(AMS) LaTeX 数学表达式并将其转换为 HTML、MathML 或 PNG 图形。” 听起来像是您正在寻找的东西。
在这里查看维基百科关于如何处理数学方程的文章:http://en.wikipedia.org/wiki /帮助:公式。 他们还对 LaTeX 以及不同渲染类型(PNG/MathML/HTML)的优缺点进行了广泛的参考。
MediaWiki can do what you are looking for. It uses Texvc (http://en.wikipedia.org/wiki/Texvc) which "validates (AMS) LaTeX mathematical expressions and converts them to HTML, MathML, or PNG graphics." Sounds like what you are looking for.
Check out Wikipedia's article on how they handle math equations here: http://en.wikipedia.org/wiki/Help:Formula. They also have an extensive reference on LaTeX and pros/cons of the different rendering types (PNG/MathML/HTML).
我曾经开发过一个 jQuery 插件,它实际上是这样做的: jsLaTeX
这是如何使用它的最简单示例:
上面将在您的页面上生成以下 LaTeX 方程:
该插件的演示页面包含更多代码示例和演示。
I once developed a jQuery plugin that does in fact this: jsLaTeX
Here's the simplest example of how it can be used:
The above will generate the following LaTeX equation on your page:
The Demo Page of the plugin contains more code examples and demos.
我更喜欢 MathJax 而不是选择渲染图像(这会导致锯齿问题)的解决方案。
MathJax 是一个开源的 Javascript 数学渲染引擎。
它使用 CSS 和 Webfonts 而不是图像或 flash,并且可以渲染 LaTeX 或 MathML。
这样您就不会遇到缩放问题,甚至与屏幕阅读器兼容。
I prefer MathJax over solutions that choose to render images (which causes aliasing problems).
MathJax is an open source Javascript rendering engine for mathematics.
It uses CSS and Webfonts instead of images or flash and can render LaTeX or MathML.
That way you don't have problems with zoom and it's even screenreader compatible.
这个问题只有一个(非常好的)答案引用了 KaTeX,根据我的经验,这是最有效的解决方案,加载速度快且易于实施。
您需要向
添加一个
标记(用于样式表)和两个
。 然后使用
\( \)
作为内联数学的分隔符,使用\[ \]
作为内显示数学的分隔符。
这是一个带有实现的最小 html5 文件:
Only one (very good) answer to this question cites KaTeX that, in my experience, is the most effective solution, loading fast and easy to implement.
You need to add one
<link>
tag (for the stylesheet) and two<script>
s to your<head>
. Then use\( \)
as delimiters for inline math and\[ \]
as delimiters for displayed math within the<body>
.Here's a minimal html5 file with an implementation:
如果您想将数学嵌入为图像,您可以查看MathTran。
如果您希望将数学主要作为文本插入到页面中(仅在必要时使用图像),jsMath 可能就是您正在寻找的。
If you want to embed the mathematics as images, you may take a look at MathTran.
If you'd prefer to have the math inserted into the page primarily as text (using images only when necessary), jsMath may be what you're looking for.
从历史上看,渲染 LaTeX 并提取图像一直是跨平台、跨浏览器数学内容的最佳选择。 MathML 越来越成为一种合理的选择。 这里是一个在线转换器,它将从 Tex 标记发出 MathML,您可以使用它然后可以嵌入到您的网页中。 我知道基于 Gecko 的浏览器(例如 Firefox 和 Camino)与 MathML 配合得很好,Opera 也是如此。 IE 不能开箱即用,但有可用的插件(例如 这个) 。
Texvc 是一个很棒的发现! 如果您对上标/下标/斜体/常见符号最感兴趣,则普通 HTML 输出应该可以很好地工作,但对于更复杂的事情,请注意最流行的数学导向网站(例如 Wolfram) 生成图像,因此如果您对跨浏览器兼容性感兴趣,您能做的可能只有这么多:-(
Historically, rendering the LaTeX and extracting an image has been your best bet for cross-platform, cross-browser math stuff. More and more, MathML is becoming a reasonable alternative. Here's an online converter that will emit MathML from Tex markup, which you can then embed in your webpage. I know Gecko-based browsers like Firefox and Camino play nice with MathML, as does Opera. IE doesn't work out of the box, but there are plugins available (like this one).
Texvc is a great find! The vanilla HTML output should work well if you're mostly interested in superscripts/subscripts/italics/common symbols, but for more complex things, be aware that the most popular math-oriented sites out there (e.g. Wolfram) generate images, so there may be only so much you can do if you're interested in cross-browser compatibility :-(
我自己开始研究这个问题,事情似乎已经发生了变化。 我遇到过 这个 的比较演示 KaTeX 和 MathJax。
长话短说(截至撰写本文时):
但也许最具有决定性的是,我发现页面的总 MathJax 处理在三次运行中平均为 1674 毫秒。 相比之下,KaTeX 的平均128 ms,要好一个数量级!
在浏览各自的网站时,还需要考虑其他一些比较点:
I'm starting to look into this myself and it seems things have evolved. I have come across this comparison demo of KaTeX and MathJax.
Long story short (as of this writing):
But perhaps most decisive of all, I found that the total MathJax processing for the page averaged to 1674 ms for three runs. In contrast, KaTeX averaged 128 ms, which is an order of magnitude better!
There are some other points of comparison to consider when looking through their respective websites:
您可以使用 tex2gif。 它需要一个 LaTeX 片段,运行 LaTeX 并生成一个 PNG(或 GIF)。 易于嵌入,易于编写脚本。 这个对我有用。
您还可以检查 tex2png。
You can use tex2gif. It takes a LaTeX snippet, runs LaTeX and produces a PNG (or GIF). Easy to embed, easy to script. It works for me.
You can also check tex2png.
我不是最有经验的程序员,但我找到了另一种解决方案来解决在网页中放置 LaTex 方程的问题。
首先,让我们看一下:
echo "
";
echo "此 div 内有一些内容。";
回显“
”;
?>
以上是部分内容的划分。 使用 CSS div 'test1' 可以放置在网页中的任何位置。
现在考虑以下事项; 我知道这很不寻常,但请看:
echo "
";
?>
在第二个示例中,div 被分解; 它从顶部的 php 开始,然后 php 结束; 然后用 HTML 编写 LaTex 方程。 然后 php 恢复并且 div 结束。
HTML 位于 div 内。 它可以使用 CSS 放置在网页中的任何位置,并且您正在 HTML 中编写纯 LaTex,因此您知道它会是什么样子。 以这种方式定位 LaTex 对我来说很有效——祝你好运!
I am not the most experienced programmer, but I have found another solution to the issue of placing LaTex equations in a webpage.
First, let us look at this:
<?php
echo "<div id='test1'>";
echo "Some content is within this div.";
echo "</div>";
?>
The above is a division with some content. Using CSS div 'test1' can be placed anywhere in a webpage.
Now consider the following; I know it is unusual but please look:
<?php
echo "<div id='test2'>";
?>
In the second example the div is broken up; it begins in php on the top then php ends; a LaTex equation is then written in HTML. Then php resumes and the div is ended.
The HTML is within the div. It can be placed anywhere in a webpage using CSS, and you are writing pure LaTex in HTML so you know what it will look like. Positioning LaTex in this way works for me--good luck!
您可以尝试 LaTexRenderer。 我不知道这是否是最好的,但它确实有效。
You could try LaTexRenderer. I don't know if it's the best, but it does work.
如果 MathML 适合您正在寻找的内容,我绝对会鼓励您查看它,但需要使用 JsTeX 可以为您提供所需的一切。
I would definitely encourage you to look at MathML if that fits what you're looking for but a little work with JsTeX could give you everything you need.
我阅读了这里所有的答案,令我惊讶的是没有人提到从 PDF 到 HTML 的转换。
如果您使用 pdf2htmlEX 它将从 pdf 创建完美的网页。 您只需将 Latex 编译为 pdf (pdflatex)。
默认情况下,它会生成一个 html 文件,其中 PDF 的内容由 CSS、javascript 和 html 组成。 我尝试了很多工具将 Latex 转换为 html,这是迄今为止我发现的最好和最简单的解决方案。
I read all the answers here, and I'm surprised no one mentioned the convertion from PDF to HTML.
If you use pdf2htmlEX it will create perfect webpages from a pdf. You just have to compile your latex to pdf (pdflatex).
By default it generates a single html file, with the contents of your PDF made out of CSS, javascript and html. I tried a lot of tools to convert latex to html and this is by far the best and easiest solution I found.