将LaTeX嵌入网页中的最佳方法是什么?

时间:2020-03-06 14:33:03  来源:igfitidea点击:

我不是在问将LaTeX文档转换为html的问题。

我想做的是有某种方法可以在html文档中使用LaTeX数学命令,并使其正确显示在浏览器中。这可以在服务器端或者客户端完成。

解决方案

MediaWiki可以满足需求。它使用Texvc(http://zh.wikipedia.org/wiki/Texvc)来"验证(AMS)LaTeX数学表达式并将其转换为HTML,MathML或者PNG图形"。听起来像我们要找的东西。

在此处查看Wikipedia上有关它们如何处理数学方程式的文章:http://en.wikipedia.org/wiki/Help:Formula。他们还对LaTeX和不同渲染类型(PNG / MathML / HTML)的优缺点有广泛的参考。

MediaWiki uses a subset of TeX markup,
  including some extensions from LaTeX
  and AMS-LaTeX, for mathematical
  formulae. It generates either PNG
  images or simple HTML markup,
  depending on user preferences and the
  complexity of the expression. In the
  future, as more browsers are smarter,
  it will be able to generate enhanced
  HTML or even MathML in many cases.
  (See blahtex for information about
  current work on adding MathML
  support.)
  
  More precisely, MediaWiki filters the
  markup through Texvc, which in turn
  passes the commands to TeX for the
  actual rendering. Thus, only a limited
  part of the full TeX language is
  supported; see below for details.
  ...
  
  Pros of HTML
  
  
  In-line HTML formulae always align properly with the rest of the HTML
  text.
  The formula's background, font size and face match the rest of HTML
  contents and the appearance respects
  CSS and browser settings.
  Pages using HTML will load faster.
  
  
  Pros of TeX
  
  
  TeX is semantically superior to HTML. In TeX, "x" means
  "mathematical variable x", whereas in
  HTML "x" could mean anything.
  Information has been irrevocably lost.
  This has multiple benefits:
  
  
  TeX can be transformed into HTML, but not vice-versa. This means that on
  the server side we can always
  transform a formula, based on its
  complexity and location within the
  text, user preferences, type of
  browser, etc. Therefore, where
  possible, all the benefits of HTML can
  be retained, together with the
  benefits of TeX. It's true that the
  current situation is not ideal, but
  that's not a good reason to drop
  information/contents. It's more a
  reason to help improve the situation.
  TeX can be converted to MathML for browsers which support it, thus
  keeping its semantics and allowing it
  to be rendered as a vector.
  
  TeX has been specifically designed for typesetting formulae, so input is
  easier and more natural, and output is
  more aesthetically pleasing.
  When writing in TeX, editors need not worry about browser support, since
  it is rendered into an image by the
  server. HTML formulae, on the other
  hand, can end up being rendered
  inconsistent of editor's intentions
  (or not at all), by some browsers or
  older versions of a browser.

从历史上看,渲染LaTeX和提取图像一直是跨平台,跨浏览器的数学知识的最佳选择。越来越多的MathML成为一种合理的选择。这是一个在线转换器,将从Tex标记中发出MathML,然后我们可以将其嵌入到网页中。我知道基于Firefox和Camino的基于Gecko的浏览器可以与MathML完美配合,Opera也可以。 IE不能立即使用,但是有可用的插件(例如此插件)。

Texvc是一个很棒的发现!如果我们最感兴趣的是上标/下标/斜体/常见符号,那么原始的HTML输出应该可以很好地工作,但是对于更复杂的事情,请注意,那里最受欢迎的面向数学的网站(例如Wolfram)会生成图像,因此如果我们对跨浏览器的兼容性感兴趣,那么我们可能只会做很多事情:-(

我们可以尝试LaTexRenderer。我不知道这是不是最好的,但是确实有效。

如果可以满足需求,我绝对会鼓励我们考虑使用MathML,但是与JsTeX一起工作可以为我们提供所需的一切。

如果要将数学作为图像嵌入,可以看一下MathTran。

如果我们希望将数学运算主要以文本形式(仅在必要时使用图片)插入页面,则jsMath可能就是我们想要的。

我们可以使用tex2gif。它需要一个LaTeX片段,运行LaTeX并生成一个PNG(或者GIF)。易于嵌入,易于编写脚本。这个对我有用。

我们也可以检查tex2png。

我曾经开发过一个jQuery插件,实际上可以做到这一点:jsLaTeX

这是如何使用它的最简单的示例:

$(".latex").latex();

<div class="latex">  
    \int_{0}^{\pi}\frac{x^{4}\left(1-x\right)^{4}}{1+x^{2}}dx =\frac{22}{7}-\pi  
</div>

上面的代码将在页面上生成以下LaTeX方程:

等式http://img16.imageshack.us/img16/5462/gifwm.gif

插件的演示页面包含更多代码示例和演示。