2013-10-07 38 views
4

我正在使用knitr和xtable來自動執行報告過程。我想突出顯示錶格的幾行,並在每行上方突出顯示一條水平線。我使用的.Rnw文件讀取如下:knitr xtable突出顯示併爲同一行添加水平線,

\usepackage{colortbl, xcolor} 
\usepackage{longtable} 

\begin{document} 

<<do_table, results = "asis">>= 
library(xtable) 
mydf <- data.frame(id = 1:10, var1 = rnorm(10), var2 = runif(10)) 

print(xtable(mydf), add.to.row = list(pos = list(0,2), command = rep("\\rowcolor[gray]{0.75}",2)),hline.after=c(0,2)) 
@ 

\end{document} 

這一切正常,但是,我有工作表應該是一個longtable,如果我調整的代碼的最後一行到

print(xtable(mydf), add.to.row = list(pos = list(0,2), command = rep("\\rowcolor[gray]{0.75}",2)),hline.after=c(0,2),tabular.environment="longtable",floating=FALSE) 

輸出是相當醜陋的,行沒有按預期突出顯示。任何人都可能知道這個問題的答案?

感謝,

大衛

回答

2

你是在正確的軌道上,但我有點困惑:你想選擇的行通過hlinerowcolor突出?根據我的經驗,rowcolor本身看起來更好,所以我會假設在下面的答案中(但是您可以輕鬆使用兩者,只需附加\\hline命令)。

作爲獎勵,下面的所有代碼都假設您使用LaTeX booktabs包,該包提供正確的加權規則(與Hline不同)。說實話,我總是使用booktabs,並且我無法調整代碼以使用hline - 但是如果您更喜歡hline,請將所有\toprule\midrule\bottomrule宏替換爲\hline

你似乎已經錯過了LaTeX的longtables需要一個特殊的頭,我們需要太提供,作爲一個元素到add.to.row列表command矢量(這可能是你排版表看起來不好的原因)。

longtable.xheader <- 
    paste("\\caption{Set your table caption.}", 
    "\\label{tab:setyourlabel}\\\\ ", 
    "\\toprule ", 
    attr(xtable(mydf), "names")[1], 
    paste(" &", attr(xtable(mydf), "names")[2:length(attr(xtable(mydf), "names"))], collapse = ""), 
    "\\\\\\midrule ", 
    "\\endfirsthead ", 
    paste0("\\multicolumn{", ncol(xtable(mydf)), "}{c}{{\\tablename\\ \\thetable{} -- continued from previous page}}\\\\ "), 
    "\\toprule ", 
    attr(xtable(mydf), "names")[1], 
    paste("&", attr(xtable(mydf), "names")[2:length(attr(xtable(mydf), "names"))], collapse = ""), 
    "\\\\\\midrule ", 
    "\\endhead ", 
    "\\midrule ", 
    paste0("\\multicolumn{", as.character(ncol(xtable(mydf))), "}{r}{{Continued on next page}}\\\\ "), 
    "\\bottomrule \\endfoot ", 
    "\\bottomrule \\endlastfoot ", 
    collapse = "") 

隨着該照顧的,繼續前進,print的xtable:

print(xtable(mydf), 
     floating = FALSE, % since longtable never floats 
     hline.after = NULL, % hline off since I use booktabs 
     add.to.row = list(pos = list(-1, 
           c(0, 2), 
           nrow(xtable(mydf))), 
        command = c(longtable.xheader, 
           "\\rowcolor[gray]{0.75}\n", 
           "%")), % comments out a spurious \hline by xtable 
     include.rownames = FALSE, % depends on your preference 
     include.colnames = FALSE, % depends on your preference 
     type = "latex", 
     tabular.environment = "longtable", 
     % xtable tries to escape TeX special chars, can be annoying sometimes 
     sanitize.text.function = function(x){x}, 
     % not all dashes are meant to be math negative sign, set according to your data 
     math.style.negative = FALSE) 

我希望我使用的答案booktabs並沒有混淆你太多。 繼續編織!

3

對不起,略微offtopic,但展示用於容易地突出顯示細胞/行的只降價溶液:

> mydf <- data.frame(id = 1:10, var1 = rnorm(10), var2 = runif(10)) 
> library(pander) 
> emphasize.strong.rows(c(1, 3)) 
> pander(mydf) 

--------------------------- 
id  var1  var2 
----- ---------- ---------- 
**1** **0.7194** **0.6199** 

    2  0.8094  0.1392 

**3** **-1.254** **0.5308** 

    4  0.4505  0.8235 

    5 -0.3779  0.7534 

    6 -0.3518  0.3055 

    7  1.759  0.5366 

    8  0.9822  0.9938 

    9  1.549  0.3589 

10  -1.077  0.5153 
--------------------------- 

即可以被轉換爲直接膠乳或PDF 。