代码之家  ›  专栏  ›  技术社区  ›  Chase

打印长字符串文本/swave

  •  3
  • Chase  · 技术社区  · 15 年前

    在我所做的一项调查结束时,我们会给受访者一个开放式的方框,告诉我们调查中没有涉及的任何内容调查。这些评论通常会跨越几页。我熟悉 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}")
    @
    

    虽然这个解决方案在技术上是可行的,但看起来并不是很完美,需要改进。我有两个相关的问题:

    1. tex . 例如,如果有人说 Your survey is awesome & I would take more surveys for $$$ 100% of the time! 特殊人物 &, $, % 当处理通过 LaTeX . 有什么比一张清单更有效的吗 gsub
    2. 关于如何更好地打印这些长评论的建议 Sweave & LaTeX .
    1 回复  |  直到 15 年前
        1
  •  2
  •   Joris Meys    15 年前

    您可以查看用于创建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 , 
    

    编辑:

    推荐文章