使用表格 + knitr for latex 的简单示例 [英] Simple example of using tables + knitr for latex

查看:26
本文介绍了使用表格 + knitr for latex 的简单示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使用

请注意,add.to.row 需要一个包含两个元素的列表:pos 和 command.第一个必须是列表,第二个必须是字符串或向量,请参见 ?print.xtable.pos 给出 LaTeX 插入的行号,command 是插入.格式化时要小心,因为如果您不输入空格或 ,它将直接运行到第一列的下一个单元格.

有很多自定义选项,允许您通过一些调整来创建相当复杂的表格.

打印(xtable(df),包括.rownames = FALSE,包括.colnames = FALSE,hline.after = c(-1,0),add.to.row = 列表(位置 = 列表(0,5,10),命令 = c("& \multicolumn{4}{c}{4 列标题} \\\cline{2-5}col1 &col2 &col3 &col4 &col5 \\ ","\hline \multicolumn{5}{l}{表格中的分隔符.} \\ \hline","\hline \multicolumn{5}{l}{表格末尾的注释.} \\")))

在本例中,我更改了 xtable 放置 hline 的位置的默认设置,允许我在注释上方添加最后一个 hline- 有助于解释表格中的上标.

还要注意使用 cline{2-5} 在第 2 - 5 列上给我一行.

请参阅 gist 了解完全可重现的示例.

I'm struggling with a tables package, all the examples in the packable docs are so complex and none of them works for me with knitr and latex. Could somebody help be out and display a simple table with some formulas and multiline labels in the header? 

Something like this:

df <- data.frame(matrix(1:9, nrow = 3))
colnames(df) <- c("first column", "second \frac{1}{2}", "third column first line \ second line")

Thank you in advance

解决方案

It is possible to create multi-line headers for tables in LaTeX using the xtable package. These can be compiled from either .Rmd or .Rnw files. Building on the example by mattw and using add.to.row in the print method for xtable:

df <- data.frame(matrix(1:50, nrow = 10))

print(
  xtable(df),
  include.rownames = FALSE,
  include.colnames = FALSE,
  add.to.row = list(
    pos = list(0),
    command = c(
      "& \multicolumn{4}{c}{4 column header} \\
       \cline{2-5}
      col1 & col2 & col3 & col4 & col5 \\ "
    )
  )
)

Note that add.to.row requires a list with two elements: pos and command. The first must be a list, the second a character string or vector, see ?print.xtable. pos gives the row number for the LaTeX insertion, and command is the insertion. Be a bit careful with formatting this, as it is will run directly into the next cell of the first column if you don't put in spaces or .

There are lots of options for customisation, allowing you to create quite complex tables with a bit of tweaking.

print(
  xtable(df),
  include.rownames = FALSE,
  include.colnames = FALSE,
  hline.after = c(-1,0),
  add.to.row = list(
    pos = list(0,5,10),
    command = c(
      "& \multicolumn{4}{c}{4 column header} \\
       \cline{2-5}
      col1 & col2 & col3 & col4 & col5 \\ ",
      "\hline \multicolumn{5}{l}{Separator in table.} \\ \hline",
      "\hline \multicolumn{5}{l}{Notes at end of table.} \\ "
    )
  )
)

In this example I change the default settings for where xtable puts hline, allowing me to add the last hline above the notes - useful for explaining superscripts in the table.

Note also the use of cline{2-5} giving me a line over columns 2 - 5.

See gist for fully reproducible example.

这篇关于使用表格 + knitr for latex 的简单示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆