在顶级标题之前将图像添加到 Rmarkdown Bookdown 输出

Mar*_*eal 5 r-markdown bookdown

下面的示例(即保存为文件index.rmd)具有相同的代码块,用于在顶级标题的上方和下方显示图像,但图像不会出现在顶级标题的上方。如果在同一目录中有一个只有这个条目的文件_output.yml,就会发生这种情况bookdown::gitbook:

该行似乎强制执行目录(我想要的)并且默认情况下似乎在第一个顶级标题之前删除任何内容(图像或文本)(我不想要) - 那么可以修改这种行为吗?

---
site: bookdown::bookdown_site
---

```{r echo=FALSE, message=FALSE, warning=FALSE}
library(imager)
im <- load.image(system.file('extdata/Leonardo_Birds.jpg',package='imager'))
plot(im, axes=FALSE)
```

# R Markdown

```{r echo=FALSE, message=FALSE, warning=FALSE}
library(imager)
im <- load.image(system.file('extdata/Leonardo_Birds.jpg',package='imager'))
plot(im, axes=FALSE)
```
Run Code Online (Sandbox Code Playgroud)

Rad*_*tić 5

接下来是选项 2 和 3 的解决方法,使用 Markdown 和 CSS 来设置图像样式,使用 HTML+CSS 来设置文本样式;另外,使用 base64 图像(透明 gif)生成器作为元素之间的空格分隔符。
小心空格!(在每行末尾 - 放置两个空格并按 ENTER 键)
这些方法/技巧之一对您有用吗?如果不是,最好删除答案,这可能会误导其他人。

---
title: |
  ![](www/image.png){width=300px}|  
  |:-:|  
  ![](www/image.png){width=300px style="display: block; margin:0 auto"}  
  ![](www/image.png){width=300px height=90px align=left}  
  ![](www/image.png){width=300px height=90px align=center}  
  ![](www/image.png){width=300px height=90px align=right}  
  ![](data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==){width=150px}  
  R Markdown Title  
  <center>R Markdown Title</center>  
  <p style="text-align: right;">R Markdown Title</p>   
  ![](data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==){width=150px}
author: "Author Name"
date: "08/03/2020"
---
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

使用 Markdown 表格来“设计”图像|-| (left-aligned), |:-:| (centered) and |-:| (right-aligned)可以很好地与简单的 RMarkdown 输出配合使用。

我意识到您# top level heading在页面最顶部有一个图像 - 带有top:0px。导致图像重复,并可能导致悬停问题:

<img src="https://i.imgur.com/GiViTbA.png" style="position:absolute;top:0px;height:100px;" />
Run Code Online (Sandbox Code Playgroud)

用。。。来代替:

![](http://stackoverflow.com/favicon.ico){width=50px style="display: block; margin:0 auto;"}
Run Code Online (Sandbox Code Playgroud)

看看会发生什么。

---
title: |
  ![](https://i.imgur.com/GiViTbA.png){width=300px style="display: block; margin:0 auto;"}  
  ![](data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==){width=50px}  
  R Markdown Title  
  ![](data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==){width=50px}
output:
  html_document: default
---

# I'm a top level heading {-}

![](http://stackoverflow.com/favicon.ico){width=50px style="display: block; margin:0 auto;"}

Note, you need to replace the image with a local image if you want to show the image in the rstudio viewer.
The image will be visible in the html file created when you knit, if you open in a browser connected to the internet.

```{r echo=FALSE, message=FALSE, warning=FALSE}
plot(cars)
```
Run Code Online (Sandbox Code Playgroud)

编辑

让我们尝试找到一个共同点,一个最小的书籍示例,github这里

作出的调整index.Rmd

--- 
title: |
  ![](data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==){height=300px}  
author: "Author Name"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
output: bookdown::gitbook
---

# Prerequisites

<img src="https://i.imgur.com/GiViTbA.png" style="position:absolute;top:50px;height:300px;align:center;" /> 

This is a _sample_ book written in **Markdown**. You can use anything that Pandoc's Markdown supports, e.g., a math equation $a^2 + b^2 = c^2$.
Run Code Online (Sandbox Code Playgroud)

index.Rmd输出:
在此输入图像描述

Chapter: Introduction( )中的调整01-intro.Rmd

# Introduction {#intro}

![](data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==){height=240px}

<img src="https://i.imgur.com/GiViTbA.png" style="position:absolute;top:50px;height:300px;align:center;" />

You can label chapter and section titles using `{#label}` after them, e.g., we can reference Chapter \@ref(intro). If you do not manually label them, there will be automatic labels anyway, e.g., Chapter \@ref(methods).
Run Code Online (Sandbox Code Playgroud)

01-intro.Rmd输出:
在此输入图像描述

通过此解决方案,我们用图像“掩盖”顶级标题 ( # Introduction) .png,该标题将作为文本显示在内容列表中。
缺点:除了明显的 hack 之外,图像width必须至少等于或宽于顶级标题。