Reputation: 1335
I am producing a pdf document using bookdown. In the document I have a table and in some of the cells in the table I have png images.
I can successfully build the book, but the alignment of figures and text does not match. It looks like all the text is aligned to the bottom of the cells whereas the images are aligned to the top of the cells.
I have tried using the pander
package instead of kableExtra
and got the exact same result.
Here is an example:
---
site: bookdown::bookdown_site
documentclass: book
output:
bookdown::pdf_book:
toc: false
includes:
delete_merged_file: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message=FALSE, cache=TRUE)
library(kableExtra)
library(knitr)
```
# Hello World
I think the world is flat
```{r}
flag = "http://flagpedia.net/data/flags/mini/gb.png"
download.file(flag,'flag.png', mode = 'wb')
tbl_img <- data.frame(
col1 = c("row1", "row2"),
col2 = c("",
""),
col3 = c(
"here is a whole bunch of text to show how the images and text in the table are not properly aligned.",
"here is a whole bunch of text to show how the images and text in the table are not properly aligned."))
tbl_img %>%
kbl(booktabs = T) %>%
column_spec(2, image = spec_image(
c("flag.png", "flag.png"), 600, 400)) %>%
column_spec(3, width = "5cm")
```
Which produces this:
Is it possible to get the text in columns 1 and 3 to be vertically aligned in their cells (text starting in the top left of the cell)?
Upvotes: 0
Views: 948
Reputation: 656
I have used the workaround described by @Lyngbakr in the past kable: Vertical alignment does not work with pdf output
---
site: bookdown::bookdown_site
documentclass: book
output:
bookdown::pdf_book:
toc: false
header-includes:
- \usepackage[export]{adjustbox}
delete_merged_file: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message=FALSE, cache=TRUE)
library(kableExtra)
library(knitr)
```
# Hello World
I think the world is flat
```{r}
flag = "http://flagpedia.net/data/flags/mini/gb.png"
download.file(flag,'flag.png', mode = 'wb')
tbl_img <- data.frame(
col1 = c("row1", "row2"),
col2 = c("\\includegraphics[valign=T,scale=2.5,raise=2mm]{flag.png}",
"\\includegraphics[valign=T,scale=2.5,raise=2mm]{flag.png}"),
col3 = c(
"here is a whole bunch of text to show how the images and text in the table are not properly aligned.",
"here is a whole bunch of text to show how the images and text in the table are not properly aligned."))
tbl_img %>%
kbl(booktabs = T, escape = F) %>%
kable_styling(full_width = T)
```
Upvotes: 1