【问题标题】:Knit PDF from Rmarkdown with one image per page, vertically centered从 Rmarkdown 编织 PDF,每页一张图像,垂直居中
【发布时间】:2023-03-19 07:48:01
【问题描述】:

我有数百个图像,我想从 Rmarkdown 将它们编织成一个 pdf 文件。我希望每页只有一个图像,并且每个图像在页面上水平和垂直居中。我一直在玩 kable 包好几个小时试图完成,但没有运气。谢谢您的帮助。

这是一组可以使用的示例图像:

filenames <- structure(list(img_url = c("https://images.freeimages.com/images/large-previews/ce3/puppies-1-1308839.jpg", 
"https://images.freeimages.com/images/large-previews/006/young-dachshund-1362378.jpg", 
"https://images.freeimages.com/images/large-previews/20c/my-puppy-maggie-1362787.jpg"
)), row.names = c(NA, -3L), class = c("tbl_df", "tbl", "data.frame"
))

【问题讨论】:

  • 我没有意识到您在发布your comment 后提出了这个问题。你还没有评论我的回复...

标签: r r-markdown knitr


【解决方案1】:

您可以使用 glue 包在每个图像周围添加一些 LaTeX,这应该可以满足您的需求。

glue 允许您将变量直接放入字符串中,然后您可以使用 `results = 'asis' 块选项将其合并到编织文件中。

---
title: "One Image Per Page"
output: pdf_document
---

```{r one-per-page, results = 'asis', echo = FALSE}
library("glue")

# full path to all image files, URLs should work too
image_path_list <- c("/path/image1.jpg",
                     "/path/image2.jpg")

# create LaTeX output, alter the glue open delimiter to avoid conflict
glue('\\vspace*{\\fill}\n\\begin{center}\n\\includegraphics{{(image_path_list}}\n\\end{center}\n\\vspace*{\\fill}\n\\newpage\n', .open = "{(")

```

【讨论】:

  • 虽然很优雅,但该解决方案不会将页面上的图像垂直居中,而这正是我真正难以弄清楚的部分。您可以更新此答案以包含该修复程序吗?提前致谢。
猜你喜欢
  • 1970-01-01
  • 2010-12-30
  • 1970-01-01
  • 2016-07-01
  • 1970-01-01
  • 2012-09-29
  • 2013-03-04
相关资源
最近更新 更多