xtable单元格中的换行

时间:2015-06-24 11:09:32

标签: r latex newline xtable

我使用xtable自动生成LaTeX表,并希望在单元格内强制换行/中断。

可以使用单元格中的\newline来完成此操作,但是我无法阻止xtable\替换反斜杠($\backslash$)。< / p>

以下是一个例子......

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 helpgraph gallery,并认为我可以使用sanitise.text.function明确指定\替换{ {1}}(然后我可以在我的\电话中使用\newline但不幸的是,这不会起作用,因为第二个paste0()被视为逃避以下关闭双引号......

\

我可以使用整个字符串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进行解析后被视为换行符,因此无欢乐。

This thread建议使用xtable作为identity的函数参数但不起作用(也不使用sanitise.text.function的标识函数)。我发现post on R-help提出类似问题(但问题是function(x){x}被解释为标签)但是这也没有用(为简洁起见,省略了输出)。

\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} 选项,但我无法解决这个问题。有办法以某种方式这样做吗?

提前感谢任何建议/见解。

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:1)

我可以确认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")