adding rows or boldify single row.names with print

2020-03-22 06:28发布

I try to use print.xtable add.to.row to get table formatted like this:

sports

share of ballers    22.3 
share of skiers      4.6

addiction

share of smokers    20.3 
share of drinkers    6.6

My R table does contain the additional row.names even if these rows don't contain any values. I used the add.to.row option to add colors to the different rows like suggested here which worked fine. But what does not work is to add bold text with xtable or add additional hline between the rows. I always get an error message like:

Bad type area settings! The detected line width is about 52 % (typearea) larger than the heuristically determined line width.

So this could mean the table gets to big for its environment because of my changes, but do not have an idea what to do about it. Note I have reads posts like this one that modifies the xtable output itself, but even though this might possible for me to I am looking for an easier solution. Because if I go for this solution I had to capture.output and use regexp replacement to add something in between.

Is there a way around this? Or is there a simpler solution?

标签: r latex xtable
1条回答
孤傲高冷的网名
2楼-- · 2020-03-22 07:16

For the hline part, see ?print.xtable.

hline.after: When 'type="latex"', a vector of numbers between -1 and '"nrow(x)"', inclusive, indicating the rows after which a horizontal line should appear

To embolden all you rows names:

bold.allrows <- function(x) {
  h <- paste('\\textbf{',x,'}', sep ='')
  h
}
print(xtable(yourTable), 
      sanitize.rownames.function =  bold.allrows)

To embolden some row names, you can add a " special markup" to those rows, e.g. BOLD:

bold.somerows <- 
        function(x) gsub('BOLD(.*)',paste('\\\\textbf{\\1','}'),x)

print(xtable(yourTable), 
      sanitize.rownames.function =  bold.somerows)

e.g:

require(xtable)
hh <- head(mtcars)[ , c(1:5)]
## I want to bold 1 and 3 rows 
rownames(hh)[c(1, 3)] <- paste('BOLD', rownames(hh)[c(1, 3)])
print(xtable(hh), sanitize.rownames.function =  bold.somerows)
查看更多
登录 后发表回答