【问题标题】:Why does RMarkdown not properly escape the second LaTeX table?为什么 RMarkdown 不能正确转义第二个 LaTeX 表?
【发布时间】:2021-04-07 08:44:15
【问题描述】:

我开发了 R 包 vtable,最近有人报告了这个错误,我对 RMarkdown 感到非常困惑。

具体来说,当编织成 PDF 时,我输出的第一个表格很好,但第二个表格没有正确转义。 但只在第一行

这是一个例子:

---
title: "Untitled"
author: "Author"
date: "Date"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(vtable)
d <- data.frame(a = 1)
```

```{r b1, results = 'asis'}
vtable(d, out = 'latex')
```

```{r b2, results = 'asis'}
vtable(d, out = 'latex')
```

请注意,如果我省略了 b2 块,则可以很好地构建为 PDF。但是有了它,构建就会失败,因为只有 first 表可以正常运行。在.tex 输出它的代码是

\begin{table}[!htbp] \centering 
 \renewcommand*{\arraystretch}{1.1} 

\caption{Variable Table}

\begin{tabular}{p{0.290909090909091\textwidth}p{0.145454545454545\textwidth}p{0.363636363636364\textwidth}}
\hline
\hline
Name & Class & Values \\ 
\hline
a & numeric & Num: 1 to 1\\ 
\hline
\hline
\end{tabular}
\end{table}

但是第二个是一团糟,一些字符被不正确地转义并被视为文本。更奇怪的是,只有第一行搞砸了。该表的 LaTeX 代码与前面的代码块完全相同,只是第一行是

\textbackslash begin\{table\}{[}!htbp{]} \centering 

其余的工作正常。如果我添加第三个/第四个/等。表他们同样有一个混乱的第一行。

那么,RMarkdown 中的什么导致只有 第二个和后续表第一行 被不正确地转义?而且,关于如何使表格正确显示的任何想法?谢谢!

【问题讨论】:

  • 您的代码在 sweave 中工作,但看起来 markdown 中的某些内容不喜欢您添加的所有换行符(乳胶通常也不喜欢这些)。将 dftoLaTeX (table.latex &lt;- "\\begin{table}[!htbp] \\centering \n \\renewcommand*{\\arraystretch}{1.1} \n") 的第 77 行更改为 table.latex &lt;- "\\begin{table}[!htbp] \\centering \\renewcommand*{\\arraystretch}{1.1} \n" 就足够了。 (我还将努力删除在您的乳胶代码、标题等周围添加的所有行空格)
  • 太棒了!我会试试这个,谢谢。
  • 顺便说一句,这确实有效,再次感谢。如果您添加您的评论作为答案,我会接受它
  • 好东西。皮斯随意自我回答

标签: r latex r-markdown


【解决方案1】:

根据上面@user20650 的评论:

vtable()(通过dftoHTML())的输出在转义之前开始

"\\begin{table}[!htbp] \\centering \n \\renewcommand*{\\arraystretch}{1.1} \n"

RMarkdown 很难处理所有这些换行符\n

通过将转义前输出更改为

"\\begin{table}[!htbp] \\centering \\renewcommand*{\\arraystretch}{1.1}"

问题解决了。

【讨论】:

    猜你喜欢
    • 2012-07-26
    • 1970-01-01
    • 2016-10-27
    • 2016-10-20
    • 2017-01-31
    • 2016-08-15
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    相关资源
    最近更新 更多