2015-06-24 68 views
2

我使用xtable来自动生成LaTeX表格,并且想要在单元格中强制换行符/换行符。xtable单元格中的换行符

这可以在小区内使用\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} 

因此,通过乳胶解析,当我在该单元格换行符。

类似的问题有关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} 

接下来我看着xtablehelpgraph gallery,我想我也许能使用sanitise.text.function明确指定替代\\(我可以那么就使用\newlinepaste0()调用中),但不幸的是,这并不工作,要么作为第二\被视为逃避跟随着摹关闭双引号...

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使用identity作为函数参数sanitise.text.function建议,但没有工作,要么(也没有使用function(x){x}身份功能)。我发现一个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选择,但我不能工作了。有没有办法做到这一点?

在此先感谢您的任何建议/见解。

回答

2

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

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

防止\转换在细胞$\backslash$

+0

感谢您的建议,我在完成这项工作的截止日期的最后一周,我忙于检查,但一旦工作完成,我会整理一些东西。 – slackline

相关问题