Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 打印长字符串文本LaTeX/Swave_R_Latex_Sweave_Longtable - Fatal编程技术网

R 打印长字符串文本LaTeX/Swave

R 打印长字符串文本LaTeX/Swave,r,latex,sweave,longtable,R,Latex,Sweave,Longtable,在我所做的一项调查结束时,我们会给受访者一个开放框,告诉我们调查中没有涉及的内容。这些评论通常会跨越几页。我熟悉LaTeX的longtable包,下面是我模拟的解决方案: <<results = tex>>= cat("\\begin{longtable}{p{14cm}}\n") cat("\\hline\n") write.table(toBePrinted, eol = "\\\\\n", col.names = FALSE) cat("\\hline\n") ca

在我所做的一项调查结束时,我们会给受访者一个开放框,告诉我们调查中没有涉及的内容。这些评论通常会跨越几页。我熟悉LaTeX的
longtable
包,下面是我模拟的解决方案:

<<results = tex>>=
cat("\\begin{longtable}{p{14cm}}\n")
cat("\\hline\n")
write.table(toBePrinted, eol = "\\\\\n", col.names = FALSE)
cat("\\hline\n")
cat("\\end{longtable}")
@
=
cat(\\begin{longtable}{p{14cm}}\n)
cat(\\hline\n)
write.table(toBePrinted,eol=“\n”,col.names=FALSE)
cat(\\hline\n)
cat(\\end{longtable})
@
虽然这个解决方案在技术上是可行的,但它看起来并不完美,需要改进。我有两个相关的问题:

  • 将被视为
    tex
    的swave输出的文本提示。例如,如果有人说
    你的调查很棒&我会在$$$100%的时间里做更多的调查通过
    LaTeX
    处理时,特殊字符
    和,$,%
    会损坏。是否有比一系列的
    gsub
    调用更有效的方法来用一些善意的东西替换冒犯的字符
  • 关于使用
    swave&LaTeX
    打印这些长评论的更好方法的建议

  • 您可以查看用于创建latex表的xtable包,但我想这在longtable中不太管用。或者,看看包Hmisc中的函数latex,它有一个选项“longtable”,允许对输出进行更多控制

    要为Latex中使用的特殊字符添加斜杠,可以执行以下操作:

    add.slash <- function(x){
        where <- embed(c(1,gregexpr("[&#$%]",x)[[1]],nchar(x)+1),dim=2)
        out <- paste(apply(where,1,function(y){substr(x,y[2],y[1]-1)}),collapse="\\")
        return(out)
    }
    
    > x <- "I print $ and % and & and # and . and ! and ,"
    
    > cat(add.slash(x),"\n")
    I print \$ and \% and \& and \# and . and ! and , 
    

    add.slash正如您所指出的,
    xtable
    不能很好地处理试图跨越多个页面的表。我正在使用
    xtable
    为报告的其余部分生成LaTeX格式的表。我将不得不查看
    Hmisc
    软件包,看看是否能找到工作。
    add.slash
    函数将正常工作。谢谢~