xtable 单元格中的换行符

Line breaks in xtable cells

我正在使用 xtable 自动生成 LaTeX 表格,并希望在单元格内强制使用 line-feed/break。

这可以在单元格中使用 \newline 来完成,但是我无法阻止 xtable 将我的反斜杠 (\) 替换为 $\backslash$

举个例子....

library(magrittr)
library(xtable)
my.data <- cbind(c("1", "2", "3"),
                 c("0", "1", "2"),
                 c("2", "3", "4")) %>%
           as.data.frame()
## Generate output variable
my.data$out <- paste0(my.data$V1,
                      " \newline (",
                      my.data$V2,
                      " to ",
                      my.data$V3,
                      ")")
## Check the data frame...
my.data
  V1 V2 V3                  out
1  1  2  3 1 \newline (2 to 3)
2  0  1  2 0 \newline (1 to 2)
3  2  3  4 2 \newline (3 to 4)
## Now generate xtable
xtable(my.data)
% latex table generated in R 3.2.0 by xtable 1.7-4 package
% Wed Jun 24 11:51:59 2015
\begin{table}[ht]
\centering
\begin{tabular}{rllll}
  \hline
  & V1 & V2 & V3 & out \ 
  \hline
1 & 1 & 2 & 3 & 1 $\backslash$newline (2 to 3) \ 
2 & 0 & 1 & 2 & 0 $\backslash$newline (1 to 2) \ 
3 & 2 & 3 & 4 & 2 $\backslash$newline (3 to 4) \ 
  \hline
\end{tabular}
\end{table}

paste0() 按预期工作正常,但将此数据帧传递给 xtable 不会产生我预期(/希望)的结果,即....

xtable(my.data)
% latex table generated in R 3.2.0 by xtable 1.7-4 package
% Wed Jun 24 11:51:59 2015
\begin{table}[ht]
\centering
\begin{tabular}{rllll}
  \hline
  & V1 & V2 & V3 & out \ 
  \hline
1 & 1 & 2 & 3 & 1 \newline (2 to 3) \ 
2 & 0 & 1 & 2 & 0 \newline (1 to 2) \ 
3 & 2 & 3 & 4 & 2 \newline (3 to 4) \ 
  \hline
\end{tabular}
\end{table}

因此,当通过 LaTeX 进行解析时,我会在该单元格中换行。

关于 line breaks in captions 的类似问题建议使用双转义反斜杠,但在这种情况下也不起作用...

my.data$out <- paste0(my.data$V1,
                      " \\newline (",
                      my.data$V2,
                      " to ",
                      my.data$V3,
                      ")")
% latex table generated in R 3.2.0 by xtable 1.7-4 package
% Wed Jun 24 11:57:04 2015
\begin{table}[ht]
\centering
\begin{tabular}{rllll}
  \hline
 & V1 & V2 & V3 & out \ 
  \hline
1 & 1 & 2 & 3 & 1 $\backslash$$\backslash$newline (2 to 3) \ 
  2 & 0 & 1 & 2 & 0 $\backslash$$\backslash$newline (1 to 2) \ 
  3 & 2 & 3 & 4 & 2 $\backslash$$\backslash$newline (3 to 4) \ 
   \hline
\end{tabular}
\end{table}

接下来我查看了 xtable help and graph gallery 并认为我可以使用 sanitise.text.function 明确指定用 \ 替换 \ (然后我可以在我的 paste0() 调用中使用 \newline )但不幸的是这不起作用,因为第二个 \ 被视为转义以下结束双引号...

xtable(my.data, sanitise.text.function = function(str)gsub("\", "\",str,fixed=TRUE))
Error: unexpected input in:
"xtable(my.data,
       sanitise.text.function = function(str)gsub("\", "\"

我可以使用整个字符串 \newline 并将其替换为自身吗?

my.data$out <- paste0(my.data$V1,
                      " \newline (",
                      my.data$V2,
                      " to ",
                      my.data$V3,
                      ")")
xtable(my.data, sanitise.text.function = function(str)gsub("\newline", "\newline",str,fixed=TRUE))
% latex table generated in R 3.2.0 by xtable 1.7-4 package
% Wed Jun 24 12:05:25 2015
\begin{table}[ht]
\centering
\begin{tabular}{rllll}
  \hline
 & V1 & V2 & V3 & out \ 
  \hline
1 & 1 & 0 & 2 & 1 
ewline (0 to 2) \ 
2 & 2 & 1 & 3 & 2 
ewline (1 to 3) \ 
3 & 3 & 2 & 4 & 3 
ewline (2 to 4) \ 
   \hline
\end{tabular}
\end{table}

因为 \n 现在在通过 xtable 解析时被视为换行符,所以没有什么乐趣。

This thread suggested using identity as the function argument to sanitise.text.function but that didn't work either (nor does using an identity function of function(x){x}). I found a post on R-help 询问类似(但问题是 \t 被解释为制表符)但这也不起作用(为简洁起见再次省略输出)。

 my.data
  V1 V2 V3                  out
1  1  0  2 1 \newline (0 to 2)
2  2  1  3 2 \newline (1 to 3)
3  3  2  4 3 \newline (2 to 4)
xtable(my.data, sanitise.text.function = identity)
% latex table generated in R 3.2.0 by xtable 1.7-4 package
% Wed Jun 24 13:09:47 2015
\begin{table}[ht]
\centering
\begin{tabular}{rllll}
  \hline
 & V1 & V2 & V3 & out \ 
  \hline
1 & 1 & 0 & 2 & 1 $\backslash$newline (0 to 2) \ 
2 & 2 & 1 & 3 & 2 $\backslash$newline (1 to 3) \ 
3 & 3 & 2 & 4 & 3 $\backslash$newline (2 to 4) \ 
 \hline
\end{tabular}
\end{table}

my.data
  V1 V2 V3                 out
1  1  0  2 1 \newline (0 to 2)
2  2  1  3 2 \newline (1 to 3)
3  3  2  4 3 \newline (2 to 4)
xtable(my.data,
       sanitise.text.function = identity)
% latex table generated in R 3.2.0 by xtable 1.7-4 package
% Wed Jun 24 13:28:28 2015
\begin{table}[ht]
\centering
\begin{tabular}{rllll}
  \hline
 & V1 & V2 & V3 & out \ 
  \hline
1 & 1 & 0 & 2 & 1 
ewline (0 to 2) \ 
  2 & 2 & 1 & 3 & 2 
ewline (1 to 3) \ 
  3 & 3 & 2 & 4 & 3 
ewline (2 to 4) \ 
   \hline
\end{tabular}
\end{table}

感觉我快要使用 sanitise.text.function 选项了,但我无法解决。有办法做到这一点吗?

提前感谢任何suggestions/insights。

您是否使用 options() 而不是 print() 配置 sanitize.*.function?例如,

options(xtable.sanitize.text.function=identity)

防止在单元格中将 \ 转换为 $\backslash$

我可以确认 xtable.sanitize.text.function 有效。我使用它如下

options(xtable.sanitize.text.function = line_break_function)

line_break_function <- function(x){
gsub("$<$br$>$","<br>",x)
}

我如下使用它在汇总函数中为文本块添加换行符

cv %>%
group_by() %>%
summarise(colname = paste(`summary`,":",`Submit Date`,collapse = ",<br>"))

print(xtable, type = "html")