由于更新,knitr 中不需要的清理(转义)

Unwanted sanitizing (escaping) in knitr due to update

最后 Spring,我使用了类似于下面我的最小工作示例 (MWE) 的代码来实现:https://cdn.advocacy.sba.gov/wp-content/uploads/2019/04/23142650/2019-Small-Business-Profiles-MA.pdf

根据 knitr 文档,table 代码不应被清理。然而,如果你编织 MWE,你会看到几个字符确实被清理(转义)。例如,\ 有时会变成 \textbackslash{}。因此,当代码被清理时,似乎某些包更新发生了变化。如果您只是从下面复制 RMD table 代码并将其粘贴到 TeX 输出中,它可以正常编译。

我已经尝试恢复到去年的许多包裹,但无济于事。

---
title: "MWE"
output:
  pdf_document:
    keep_tex: yes
---
\begin{tabular}{@{}lll}
\begin{tabular}{ll}
A & B \
A & B \
\end{tabular}
&
 \begin{tabular}{ll}
A & B \
A & B \
\end{tabular}
&
\begin{tabular}{ll}
A & B \
A & B \
\end{tabular}
\end{tabular}

*[在我的原始代码中,它是 "asis" 块的一部分。你可以试试这个,发现同样的问题。]

这是一个已知问题,解决方案是将 raw attributes 添加到 pandoc,以确保块内的内容不会被转义。

1) 添加md_extensions: +raw_attribute
2) 添加乳胶块

对于您的示例,其工作方式如下:

---
title: "MWE"
output:
  pdf_document:
    md_extensions: +raw_attribute
    keep_tex: yes
---

```{=latex}
\begin{tabular}{@{}lll}
\begin{tabular}{ll}
A & B \
A & B \
\end{tabular}
&
 \begin{tabular}{ll}
A & B \
A & B \
\end{tabular}
&
\begin{tabular}{ll}
A & B \
A & B \
\end{tabular}
\end{tabular}
```

或者在代码块中使用它时:

```{r, results = "asis"}
cat("```{=latex}")
// your latex code
cat("```")
```

P.S.: 此功能需要 pandoc 版本 2.0.0