how to insert image above bookdown table of contents

In the demo bookdown files, _output.yml contains some code to put some content before and after the TOC. I've modified it as follows, but the image is not included in the book build. How can I do this so that the image gets included in the static site folder, thus allowing the image to show up in the browser?

  css: style.css
      before: |
        <a href=""><img src="images/example.png" width="280"></a>
        <li><a href="./">An example Resource</a></li>
      after: |
        <li><a href="" target="blank">Published with bookdown</a></li>

I realize that what's happening is that the "|" symbol at the start of the line stops processing of the rest of the line and just includes it. However, if I remove that, won't I be working in YAML, rather than markdown? And I have no idea how to insert an image via YAML.

So either I need to figure out how to include this static image in the built book, even though it's never referenced in the markdown of the book, or I need to figure out how to use something other than HTML to insert it above the TOC. Either solution would be fine with me.

Any help greatly appreciated. Thanks in advance.

Is hosting your image in an online service (like imgur ?) something you could do ? This way your image would be available to your book as the img tag would linked to http.

Otherwise, you could also build your book using a makefile for example. First executing, render_book then copying the image into the _book folder. Would it work for you ?
An rscript to do that is also a solution.

In fact this is a hidden feature: You can use a Makefile or a _render.R script to be executed to render the book instead of rendering with render_book only. If _render.R is found, it will be executed, if not but a Makefile exists, it will be executed. If none of the 2 files exist then render_book("index.Rmd") is executed.
In one of the two scripts, you can put the logic you want pre-rendering and post-rendering. That is something used by some book like the Rmarkdown book

This is not really documented but you could find how it works by looking at the code of bookdown_site renderer.

At last, I am not sure the discovery of local resource would work for this html line in yml. I'll dig a bit in bookdown code to see about that :thinking:

@jtbayly in fact, after building my own example, this work perfectly for me. The local resource are detected from the html pages rendered. Where is your example.png ? it must be in your bookdown working directory in images/example.png, then it will be detected and moved to _book/images/example.png

I just added this in my _output.yml

 before: |
        <li><a href="./"><img src="img.png" width="280"></a></li>

with the file img.png in my bookdown-demo project directory. When building the book, the file is moved in _book and html book finds it and it appears on my browser.

1 Like

Thank you for both tips. I did have the image in a folder called images, but as you can see I included /images in my src url. I’ll try again without that and see if it works.

I’d rather not use an external image hosting service. Too many examples of files disappearing later using this method.

The other method sounds like it might be the answer to another question I asked on here about how to keep a small modification to one of the default JavaScript files after rebuilding the book. It sounds like I can just include a command in the _render.R file to make the change every time after rendering is complete.

1 Like

Odd. It's not working for me.

The image is at /images/img.png, and that images folder is in the same folder as all my .rmd files. It doesn't get moved into the _book folder, and furthermore, no image folder is even created in _book (unless there are images elsewhere in the book), and the src URL in the TOC doesn't get changed to /images/img.png. It remains img.png, so that wouldn't work, even if the image got moved.

I started with @yihui's demo book. Is the build process perhaps customized and different than yours?

I also customized the demo book.
Try with an image in no folder at the root of the directory. It should be moved.
The path does not need to be change in the HTML code because it will stay relative.

You should have a images directory with the png inside then a relative link to your image from the root dir. bookdown should find the relative path, check it exists then move.

Have you correctly put the file and the link ?
You can share directories structure and code in yaml.

Thanks for the help. I tried what you said and it still didn't work. I updated Rstudio, R, and all my packages, (which was not easy on a mac), and it works now, just like you said it should.

Edit: I should add I'm sorry for the trouble, and grateful for the help. I'm not sure why it never occurred to me to check my versions, first.

I was curious so I looked it up and it is a bug fixed in bookdown 0.14. For reference:

You may have had an older version with bug present.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.