kableExtra:垂直对齐在具有多列的 PDF 输出中不起作用

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

我想将 kableExtra table 中的所有列对齐到顶部。 valign = "top" 选项似乎没有解决这里的问题。 此外,由于某种原因,第三列以某种方式落在了第二列之上,而且引用也不起作用。

下面的 MWE 是基于这个相关的 SO 问题,它只需要 2 列:

有关 MWE 的屏幕截图,请参见此处:https://i.stack.imgur.com/RYg4r.jpg

想法是稍后在 Rmd 报告中使用 table(使用 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)

\\ after \includegraphics 在每个图像后生成一个新行,因此第 3、4、5 列移动到每个图像下方。只需删除 \\ 并添加 valign = TRUE,即

fig_path <- paste0("\includegraphics[valign = TRUE, scale = 0.5]{uw.png}")