使xtable适合pdf文件

时间:2013-03-26 18:20:39

标签: r latex sweave

我正在创建一个使用xtable创建表并放入pdf文件的sweave文档。它工作,但表不适合文档,一些文本丢失。有没有办法在xtable中完全匹配xtable /将xtable完全匹配到pdf文件?

这是我的数据:

dput(x)
structure(list(App = structure(c(3L, 2L, 1L), .Label = c("AppServer", 
"Db", "Web"), class = "factor"), Group = structure(c(2L, 1L, 
1L), .Label = c("Back", "Front"), class = "factor"), Owner = structure(c(1L, 
1L, 1L), .Label = "Infrasructure", class = "factor"), Server = structure(1:3, .Label = c("ServerA", 
"ServerB", "ServerC"), class = "factor"), NumberCPU = c(64L, 
120L, 120L), Description = structure(c(1L, 3L, 2L), .Label = c("Front End server to server web traffic", 
"Hold Web templates to generate dynamic content", "Keeps customer data and login information"
), class = "factor"), Cost = structure(1:3, .Label = c("$200,000 ", 
"$400,000 ", "$500,000 "), class = "factor")), .Names = c("App", 
"Group", "Owner", "Server", "NumberCPU", "Description", "Cost"
), class = "data.frame", row.names = c(NA, -3L))

这是将表放在pdf中的代码:

print(xtable(x, caption=paste("Summary of applications"),table.placement="!h",caption.placement="top", align=c('l', 'p{1.5in}', rep('c',6) )))

2 个答案:

答案 0 :(得分:4)

我建议查看xtable gallery,有很多有用的示例。基本上,如果您不想通过缩短字符串来调整表格,我会看到两个选项:

  1. 使用较小的字体。
  2. 使用横向模式。
  3. 在这里,我结合使用两者:

    \documentclass{article}
    \usepackage{rotating}
    
    \begin{document}
    
    <<Data,echo=FALSE>>=
    library(xtable)
    x <- structure(list(App = structure(c(3L, 2L, 1L), .Label = c("AppServer", 
    "Db", "Web"), class = "factor"), Group = structure(c(2L, 1L, 
    1L), .Label = c("Back", "Front"), class = "factor"), Owner = structure(c(1L, 
    1L, 1L), .Label = "Infrasructure", class = "factor"), Server = structure(1:3, .Label = c("ServerA", 
    "ServerB", "ServerC"), class = "factor"), NumberCPU = c(64L, 
    120L, 120L), Description = structure(c(1L, 3L, 2L), .Label = c("Front End server to server web traffic", 
    "Hold Web templates to generate dynamic content", "Keeps customer data and login information"
    ), class = "factor"), Cost = structure(1:3, .Label = c("$200,000 ", 
    "$400,000 ", "$500,000 "), class = "factor")), .Names = c("App", 
    "Group", "Owner", "Server", "NumberCPU", "Description", "Cost"
    ), class = "data.frame", row.names = c(NA, -3L))
    @
    
    <<tab,echo=FALSE,results='asis'>>=
    print(xtable(x, caption=paste("Summary of applications"),
                 caption.placement="top",
                 align=c('l', 'p{1.5in}', rep('c',6))),
                 size="footnotesize",
                 floating.environment="sidewaystable")
    @
    
    
    \end{document}
    

    请注意,您必须使用LaTex包rotating。这应该给你这样的东西:

    Output

答案 1 :(得分:1)

另一种解决方案是使用一些降价后端(knitrmarkdownpander包)并使用{自动将表拆分为80个字符(或其他用户指定的宽度) {1}}。 E.g:

pander

结果可以轻松转换为pdf或LaTeX,然后直接从R > library(pander) > res <- structure(list(App = structure(c(3L, 2L, 1L), .Label = c("AppServer", "Db", "Web"), class = "factor"), Group = structure(c(2L, 1L, 1L), .Label = c("Back", "Front"), class = "factor"), Owner = structure(c(1L, 1L, 1L), .Label = "Infrasructure", class = "factor"), Server = structure(1:3, .Label = c("ServerA", "ServerB", "ServerC"), class = "factor"), NumberCPU = c(64L, 120L, 120L), Description = structure(c(1L, 3L, 2L), .Label = c("Front End server to server web traffic", "Hold Web templates to generate dynamic content", "Keeps customer data and login information"), class = "factor"), Cost = structure(1:3, .Label = c("$200,000 ", "$400,000 ", "$500,000 "), class = "factor")), .Names = c("App", "Group", "Owner", "Server", "NumberCPU", "Description", "Cost"), class = "data.frame", row.names = c(NA, -3L)) > pander(res) ---------------------------------------------------- App Group Owner Server NumberCPU --------- ------- ------------- -------- ----------- Web Front Infrasructure ServerA 64 Db Back Infrasructure ServerB 120 AppServer Back Infrasructure ServerC 120 ---------------------------------------------------- Table: Table continues below --------------------------------------- Description Cost ------------------------------ -------- Front End server to server web $200,000 traffic Keeps customer data and login $400,000 information Hold Web templates to generate $500,000 dynamic content --------------------------------------- pandoc转换。