Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical align kable's column name

Suppose the next example:

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

How can align vertically the second column name?

like image 943
aldo_tapia Avatar asked Feb 01 '18 12:02

aldo_tapia


2 Answers

I am not sure if there is an easier way, but you can go with the multirow package:

---
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)
```
like image 83
Martin Schmelzer Avatar answered Oct 08 '22 07:10

Martin Schmelzer


As of August 2020, The column_spec() function in the kableExtra package has a latex_valign argument. This only works if you also specify the column width, so it's not compatible with 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")
like image 24
even_of_the_hour Avatar answered Oct 08 '22 05:10

even_of_the_hour