我一直在用Markdown做课堂笔记,它很棒。我甚至在Markdown上做了一些预处理,这样我就可以做表格之类的事情。但这学期我要教一门数学课程,我希望能够将LaTeX公式与Markdown一起使用,就像这样:

The refinement relation is written $a \sqsubseteq b$, which can be 
pronounced "$a$ approximates $b$" or "$b$ is at least as defined as $a$".

我希望能够将LaTeX的每个片段预处理为一个不错的反锯齿PNG文件,然后通过HTML <img>标记将其包含在我的Markdown中。但我完全不知道如何获取LaTeX的片段并得到一张漂亮的图像

有正确的边界框吗 是平滑

我只知道如何获得DVI、PostScript或PDF格式的完整页面。

我相信这个问题已经解决了,但我还没能猜出正确的搜索词。有什么建议如何解决它或在哪里寻找现有的解决方案?


编辑:在安装了mathTeX之后,我可以说代码是不灵活的,它违反了Linux文件系统层次结构标准,而且它是业余工作——从这个词的好坏两方面来看都是如此。代码非常复杂,没有明显的错误。我会寻找替代方案。

此外,很明显,在底层,解决方案是基于dvipng的。


一年后:我从来没有得到我一直希望的无缝整合,但我在自己设计的剧本上蹒跚前行。事实证明,使用dvips -E和ImageMagick的转换程序而不是dvipng更容易一些。这样做的好处是可以更好地控制缩放等事情,并且可以轻松地制作透明的背景。 好奇的人可以看看这个例子。

我不能向任何人推荐这个解决方案。但我也不推荐MathTeX。


当前回答

是的,但你得自己动手。我写了一个过滤器,用适当的图像标签替换乳胶标签$\some\inline\latex$或$$\some\equation$$到一个mimex .cgi脚本。总共花了5分钟。

警告:极其丑陋……

#!/usr/bin/env python
import sys, markdown,re

MIMETEX_LOC="http://some.server.com/cgi-bin/mimetex.cgi"

def sanitizeLatex(text):
    return re.sub(r"\\",r"%5C", text)

def wrapLatexBlock(text):
    return '<img alt="equation" class="block" src="%s?%s"></img>'%(MIMETEX_LOC,text)

def wrapLatexInline(text):
    return '<img alt="equation" class="inline" src="%s?%s"></img>'%(MIMETEX_LOC,text)

def prepLatexBlock(matchobj):
    return wrapLatexBlock(sanitizeLatex(matchobj.group()[2:-2]))

def prepLatexInline(matchobj):
    return wrapLatexInline(sanitizeLatex(matchobj.group()[1:-1]))


if __name__ == "__main__":
    # initialise markdown
    md=markdown.Markdown()
    raw_md=open(sys.argv[1],"r").read()

    ##
    # deal with embedded latex
    ##
    raw_md=re.sub(r'\$\$(.*?)\$\$',prepLatexBlock, raw_md)
    raw_md=re.sub(r'\$(.*?)\$',prepLatexInline, raw_md)

    ##
    # once latex is parsed, convert md to html
    ##
    main_html=md.convert(raw_md)

    # hey presto!
    print(main_html)

当然,你必须自己为.block和.inline图像定义适当的css…

其他回答

也许mathJAX就是门票。它构建在jsMath上,这是一个2004年的JavaScript库。

在2015年2月5日,我转而推荐KaTeX——可汗学院的性能最好的Javascript LaTeX库。

嘿,这可能不是最理想的解决方案,但对我来说很管用。我最终创建了一个Python-Markdown LaTeX扩展。

https://github.com/justinvh/Markdown-LaTeX

它使用$math$和%text%语法增加了对内联数学和文本表达式的支持。扩展是一个预处理器,将使用latex/dvipng为各自的方程/文本生成png,然后base64编码数据直接内联图像,而不是有外部图像。

然后将数据放入一个简单分隔的缓存文件中,该文件将表达式编码为base64表示。这限制了乳胶实际运行的次数。

这里有一个例子:

%Hello, world!% This is regular text, but this: $y = mx + b$ is not.

输出:

$ markdown -x latex test.markdown
<p><img class='latex-inline math-false' alt='Hello, world!' id='Helloworld' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFwAAAAQBAMAAABpWwV8AAAAMFBMVEX///8iIiK6urpUVFTu7u6YmJgQEBDc3NxERESqqqqIiIgyMjJ2dnZmZmbMzMwAAAAbX03YAAAAAXRSTlMAQObYZgAAAVpJREFUKM9jYICDOgb2BwzYAVji8AQg8fb/PZ79u4AMvv0Mrz/gUA6W8F7AmcLAsJuBYT7Y1PcMfLiUgyWYF/B8Z2DYAVReABKrZ2DHpZwdopzrA0nKOeHKj66CKOcKPQJWwJo2NVFhfwCQyymhYwCUYD0avIApgYFh2927/QUcE3gDwMpvMhRCDJzNMIPhKZg7UW8DUOIMg9sCPgGo6e8ZODeAlAP9xLEArNy/IIwhAMx9D3IM+3cgi70BqnxZaNQFkHJWAQbeBrByjgURExaAuc9AyjnB5hjAlEO9ygVXzrplpskEMPchQvkBmGMcGApgjjkAVs7yhyWVAcwFK2f/AlJeAI0m5gMsEK+aMhQ6aDuA1DcDIZirBg7IOwxlB5g2QBJBF8OZVUz95hqfC3hOXWGYrwBSHskwk4EByGXab8QAlOBaGizFKYAtUlgUGEgBTCSpZnDCLQUA+y6MXeYnPDgAAAAASUVORK5CYII='> This is regular text, but this: <img class='latex-inline math-true' alt='y = mx + b' id='ymxb' src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFIAAAAOBAMAAABOTlYkAAAAMFBMVEX///9ERETu7u4yMjK6urp2dnZUVFSIiIjMzMwQEBDc3NwiIiJmZmaYmJiqqqoAAADS00rKAAAAAXRSTlMAQObYZgAAAOtJREFUKM9jYCAACsCk4wYGgiABTLInEKuS+QGxKvkVGBj47jBwI8tcffI84e45BoZ7GVcLECo9751iWLeSoRPITBQEggMMDBy9sxj2MDgz8DIE8yCpPMxwjWFBGUMMkpFcbAEMvxjKGLgYxIE8NkHBiYIyQMY+hmoGhi0Mdsi2czawbGCQBTJ+ILvzE0MaA9MHIIWwnWE9A+sBpk8LGDgmMCnAVXJNYPgCJHhRQvUiA/cDXoECZx4DXoSZTBtYgaaEPw5AVnkOGBRc5xTcbsReQrL9+nWwyxbgC88DcJZ+QygDcYD1+QPiFAIAtLA8KPZOGFEAAAAASUVORK5CYII='> is not.</p>

正如你所看到的,这是一个详细的输出,但这真的不是一个问题,因为你已经在使用Markdown:)

将以下代码添加到Markdown文件的顶部,以获得MathJax呈现支持

<style TYPE="text/css">
code.has-jax {font: inherit; font-size: 100%; background: inherit; border: inherit;}
</style>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [['$','$'], ['\\(','\\)']],
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'] // removed 'code' entry
    }
});
MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i = 0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';
    }
});
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-AMS_HTML-full"></script>

然后' $x^2$ '或' $$x^2$$ '将按预期呈现:-)

如果您不想使用在线发行版,您总是可以安装MathJax的本地版本,但您可能需要通过本地web服务器托管它。

更新:这些天我只使用pandoc而不是规范markdown,但上面仍然有用。

你试过Pandoc吗?

EDIT: Although the documentation has become a bit complex, pandoc has supported inline LaTeX and LaTeX templates for 10 years. Documents like the following one can be written in Markdown: --- title: Just say hello! author: My Friend header-includes: | \usepackage{tikz,pgfplots} \usepackage{fancyhdr} \pagestyle{fancy} \fancyhead[CO,CE]{This is fancy} \fancyfoot[CO,CE]{So is this} \fancyfoot[LE,RO]{\thepage} abstract: This is a pandoc test with Markdown + inline LaTeX --- Just say hello! =============== This could be a good example or inlined \LaTeX: \begin{tikzpicture} \begin{axis} \addplot[color=red]{exp(x)}; \end{axis} \end{tikzpicture} %Here ends the furst plot \hskip 5pt %Here begins the 3d plot \begin{tikzpicture} \begin{axis} \addplot3[ surf, ] {exp(-x^2-y^2)*x}; \end{axis} \end{tikzpicture} And now, just a few words to terminate: > Goodbye folks! Which can be converted to LaTeX using commands like this: pandoc -s -i Hello.md -o Hello.tex Following is an image of the converted Hello.md to Hello.pdf file using MiKTeX as LaTeX processor with the command: pandoc -s -i Hello.md -o Hello.pdf Finally, there are some open source LaTeX templates like this one: https://github.com/Wandmalfarbe/pandoc-latex-template, that can be used for better formatting. As always, the reader should dig deeper if he has less trivial use cases than presented here.

当我找到teqhtml时,我正在寻找完全相同的东西。它将$和$$方程转换为图像,并将生成的图像与周围的文本垂直对齐。虽然不是很多,但是很简单。

希望对以后的读者有所帮助。