垂直对齐 kable 的列名

Vertical align kable's column name

假设下一个例子:

library(knitr)
library(kableExtra)

df <- data.frame(a = letters[1:10], b = 1:10)

names(df) <- c("This is a looooooong title, I don't know how to handle this... Also, I'm trying to extend this title even more... This column name will be used to compute vertical space","I want to align vectically this column name")

df %>% kable(format = 'latex', linesep = "", align = 'c') %>% kable_styling(full_width = T)

如何垂直对齐第二列名称?

我不确定是否有更简单的方法,但你可以使用 multirow 包:

---
title: "Test Book"
header-includes:
  - \usepackage{multirow}
author: "therimalaya"
output: 
  pdf_document:
    keep_tex: yes
---

# Hello World


```{r, error = TRUE, echo = T}
library(knitr)
library(kableExtra)

df <- data.frame(a = letters[1:10], b = 1:10)
names(df) <- c("This is a looooooong title, I don't know how to handle this... Also, I'm trying to extend this title even more... This column name will be used to compute vertical space","\multirow{1}{*}[0pt]{I want to align vectically this column name}")

df %>% kable(format = 'latex', linesep = "", align = 'c', escape = F) %>% kable_styling(full_width = T)
```

August 2020 开始,kableExtra 包中的 column_spec() 函数有一个 latex_valign 参数。这仅在您还指定列宽时才有效,因此它与 kable_styling(full_width = T)

不兼容
library(knitr)
library(kableExtra)

df <- data.frame(a = letters[1:10], b = 1:10)

names(df) <- c("This is a looooooong title, I don't know how to handle this... Also, I'm trying to extend this title even more... This column name will be used to compute vertical space","I want to align vectically this column name")

  df %>% kable(
        linesep = "",
        align = "c",
        format = "latex") %>% 
  column_spec(1:2, 
              width = "3in", 
              latex_valign = "m")