在 table - pdf 输出中对齐文本和图像
Align text and images in a table - pdf output
我正在使用 bookdown 制作 pdf 文档。在文档中,我有一个 table,在 table 的一些单元格中,我有 png 图像。
我可以成功建书,但是图文对齐不匹配。看起来所有文本都与单元格底部对齐,而图像与单元格顶部对齐。
我尝试使用 pander
包而不是 kableExtra
并得到了完全相同的结果。
这是一个例子:
---
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")
```
产生这个:
是否可以让第 1 列和第 3 列中的文本在其单元格中垂直对齐(文本从单元格的左上角开始)?
我过去使用过@Lyngbakr 描述的解决方法
---
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)
```
我正在使用 bookdown 制作 pdf 文档。在文档中,我有一个 table,在 table 的一些单元格中,我有 png 图像。
我可以成功建书,但是图文对齐不匹配。看起来所有文本都与单元格底部对齐,而图像与单元格顶部对齐。
我尝试使用 pander
包而不是 kableExtra
并得到了完全相同的结果。
这是一个例子:
---
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")
```
产生这个:
是否可以让第 1 列和第 3 列中的文本在其单元格中垂直对齐(文本从单元格的左上角开始)?
我过去使用过@Lyngbakr 描述的解决方法
---
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)
```