垂直对齐kable表格的列名

7
假设下面是一个例子:
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)

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

enter image description here

2个回答

6

我不确定是否有更简便的方法,但你可以使用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)
```

1
截至2020年8月,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")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接