r - knitr xtable highlight and add horizontal lines for the same row, -
i using knitr , xtable automate reporting procedure. want highlight few rows of table , have horizontal line right above each row highlighted. .rnw file using reads below:
\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} this works fine, however, table working should longtable, if adjust last line of code
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) the output quite ugly, , rows not highlighted expected. might know answer question?
thanks,
david
you on right track, bit confused: want selected rows highlighted hline and rowcolor? in experience, rowcolor alone looks better, assume in answer below (but use both, append \\hline command).
as bonus, code below assumes use latex booktabs package, gives correctly weighted rules (unlike hline). honest, work booktabs, , couldn't bother adjust code use hline -- if prefer hline, replace \toprule, \midrule , \bottomrule macros \hline.
you seem have missed latex longtables require special header, , need supply element command vector of add.to.row list (this may reason typeset table looks bad).
longtable.xheader <- paste("\\caption{set 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 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 = "") with taken care of, go ahead , print xtable:
print(xtable(mydf), floating = false, % since longtable never floats hline.after = null, % hline off since 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 spurious \hline xtable include.rownames = false, % depends on preference include.colnames = false, % depends on preference type = "latex", tabular.environment = "longtable", % xtable tries escape tex special chars, can annoying sanitize.text.function = function(x){x}, % not dashes meant math negative sign, set according data math.style.negative = false) i hope use of booktabs in answer did not confuse much. keep knitting!
Comments
Post a Comment