kableExtra: Vertical alignment not working in PDF output with many columns

扶醉桌前 提交于 2020-06-13 07:06:28

问题


I would like to align all columns in a kableExtra table to the top. The valign = "top" option does not seem to solve the issue here. Also, the third column is somehow dropped on top of the second for some reason, and citations are not working too.

The MWE below is based on this related SO question, which only needs 2 columns: kable: Vertical alignment does not work with pdf output

For a screenshot of the MWE see here: https://i.stack.imgur.com/RYg4r.jpg

The idea is to use the table later in an Rmd report (using bookdown/huskydown).

   library(knitr)
   library(dplyr)
   library(kableExtra)

   num_obs <- 3
   text_string <- "Lorem ipsum dolor sit amet, vitae, augue aliquam luctus class non. Lectus maecenas ullamcorper commodo ut non maximus eros ad. Mollis rutrum bibendum ut ipsum nisl, mattis placerat, odio. Eu, non morbi nunc mollis." 
   fig_path <- paste0("\\includegraphics[scale=0.5]{uw.png} \\\\")
   fig_paths <- rep(fig_path, num_obs)

   table <- dplyr::tibble(
     col1 = 1:num_obs, 
     col2 = fig_paths,
     col3 = LETTERS[1:num_obs],
     col4 = c("\\href{http://eng.wikipedia.org}{Wiki}", "\\cite{R-base}", "\\cite{R-base}"),
     col5 = c(rep(text_string, num_obs))
   )

   kable(
     table, 
     escape = FALSE, # needed to be able to include latex commands
     format = "latex",  # format = latex, html
     booktabs = T, 
     align = "l",
     valign = "top", ## not working really
   ) %>% 
     kable_styling(full_width = F,
                   font_size = 9,
                   latex_options = c("hold_position")
     ) %>% 
     # row characteristics: set header row bold
     row_spec(row = 0, bold = T) %>%
     # column characteristics: set widths
     column_spec(1, width = "2em") %>% #
     column_spec(2, width = "6em") %>%
     column_spec(3, width = "3em") %>%
     column_spec(4, width = "20em") %>%
     column_spec(5, width = "10em") %>%
     # group rows together and give these groups labels
     pack_rows("Group 1", 1, 1) %>% 
     pack_rows("Group 2", 2, 3)

来源:https://stackoverflow.com/questions/60285820/kableextra-vertical-alignment-not-working-in-pdf-output-with-many-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!