I'm trying to add an image before the title in gitbook style bookdown project. This is similar to this question , but I would like the image to appear only before the title, not every top-level heading.
I've created a reprex here: https://bd-reprex.netlify.app/
I've edited the _output.yml
to include:
bookdown::gitbook:
...
includes:
before_body: assets/logo.html
However, as can be seen in the example above, the image now appears before every chapter, not just before the title.
Any help would be greatly appreciated!
MBN
May 28, 2020, 10:30pm
2
If you followed the bread crumb trail I left, did you try the option under EDIT in this answer?
r-markdown, bookdown
Yes, its a nasty hack, but it could get you out of trouble. Comment on the gituhub issue if you think appropriate.
opened 08:43PM - 26 Feb 20 UTC
closed 04:01AM - 11 Mar 20 UTC
The example below (i.e. saved as a file index.rmd ) has the same code chunk to d… isplay an image above and below the top level heading, but the image doesn't appear above the top level heading. This occurs **if** there is a file _output.yml with only this entry bookdown::gitbook: in the same directory.
That line seems to enforce a table of contents (which I want) and that appears to strip out anything (image or text) before the first top level heading by default (which I don't want) - so can this behaviour be modified?
````
---
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)
```
# I'm a top level heading, but please let me have my image above! {-}
```{r echo=FALSE, message=FALSE, warning=FALSE}
library(imager)
im <- load.image(system.file('extdata/Leonardo_Birds.jpg',package='imager'))
plot(im, axes=FALSE)
```
````
From a post at rstudio community, I received the option of editing the ```_output.yml``` with
```
includes:
before_body: assets/big-image.html
```
```includes``` is an option that will probably work, but it feels unnecessary - how about ```bookdown::gitbook:``` doesn't constrain the user to its ideal scenario of "nothing can exist above the top level header, therefore I will ignore anything that is there" :) - unless there is a really good reason I'm not aware of?
[Link to stackoverflow post: ](https://stackoverflow.com/q/60271684/4927395 )
[Link to rstudio community post:](https://community.rstudio.com/t/add-an-image-to-rmarkdown-output-to-bookdown-before-top-level-heading/53914)
The ```index.rmd``` and ```_output.yml``` are in the following zip
[testimage2.zip](https://github.com/rstudio/bookdown/files/4258063/testimage2.zip)
---
By filing an issue to this repo, I promise that
- [x] I have fully read the issue guide at https://yihui.org/issue/.
- [x] I have provided the necessary information about my issue.
- If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
- If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included `xfun::session_info('bookdown')`. I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: `remotes::install_github('rstudio/bookdown')`.
- If I have posted the same issue elsewhere, I have also mentioned it in this issue.
- [x] I have learned the Github Markdown syntax, and formatted my issue correctly.
I understand that my issue may be closed if I don't fulfill my promises.
Thanks! I don't think this is exactly what I'm looking for, however. Ultimately, I'm knitting to both HTML (bookdown::gitbook
) and PDF (bookdown::pdf_book
). If I mess with the title
in the YAML header, then the PDF output is also affected. It seems like there should be a way to modify the CSS for the title, but so far I've been unsuccessful.
MBN
May 29, 2020, 4:23am
4
I also output to both pdf and html formats - but using different hacks for each (I know!)
For pdf, I have an image in a pdf, with this code in before_body.yml
\includepdf[pages={1}, scale=1]{Cover_image_1.pdf}
\newpage
For html, here's some excerpts from my index.rmd
---
title: |
![](data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==){height=350px}
site: bookdown::bookdown_site
---
```
# Introduction {-}
<img src="my_image.png" style="position:absolute;top:50px;height:350px;align:center;" />
cderv
May 29, 2020, 7:01am
5
I think you can use Javascript for that. If you had this code at the end of your index.html, it should work I think. Tested on a minimal book.
```{js, echo = FALSE}
title=document.getElementById('header');
title.innerHTML = '<img src="https://cdn.pixabay.com/photo/2017/04/11/15/55/animals-2222007_960_720.jpg" alt="Test Image">' + title.innerHTML
```
The bookdown title is in a div of id header
so that helps. echo = FALSE
is required to include the JS code in your output html but without the chunk visible.
Hope it helps.
system
Closed
June 5, 2020, 7:01am
6
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.