问题
I am trying to produce a large table in pdf mixing text and figures using kable
. I am trying to align every row to the top. I made an example using a figure test.jpg located inside the working directory. I am using the version 1.22 of knitr
.
---
output: pdf_document
---
```{r}
table <- data.frame(
col1 = "test",
col2 = "![test](test.jpg){width=150px}")
knitr::kable(table)
```
It behaves correctly and aligns the figure and the text to the top if I knit it in html but it aligns the figure and the text at the bottom using the pdf. Specifying the valign
option does not change the behavior.
Did anyone experience a similar behavior ?
回答1:
I don't have a good solution, but I have a workaround. It's a cludge that uses the LaTeX package adjustbox
– specifically, valign = T
as an argument to includegraphics
. (The scale = 0.5
just makes the image 50% its original size.) I've also thrown in an escaped linebreak (\\\\
) for the sake of prettiness.
---
title: \textbf{Title}
author: \normalfont{Author}
output:
pdf_document
header-includes:
- \usepackage[export]{adjustbox}
---
```{r}
table <- dplyr::tibble(
col1 = LETTERS[1:3],
col2= c("\\includegraphics[valign=T, scale=0.5]{Osedax_roseus.jpg} \\\\",
"\\includegraphics[valign=T, scale=0.5]{Osedax_roseus.jpg} \\\\",
"\\includegraphics[valign=T, scale=0.5]{Osedax_roseus.jpg} \\\\"))
knitr::kable(table, format = "latex", escape = FALSE)
```
This example uses the photo from this Wikipedia page.
来源:https://stackoverflow.com/questions/55345679/kable-vertical-alignment-does-not-work-with-pdf-output