kable: Vertical alignment does not work with pdf output

时光毁灭记忆、已成空白 提交于 2020-07-09 06:14:59

问题


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 ?


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!