我有以下 LaTeX 表格,当
format: pdf
: 时按预期呈现
---
title: "Test Table"
format: pdf
---
\begin{center}
\begin{tabular}{|l|l|l|}
\hline
Var & Class & Description\\
\hline
$x$ & numeric & xyz \\
$y$ & numeric & xzh \\
$z$ & integer & xlp \\
\hline
\end{tabular}
\end{center}
我寻找该表也以 HTML 格式显示的可能性,即
format: html
。我有很多(很多)LaTeX 表需要转换,因此我希望有一个解决方案可以避免手动将它们全部写为 Markdown 表。一如既往,我们非常感谢任何帮助!
parse-latexQuarto 扩展是根据这种情况编写的。它的工作原理是使用 pandoc 的 LaTeX 解析器来处理文档中的所有原始 LaTeX 片段,从而可以将它们转换为任意格式。
转换受到 pandoc 的 LaTeX 解析器的限制;特别是样式信息将不会在转换中保留。为了完整起见,并且由于它相当短,这里是该扩展中附带的 pandoc Lua 过滤器的完整代码。也可以直接使用,保存到文件
parse-latex.lua
,然后在qmd文件的YAML头中与
filters: [parse-latex.lua]
一起使用。
--- parse-latex.lua – parse and replace raw LaTeX snippets
---
--- Copyright: © 2021–2022 Albert Krewinkel
--- License: MIT – see LICENSE for details
-- Makes sure users know if their pandoc version is too old for this
-- filter.
PANDOC_VERSION:must_be_at_least '2.9'
-- Return an empty filter if the target format is LaTeX: the snippets will be
-- passed through unchanged.
if FORMAT:match 'latex' then
return {}
end
-- Parse and replace raw TeX blocks, leave all other raw blocks
-- alone.
function RawBlock (raw)
if raw.format:match 'tex' then
return pandoc.read(raw.text, 'latex').blocks
end
end
-- Parse and replace raw TeX inlines, leave other raw inline
-- elements alone.
function RawInline(raw)
if raw.format:match 'tex' then
return pandoc.utils.blocks_to_inlines(
pandoc.read(raw.text, 'latex').blocks
)
end
end
xtable
包的帮助下使用 MathJax 的
array
环境:
---
title: "Untitled"
output: html_document
date: "2022-12-01"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(xtable)
```
```{r, results='asis'}
dat <- data.frame(
"\\text{Var}" = c("x", "y", "z"),
"\\text{Class}" = c("\\text{numeric}", "\\text{numeric}", "\\text{integer}"),
check.names = FALSE
)
M <- print(xtable(dat, align=rep("|c|", ncol(dat)+1)),
floating = FALSE, tabular.environment="array",
comment=FALSE, print.results=FALSE,
include.rownames = FALSE,
sanitize.text.function = function(x) x)
cat(M)
```
或者直接输入数组即可:
\begin{array}{|l|l|l|}
\hline
\text{Var} & \text{Class} & \text{Description} \\
\hline
x & \text{numeric} & \text{xyz} \\
y & \text{numeric} & \text{xzh} \\
\hline
\end{array}
array
环境处于数学模式,因此您不必将
$
作为数学符号,而必须使用
\text
作为文本模式。
并非所有 Latex 语法或环境都支持 html 输出渲染。您只能使用MathJax或KaTeX或其他列出的此处支持的那些。
并且您需要阅读这些渲染方法的文档以了解它们支持哪些 TeX 命令进行 html 渲染并相应地转换 Latex 代码。例如,
MathJax,支持的 Tex LaTex 命令包含 MathJax 支持 html 渲染的 TeX 命令的独家列表。
KaTeX,此页面包含 KaTeX 支持的 TeX 命令列表。