如何在 *.Rmd 的 HTML 输出中获得带边框的(LaTeX 数学)排版矩阵?

How do I get (LaTeX math) typeset matrix with borders in HTML output from *.Rmd?

以下 rmarkdown 在编译为 PDF(通过 LaTeX)时工作正常,但在编译为 HTML(通过 Markdown)时却不行。

PDF(通过 LaTeX)

---
title: "test"
author: "Maximilian Held"
output: pdf_document
documentclass: memoir
---

(@matrix-test2) $$
\bordermatrix{
  ~       & Petra & Ingrid \cr
  Petra   & 1     & 0 \cr
  Ingrid  & 0     & 1 \cr
}
$$


HTML(通过 Markdown)

---
title: "test"
author: "Maximilian Held"
output: html_document
documentclass: memoir
---

(@matrix-test2) $$
\bordermatrix{
  ~       & Petra & Ingrid \cr
  Petra   & 1     & 0 \cr
  Ingrid  & 0     & 1 \cr
}
$$

产量

这是怎么回事,这到底是谁的(Pandoc?Markdown?MathML?)

我认为这种行为是正常的,因为 \bordermatrix 是一个 LaTeX 命令,当你编译一个 PDF(使用 LaTeX)时它工作得很好,但我不认为直接的 LaTeX 标记可以在 HTML 即使在没有特定 class 规范的普通文档中(实际上你使用的是回忆录)。

---
title: "prova"
author: "SabDeM"
date: "09 giugno 2015"
output: html_document
---

\begin{equation}
x^2
\end{equation}

这个例子还是不行。所以我认为你很可能必须以另一种方式进行转换。 我建议您将数据存储在 R 对象中,然后使用 kable 函数并将格式更改为 htmllatex,只要您愿意。

编辑

我看到 \bordermatrix 甚至不是 LaTeX 命令而是普通的 TeX 命令。这可以使事情变得更糟或更好,这取决于 knitr 的内部解析结构,我不知道。

knitr, the tool normally used to convert RMarkdown to HTML/PDF (from LaTeX)/DOCX, uses Pandoc。 Pandoc 是一个很好的工具,可以将 Markdown 转换为 HTML 和 LaTeX,并允许您在 Markdown 中使用 LaTeX 数学环境,例如

$a x^2 + b x + c = 0$

$$a x^2 + b x + c = 0$$

\begin{equation}
a x^2 + b x + c = 0
\end{equation}

已由 Pandoc 正确转换。 Pandoc 还支持 amsmath,一个非常流行的 LaTeX 数学包。 不幸的是,Pandoc 不支持您发现的所有 (La)TeX commands/environment。

在与 Pandoc 合作时,我一直在做的是尽量让事情变得简单。在你出现的情况下,我会使用普通表格而不是矩阵。

Pandoc 为 math rendering when exporting to HTML 提供了几个不同的选项。

您可以选择使用各种 JavaScript 库,例如 jsMath, MathJax and KaTeX, but I think neither supports the \bordermatrix command. There's also MathML which few browsers support。最后,如果你真的想要像这样的高级 TeX 数学命令工作,Pandoc 可以使用 gladTeX 或 mimeTeX 将数学渲染为图像,这些图像将嵌入 HTML.