如何在R Markdown PDF输出的脚注中包含PNG图标?

How to Include PNG Icons in Footnotes in R Markdown PDF Output?

提问人:estrix 提问时间:10/9/2023 更新时间:10/9/2023 访问量:32

问:

我正在尝试在我的 R Markdown 文档的脚注中包含小图标(另存为 .png 文件),该文档将转换为 PDF 文件。我希望这些图标在脚注中提供额外的视觉信息,但我在实现这一目标时遇到了困难。

我尝试使用 HTML 标签和 LaTeX 的 \includegraphics 包,但图标在 PDF 输出中无法正确显示。任何人都可以提供有关如何成功将这些图标包含在从 R Markdown 生成的 PDF 的脚注中的指导吗?

提前感谢您的帮助!

# Load the required libraries
library(kableExtra)
library(dplyr)

# Create example data 
data <- data.frame(
  Category = c("Toyota", "Honda", "Ford", "Nissan"),
  Count = c(50, 60, 45, 55),
  Minimum = c(100, 110, 95, 105),
  Maximum = c(200, 210, 195, 205)
)

# Define a vector with image links and descriptions in a footnote
footnote_text <- c(
  "Decreasing < -5% ![Arrow](Images/arrow_down.png)",
  "Slightly Decreasing -5 to -1% ![Arrow](Images/arrow_right_down.png)",
  "Stagnant -1 to +1% ![Arrow](Images/arrow_right.png)",
  "Slightly Increasing +1 to +5% ![Arrow](Images/arrow_right_up.png)",
  "Increasing > +5% ![Arrow](Images/arrow_up.png)"
)

# Create the table with adjusted column names
table <- kbl(data, booktabs = TRUE, col.names = c("Category", "Count", "Min", "Max")) %>%
  kable_styling(latex_options = c("striped"), font_size = 10, position = "center") %>%
  add_header_above(c(" " = 1, "(n)" = 1, "in Dollars" = 2)) %>%
  add_header_above(c(" " = 1, "Count" = 1, "Values" = 2))%>%
  add_footnote(footnote_text, notation="none")

# Display the table
table
R PNG Kable 脚注

评论


答:

0赞 Aleksandr 10/9/2023 #1

这应该可以完成这项工作。提供存储在文件夹中的图像。Images

基本上,您必须将png图像作为属性传递。然后将字符串显示为 html,意思是 .<img>add_footnote(..., escape = F)

library(kableExtra)
library(dplyr)
library(glue)

# Create example data 
data <- data.frame(
  Category = c("Toyota", "Honda", "Ford", "Nissan"),
  Count = c(50, 60, 45, 55),
  Minimum = c(100, 110, 95, 105),
  Maximum = c(200, 210, 195, 205)
)

footnote_text <- c(
  "Decreasing < -5%)",
  "Slightly Decreasing -5 to -1%",
  "Stagnant -1 to +1%",
  "Slightly Increasing +1 to +5%",
  "Increasing > +5%"
)
footnote_png_paths <- c(
  "Images/arrow_down.png",
  "Images/arrow_right_down.png",
  "Images/arrow_right.png",
  "Images/arrow_right_up.png",
  "Images/arrow_up.png"
)

display_annotation <- function(width = "20px", height = "20px") {
  glue_collapse(sapply(1:length(footnote_text), function(x) glue('{footnote_text[x]} <img src="{footnote_png_paths[x]}" alt="" width="{width}" height="{height}">')), '<br>')
  
}

# Create the table with adjusted column names
table <- kbl(data, booktabs = TRUE, col.names = c("Category", "Count", "Min", "Max")) %>%
  kable_styling(latex_options = c("striped"), font_size = 10, position = "center") %>%
  add_header_above(c(" " = 1, "(n)" = 1, "in Dollars" = 2)) %>%
  add_header_above(c(" " = 1, "Count" = 1, "Values" = 2))%>%
  add_footnote(display_annotation(), notation = "symbol", escape = F)

# Display the table
table

评论

0赞 estrix 10/9/2023
感谢您的深思熟虑。当我尝试将其编织成 PDF 时,您提出的解决方案给了我以下错误。!使用 \multicolumn 扫描文件时文件结束。<插入的文本> \par <*>测试.tex Fehler:LaTeX 编译测试失败.tex