r - 对齐表格中的文本和图像 - pdf 输出
问题描述
我正在使用 bookdown 制作一个 pdf 文档。在文档中,我有一个表格,在表格中的一些单元格中,我有 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 列中的文本在其单元格中垂直对齐(文本从单元格的左上角开始)?
解决方案
我在过去kable 中使用了@Lyngbakr 描述的解决方法:垂直对齐不适用于 pdf 输出
---
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)
```
推荐阅读
- pine-script - 是否可以获得由另一个 pine 脚本设置的 barcolor ?
- c++ - 从函数中获取名称
- package - 是否有任何编程语言或包管理器支持已安装包的一致性/融合/全局唯一性?
- firebase - Expo React-Native Firebase 视频上传
- reactjs - 如何在材质ui中应用h2 span css
- kubernetes - 节点 A 上的 Traefik 找不到节点 B 上的 pod(504 网关超时)
- c# - 获取字符串列表并按字符和字符定位对它们进行分组
- postgresql - Postgres 函数在 RETURN QUERY 中传递表引用和字符串
- amazon-web-services - 一段时间后 EC2 不再可用
- java - 使用 apache-poi 更新 excel 文件时出现 POIXMLException