问题
The following rmarkdown
works fine when compiling to PDF (via LaTeX), but not when compiling to HTML (via Markdown).
PDF (via 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 (via 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
}
$$
yields
What's up with that and on whose end is this (Pandoc? Markdown? MathML?)
回答1:
knitr, the tool normally used to convert RMarkdown to HTML/PDF (from LaTeX)/DOCX, uses Pandoc. Pandoc is a nice tool to convert Markdown to HTML and LaTeX and allows you to use LaTeX math environments inside Markdown, e.g.
$a x^2 + b x + c = 0$
or
$$a x^2 + b x + c = 0$$
or
\begin{equation}
a x^2 + b x + c = 0
\end{equation}
are properly converted by Pandoc. Pandoc also supports the amsmath
, a very popular LaTeX package for math. Unfortunately, Pandoc doesn't support all (La)TeX commands/environment as you discovered.
What I always do when working with Pandoc is try to keep things simple. In the case you present, I would use normal tables instead of a matrix.
回答2:
Pandoc has a couple of different options for math rendering when exporting to HTML.
You can choose between using various JavaScript libraries like jsMath, MathJax and KaTeX, but I think neither supports the \bordermatrix
command. There's also MathML which few browsers support. Finally, if you really want advanced TeX math commands like this to work, Pandoc can work with gladTeX or mimeTeX to render the math to images which will be embedded in the HTML.
回答3:
I believe this behavior is normale because \bordermatrix
is a LaTeX command and it works well when you compile a PDF (which uses LaTeX), but I do not think direct LaTeX markup can be converted in HTML even in a normal document without specific class specification (you use memoir in fact).
---
title: "prova"
author: "SabDeM"
date: "09 giugno 2015"
output: html_document
---
\begin{equation}
x^2
\end{equation}
This example still doesn't work well. So I think that is very probable that you have to convert in another way.
I advise you to store your data in an R object and then use the kable
function and change the format to html
or latex
whenever you like.
Edit
I saw that \bordermatrix
is not even a LaTeX command but a plain TeX one. This can make things worse or better, it depends on the internal parsing structure of knitr, which I have no idea.
来源:https://stackoverflow.com/questions/30740613/how-do-i-get-latex-math-typeset-matrix-with-borders-in-html-output-from-rmd