Html 如何将 LaTeX 与 Markdown 混合使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2188884/
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
How can I mix LaTeX in with Markdown?
提问by Norman Ramsey
I've been using Markdown for class notes, and it's great. I even do some preprocessing on the Markdown so I can do things like tables. But this term I'm teaching a class with a lot of math, and I'd love to be able to put LaTeX formulas with Markdown, something like this:
我一直在使用 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$".
I'd like to be able to take each fragment of LaTeX and preprocess it into a nice antialiased PNG file which I could then include in my Markdown via the HTML <img>
tag. But I have absolutely no idea how to take a fragment of LaTeX and get a nice image that
我希望能够将 LaTeX 的每个片段预处理成一个漂亮的抗锯齿 PNG 文件,然后我可以通过 HTML<img>
标记将其包含在我的 Markdown 中。但我完全不知道如何获取 LaTeX 的片段并获得一个漂亮的图像
- Has the right bounding box
- Is antialiased
- 有正确的边界框
- 抗锯齿
All I know how to do is get full pages in DVI, PostScript, or PDF formats.
我只知道如何获取 DVI、PostScript 或 PDF 格式的完整页面。
I'm sure this problem has been addressed, but I haven't been able to guess the right search terms. Any suggestions how to solve it or where to look for an existing solution?
我确信这个问题已经得到解决,但我一直无法猜出正确的搜索词。任何建议如何解决它或在哪里寻找现有的解决方案?
EDIT: Having installed mathTeX, I can say that the code is inflexible, that it violates the Linux Filesystem Hierarchy standard, and that it is amateur work—in both the good and bad senses of that word. The code is so complex that there are no obvious faults. I will be looking for alternatives.
编辑:安装 mathTeX 后,我可以说代码不灵活,它违反了 Linux 文件系统层次结构标准,而且它是业余工作——从这个词的好的和坏的意义上来说。代码非常复杂,没有明显的错误。我会寻找替代品。
Also, it's clear that at bottom, solutions are based on dvipng
.
此外,很明显,在底部,解决方案基于dvipng
.
ONE YEAR LATER: I never did get the seamless integration I had been hoping for, but I am limping along on a script of my own devising. It turns out that instead of dvipng
it is a little easier to use dvips -E
and the convert
program of ImageMagick. The benefits are slightly more control of things like scaling, and ease of making a transparent background.
The curious can inspect this example.
一年后:我从来没有得到我一直希望的无缝集成,但我正在按照我自己设计的脚本一瘸一拐地前进。事实证明,而不是dvipng
它更容易使用dvips -E
和convert
ImageMagick的程序。好处是对缩放等内容的控制稍微多一些,并且易于制作透明背景。有兴趣的可以看看这个例子。
I can't recommend this solution to anyone. But I can't recommend MathTeX either.
我不能向任何人推荐这个解决方案。但我也不能推荐 MathTeX。
回答by duffymo
Perhaps mathJAXis the ticket. It's built on jsMath, a 2004 vintage JavaScript library.
也许mathJAX就是票。它建立在jsMath 之上,这是一个 2004 年的老式 JavaScript 库。
As of 5-Feb-2015 I'd switch to recommend KaTeX- most performant Javascript LaTeX library from Khan Academy.
截至 2015 年 2 月 5 日,我将转而推荐KaTeX- 来自可汗学院的性能最高的 Javascript LaTeX 库。
回答by fommil
Add the following code to the top of your Markdown files to get MathJax rendering support
将以下代码添加到 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>
and then `$x^2$` or `$$x^2$$` will render as expected :-)
然后`$x^2$` 或`$$x^2$$` 将按预期呈现:-)
You can always install a local version of MathJax if you don't want to use the online distribution, but you might need to host it through a local webserver.
如果您不想使用在线发行版,您始终可以安装本地版本的 MathJax,但您可能需要通过本地网络服务器托管它。
UPDATE: these days I just use pandocinstead of canonical markdown, but the above is still useful.
更新:这些天我只使用pandoc而不是规范降价,但以上仍然有用。
回答by Charles Stewart
I'll answer your question with a counter-question...
我会用一个反问来回答你的问题...
What do you think of Org-mode? It's not as pure as Markdown, but it is Markdown-like, and I find it as easy to work with, and it allows embedding of Latex. Cf. http://www.gnu.org/software/emacs/manual/html_node/org/Embedded-LaTeX.html
你如何看待组织模式?它不像 Markdown 那样纯粹,但它类似于 Markdown,而且我发现它很容易使用,并且它允许嵌入 Latex。参见 http://www.gnu.org/software/emacs/manual/html_node/org/Embedded-LaTeX.html
Postscript
后记
In case you haven't looked at org-mode, it has one great strength as a general purpose "natural markup language" over Markdown, namely its treatment of tables. The source:
如果你还没有看过 org-mode,它作为一种通用的“自然标记语言”比 Markdown 有很大的优势,即它对表格的处理。来源:
| 1 | 0 | 0 | | -1 | 1 | 0 | | -1 | -1 | 1 |
represents just what you think it will...
代表什么你认为它会......
And the Latex is rendered in pieces using tex-mode's preview-latex.
而 Latex 是使用 tex-mode 的 preview-latex 分段渲染的。
回答by Mica
you should look at multimarkdown http://fletcherpenney.net/multimarkdown/
你应该看看 multimarkdown http://fletcherpenney.net/multimarkdown/
it has support for metadata (headers, keywords, date, author, etc), tables, asciimath, mathml, hell i'm sure you could stick latex math code right in there. it's basically an extension to markdown to add all these other very useful features. It uses XSLT, so you can easily whip up your own LaTeX styles, and have it directly convert. I use it all the time, and I like it a lot.
它支持元数据(标题、关键字、日期、作者等)、表格、asciimath、mathml,我敢肯定你可以在那里粘贴乳胶数学代码。它基本上是 markdown 的扩展,用于添加所有其他非常有用的功能。它使用 XSLT,因此您可以轻松创建自己的 LaTeX 样式,并直接进行转换。我一直在使用它,我非常喜欢它。
I wish the markdown would just incorporate multimarkdown. it would be rather nice.
我希望 Markdown 只包含 multimarkdown。会比较好。
Edit: Multimarkdown will produce html, latex, and a few other formats. html can come with a style sheet of your choice. it will convert into MathML as well, which displays in Firefox and Safari/Chrome, if I remember correctly.
编辑:Multimarkdown 将生成 html、latex 和其他一些格式。html 可以附带您选择的样式表。如果我没记错的话,它也会转换为 MathML,它会显示在 Firefox 和 Safari/Chrome 中。
回答by rsacc
回答by matt
kramdown does exactly what you describe:
kramdown 完全符合您的描述:
https://kramdown.gettalong.org/syntax.html#math-blocks
https://kramdown.gettalong.org/syntax.html#math-blocks
And it's way more reliable and well-defined than Markdown.
而且它比 Markdown 更可靠和定义明确。
回答by Justin Van Horne
Hey, this might not be the most ideal solution, but it works for me. I ended up creating a Python-Markdown LaTeX extension.
嘿,这可能不是最理想的解决方案,但它对我有用。我最终创建了一个 Python-Markdown LaTeX 扩展。
https://github.com/justinvh/Markdown-LaTeX
https://github.com/justinvh/Markdown-LaTeX
It adds support for inline math and text expressions using a $math$ and %text% syntax. The extension is a preprocessor that will use latex/dvipng to generate pngs for the respective equations/text and then base64 encode the data to inline the images directly, rather than have external images.
它使用 $math$ 和 %text% 语法添加了对内联数学和文本表达式的支持。该扩展是一个预处理器,它将使用 latex/dvipng 为相应的方程/文本生成 png,然后对数据进行 base64 编码以直接内联图像,而不是具有外部图像。
The data is then put in a simple-delimited cache file that encodes the expression to the base64 representation. This limits the number of times latex actually has to be run.
然后将数据放入一个简单分隔的缓存文件中,该文件将表达式编码为 base64 表示。这限制了乳胶实际必须运行的次数。
Here is an example:
下面是一个例子:
%Hello, world!% This is regular text, but this: $y = mx + b$ is not.
The output:
输出:
$ 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>
As you can see it is a verbose output, but that really isn't an issue since you're already using Markdown :)
正如您所看到的,这是一个详细的输出,但这确实不是问题,因为您已经在使用 Markdown 了 :)
回答by Hylke Koers
I came across this discussion only now, so I hope my comment is still useful. I am involved with MathJax and, from how I understand your situation, I think that it would be a good way to solve the problem: you leave your LaTeX code as is, and let MathJax render the mathematics upon viewing.
我现在才遇到这个讨论,所以我希望我的评论仍然有用。我参与了 MathJax,根据我对您的情况的理解,我认为这将是解决问题的好方法:您将 LaTeX 代码保留原样,让 MathJax 在查看时呈现数学。
Is there any reason why you would prefer images?
有什么理由让你更喜欢图像吗?