Klone
Klone

Reputation: 127

kable: Vertical alignment does not work with pdf output

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.

Exemple

Did anyone experience a similar behavior ?

Upvotes: 4

Views: 1666

Answers (1)

Dan
Dan

Reputation: 12074

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)
```

enter image description here

This example uses the photo from this Wikipedia page.

Upvotes: 3

Related Questions