简体   繁体   中英

Caption length widening table kable extra + latex

Problem: My tables built with the wonderful kableExtra package look like this:

在此处输入图片说明

How can I force the caption to be several lines if necessary adjusting to the minimum width of the table and not the other way around?

carbon_benefits%>%
  set_names(c("Year","Date","Canary","Total","Date","Canary","Total"))%>%
kable(
    caption = "\\label{tab:carbon_costs}Carbon sequestration values over time.",
    booktabs = T,
    escape = F,
    linesep= "") %>%
  kable_styling(latex_options = c("hold_position"))%>%
  add_header_above(c("","Lower"=3,"Higher"=3))%>%
  footnote(general=paste("Combining the totals for all years for both species yields a lower bound of ",round(sum(carbon_benefits$total_lower),digits=0)," and a higher bound of ",round(sum(carbon_benefits$total_upper),digits=0),".",sep = ""),
           footnote_as_chunk = T)

Created on 2018-07-18 by the reprex package (v0.2.0).

You can use str_wrap from the stringr package to wrap the text into a reasonable line length:

footnote(general=
  stringr::str_wrap(paste0("Combining the totals for all years for both species yields a lower bound of ",round(sum(carbon_benefits$total_lower),digits=0)," and a higher bound of ",round(sum(carbon_benefits$total_upper),digits=0),"."), 
    width=txtwidth), 
  footnote_as_chunk = T)

txtwidth can then be set to be a calculation of a reasonable length based on the expected number of columns. That depends on your data structure. Looks like about 5-6 chars per column in this case, so 35-40 characters?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM