I'm trying to add an image before the title in gitbook style bookdown project. This is similar to this question from the RStudio Community page, 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!
This question was answered here.
The solution is to add a JS codeblock to the end of the index.Rmd file, such as
```{js, echo = FALSE}
title=document.getElementById('header');
title.innerHTML = '<img src="/path/to/img.png" alt="Test Image">' + title.innerHTML
```
From https://bookdown.org/yihui/rmarkdown-cookbook/latex-logo.html
title: |
![](logo.jpg){width=1in}
Adding a Logo to LaTeX Title
NB: although you cannot see them, there are two trailing spaces after {width=1in}, which means a line break in Markdown. Without the line break, the image and the title would be on the same line, which may not be what you desire.
See also related question: How to have Frontpage Image before chapter title and book title?
Related
My blog is https://testfnm.blogspot.com based on the Blogger Emporio template.
On the homepage, under the titles of the snippets, I would like to add a second line of text with the country name.
So for instance, under the title "Milford Sound" I would like to add the country name "New Zealand". Similarly, under Amsterdam, I would like to add Netherlands. And so on...
How do I do that please?
As an alternative to adding a second line of text to the snippets, I have found a method to make the Labels permanently visible on the snippets of the homepage. I have put only one label per post, the label being the country name. Thus against each snippet a label (the country name) is now visible both on desktop and mobile.
I used this CSS code to make the labels visible:
.labels-outer-container {opacity: 1 !important;}
body.feed-view .post-labels {display: inline-block;}
You can see how the results look here: https://www.firozemistry.com
The example below (i.e. saved as a file index.rmd ) has the same code chunk to display 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)
```
# 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)
```
What follows is workaround to your Options 2 and 3, using Markdown and CSS to style images and HTML+CSS to style text; also, using base64 image (transparent gif) generator as an space separator between elements.
Beware of whitespaces! (at the end of each line - place two white spaces and hit ENTER)
Does one of these approaches/hacks work for you? If not, it would be better to delete the answer, it may be misleading to others.
---
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"
---
Using Markdown table to "style" images |-| (left-aligned), |:-:| (centered) and |-:| (right-aligned) will work well with simple RMarkdown outputs.
I realised that you have an image under # top level heading positioned at the very top of the page - with top:0px. causing image duplication and, possibly, hover problem:
<img src="https://i.imgur.com/GiViTbA.png" style="position:absolute;top:0px;height:100px;" />
replace with:
![](http://stackoverflow.com/favicon.ico){width=50px style="display: block; margin:0 auto;"}
and see what will happen.
---
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)
```
EDIT:
Let's try to find a common ground, A Minimal Book Example, github here.
Adjustments made in 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$.
index.Rmd output:
Adjustments made in 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).
01-intro.Rmd output:
With this solution we are "masking" a top level heading (# Introduction) with .png image, which will appear in the Table of Content as text.
Disadvantage: besides the obvious hack, image width must be at least equal or wider than top level heading title.
Note: 3 options are provided here, and none of them are perfect. The perfect solution may rely on modifications to the bookdown package?
Option 1:
Use includes with before_body like this in your _output.yml file (suggested here):
bookdown::gitbook:
css: assets/style.css
includes:
before_body: assets/big-image.html
after_body: assets/footer.html
Disadvantages:
i) Requires making a html file just to insert an image.
ii) If using a web-based image, won't show in Rstudio viewer.
iii) If using a local image, path can get mixed up and won't show in online web html rendering
iv) Includes image at the top of each chapter of bookdown if using before_body: my_image.html. Alternative option in_header: my_image.html does not seem compatible with the sidebar index.
Option 2
Insert image via yaml in index.rmd, using the solution under Tip 3 Add a Logo in your title/header/footer at this blog post
---
title: |
![](my_image.png)
My title
Disadvantages:
i) When you hover over the image, it displays a copy of the image in a slightly different location (Can this "Hover" behaviour be disabled?)
ii) If using a web-based image, won't show in Rstudio viewer.
iii) If using a local image, path can get mixed up and won't show in online web html rendering
Option 3
The code below borrowed from here (which you can place below the top level heading) presumably goes direct through the knitting process and inserts itself in the final html. The issue is that the image doesn't make room for itself and ends up over the first text. Is there some simple html/css to sort this out?
<img src="https://i.imgur.com/GiViTbA.png" style="position:absolute;top:0px;height:100px;" />
Disadvantages:
i) Image doesn't make room for itself and ends up over the first text. You can get around this with a hack by coding extra space (trial and error) through the yaml title like this:
title: |
.
.
.
.
site: bookdown::bookdown_site
EDIT
This is superseded by Radovan's accepted answer.
This was the best answer, taking option 3 from my previous answer, and combining a code approach to making the relevant space from Radovan's answer.
You will still have problems if you want to include a title in the YAML (I don't need this, as my title is in the image).
Also, on first loading the page, it presents nicely, but image is not seen if you go to the top of the document using the table of contents (the inheader approach used by this bookdown shows a better behaviour, but appears at the top of every chapter, which is not desired).
---
title: |
![](data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==){height=300px}
output:
html_document: default
---
# I'm a top level heading {-}
<img src="https://i.imgur.com/GiViTbA.png" style="position:absolute;top:50px;height:300px;align:center;" />
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)
```
The problem of hovering the image duplicating the image can be fixed by removing the line below from the css file that is generated at \libs\gitbook-2.6.7\css\style.css
.book .book-header h1 a:hover{color:inherit;text-decoration:none}#media (max-width:1000px){.book .book-header h1{display:none}}
as a result, this code below places the image right above the title:
---
title: |
![class='btn noHover'](images/Stats.png){width=400px style="display: block; margin:0 auto; align:center;" }
<center>GEOG380 Basic Stats with R</center>
---
I'm creating a document with rmarkdown (pandoc), which includes some bibliography in a .bib file. What I'd like to do is add a link to the title of the references, so that each of them links to a page of the form http://sample.com/citation-key.html, like this:
Author. (2017). Sample Title. Journal, 1(1), 1–2.
I've tried modifying the .csl file by adding prefixes and suffixes to the title, but everything I put in there is escaped, whether I use markdown or HTML syntax. Unfortunately, I can't change the .bib file. The relevant part of the .csl file is this:
<text variable="title"/>
Sample files are:
literature.Rmd:
---
output: html_document
bibliography: literature.bib
csl: literature.csl
---
#author2017word says this doesn't work.
## References
literature.bib
#article{author2017word,
author = {Author},
journal = {Journal},
number = {1},
pages = {1--2},
title = {{Sample Title}},
volume = {1},
year = {2017}
}
literature.csl: I'm using the APA style from here (line 231).
My pull request to citeproc adding support this feature was recently merged, and as a result, the latest release of Pandoc (v2.14.2) now hyperlinks titles by default when the citation does not already show a raw URL! From the citeproc readme:
When linkBibliography=True automatically linkifies any identifiers (DOI, PMCID, PMID, or URL) appearing in a bibliography entry. When an entry has a DOI, PMCID, PMID, or URL available but none of these are rendered by the style, add a link to the title (or, if no title is present, the whole entry), using the URL for the DOI, PMCID, PMID, or URL (in that order of priority). See Appendix VI of the CSL v1.0.2 spec.
In pandoc, this option is controlled by the link-bibliography metadata field, which is True by default.
it seams i look around all the file in
templates/mytemplate/html/com_content/category/blog.php
I could find there, how to edit category blog title and some other things too. But the most important thing i couldn't find, where can i edit article title, image, text. and make them to look as I want.
where it should be title, image and text i could find only a php code
$this->item = & $item;
echo $this->loadTemplate('item');
If anyone can guide me, which file i have to edit, to make my title of an article, image and text look as i want?
Once you have the override, find the code in:
your_template/html/com_content/category/blog_item.php
Code for title in category blog:
<?php echo JLayoutHelper::render('joomla.content.blog_style_default_item_title', $this->item); ?>
Code for Intro image in category blog:
<?php echo JLayoutHelper::render('joomla.content.intro_image', $this->item); ?>
If have a problem with the componentheading and the contentpagetitle.
I got a sectionblog for my articles. The title of the page is a componentheading. The title of the articles (intro) is a contentpagetitle.
If I click on the title of a article, I can see the entire article. The title of the page is now the title of the article (contentpagetitle), and that is good.
The problem is, this title now needs the same layout as the componentheading. But if I do this, the titles of the articles also get this layout in the sectionblog
How can I make sure I got 2 different styles, or the contentpagetitle changes in a componentheading while looking at the full article?
I you don't uderstand what I'm saying just go to http://csm.infowebpreview.be/nl/realisaties, and click on the first title 'Realisatie'. The full article opens and the title 'Realisatie' should be underlined now.
Or this is also correct: if an article is the only article on a page the title should get another layout
Thanks
You can do this by being more specific with the CSS selector -
This one will apply to all of the titles, make this one the one for the main heading with the underline -
h2.contentheadingrealisaties{border-bottom: 1px solid #333}
Then use this to single out the article titles in a listing:
.leadingrealisaties h2.contentheadingrealisaties{border:0;}
Obviously you will need to add in the rest of the CSS, but this will get you started.