Add code to Bookdown gitbook template before TOC - r

I'm creating a gitbook using bookdown, and the designer wants to put a banner at the top of the book, that is, across the entire page above both the TOC and the chapter text.
If I use the before_body attribute in the YAML, bookdown inserts the code after the TOC, so the banner is only at the top of the chapter. Gitbook doesn't seem to allow for the template: attribute.
Any suggestions for how I can squeeze a line of HTML right after the < body > tag to make my designer happy? :)

I managed to solve this myself--it turns out that although officially the gitbook theme doesn't allow the template: attribute, in practice it does. So I just grabbed the existing template, hard-coded my banner after the < body > tag, and referenced my new template in the YAML. I hope this is helpful for anybody else who runs into this problem!

Related

Change right sidebar header in bs4_book (bookdown)?

I'm using the bs4_book format to set up the framework for a new book project using bookdown. Is there a way to change "On this page" at the top of the right sidebar to "In this chapter"? So far, I haven't been able to find where that text snippet lives.
Here is a solution:
I placed a short JavaScript function in the file local_js.html in the folder with the Rmd chapter files. The contents are:
<script>
$(function() {
$("#toc h2").html("In this chapter");
});
</script>
(Using JQuery 3.5.1)
#toc is the id for the right sidebar contents list, and the heading text is inside the h2 tags.
Then I added the following to the YAML header for the b4_book output:
output:
bookdown::bs4_book:
theme:
primary: "#637238"
includes:
in_header: local_js.html
This seems to work well enough, at least for a book framework that still hasn't got much text or image content in the chapters. I still haven't figured out where the default heading "On this page" is coming from. If anyone knows and can explain how to change it directly, I'd be grateful.

TOC not displaying in Blogdown post (Hugo theme Even)

I have added a new post to my site made using blogdown. Despite the presence of toc: yes in my YAML, the table of contents is not displaying on the rendered webpage. I've also tried adding:
output:
blogdown::html_page:
toc: yes
which doesn't solve the problem either. I'm currently using the Even theme (Hugo), and in the .config file, I also have toc: yes present.
I checked out this post, but I wasn't able to find the files they are referencing, nor those particular lines of code. Also my issue is with new posts, not the home page.
Does anyone know what the problem might be?
EDIT: I can see in my index.html file (generated from index.Rmd) that the code for a toc is present. For some reason, it's not being rendered on the page?
TL;DR Solution: to get the table of contents to show in the Even theme, your posts must render to the .markdown format instead of .html format. In order to make this happen, you simple use the .Rmarkdown file extension when creating the post instead of .Rmd. Now, when you use blogdown::serve_site(), your post will use the output format .markdown instead of .html. Your post will now render with a beautiful table of contents.
A note for latex math mode users: when typing in your .Rmarkdown document, you will now have to escape your $'s and \'s. So for example, $\le 0.2$
will have to be typed as \$\\le 0.2\$.
I hope this overly elaborate problem and solution is helpful to someone!
I figured out a workaround for this.
First, I'm not sure how to provide a reproducible example in this context, but I can say that if you open an RStudio project, run blogdown::new_site(theme = "olOwOlo/hugo-theme-even"), add a New Post using the Addin dropdown menu, add some headings to your post, make toc:true, use Serve Site from the Addin menu. You will see your post working fine in every respect - only it will be missing the table of contents.
Workaround:
I don't know why, but it appears that the table of contents will show if I keep a copy of the post in .md format, and delete the .html document. The default behavior seems to use the .html document if it is present; failing that, it will use the .md document.
To keep a copy of the post in .md format, use the following code in the YAML of your .Rmd document:
output:
blogdown::html_page:
keep_md: yes
Serve the site, delete the .html document (but keep the .md document), and the table of contents appears in all of its glory (you may have to actually knit the file to get the .md).
I don't know why this works...I've seen elsewhere (although I can't seem to find the post again) that some hugo themes don't play nicely with .html files, and prefer .md files, and that appears to be the case for the Even theme.
In sum: My workaround for now is thus to work on my posts, and when I'm done, knit the document with the option above, delete the .html file, and be on my way.
Caveat: blogdown seems to automatically re-render all the old posts when using Serve Site. According to this stack answer, Serve Site should only render documents that have undergone changes. Regardless, the old posts are most definitely being re-rendered, even when working on an unrelated post. I'm not sure if this behavior is attributable to blogdown per se, or the RStudio project.
I tried going to Tools -> Project Options -> Build Tools, and unchecking the boxes "Preview site when building", yet the behavior persists. Which means I will have to continuously delete these new .html files for all old posts every time a I make a change to any other file. Currently I only have one post, so not a big deal...yet.
If someone knows of a solution to this caveat, I would be extremely grateful. If I find one, I will edit this question and add it.
EDIT: it appears that blogdown will re-render the old post if there is no .html document present. Otherwise, it does not re-render it.
EDIT 2: Aha! According to the answer to this question, simply changing the file extension of the blog post from .Rmd to .Rmarkdown will produce a .markdown document instead of a .html document.

bookdown with specific latex template

I very much believe that Rmarkdown and bookdown are the future of content publishing, however some publishers use specific LaTeX templates, and in order to submit the manuscript they need it in that precise template.
For example, see this Springer manuscript template:
http://resource-cms.springer.com/springer-cms/rest/v1/content/20566/data/v3/monographs
Is it possible to make bookdown use this template in order to produce a PDF file?
EDIT: in this book (https://bookdown.org/yihui/bookdown/) on section 4.2 Theming I read it is indeed possible, but I might end up designing a "custom Pandoc LaTeX template".
I guess the question is now about how to design this Pandoc template, and section 4.3 provides some details about it.
You don't really have to use a custom Pandoc LaTeX template (of course you can if you want). Tweaking a few options and you will be done. I just put an example in the bookdown-demo repository. See this commit for what I changed, and see here for a PDF example.
You almost surely still have to tweak other things, one of which might be the index page. I also made an example for that.

In RStudio, making slide deck with RMarkdown, how can I easily change the theme?

I'm writing an .rmd file in RStudio, and using the Knit button to create a slide deck. I have two options, it appears, ioslides and slidy. I don't really like the default look-and-feel of either. (I think ioslides uses screen real estate inefficiently, and slidy feels like a graphic designer wasn't involved.)
I did a web search for alternative templates/styles/themes, and came upon pages like this gallery, but the tools there seem to be for use outside RStudio, as completely independent projects. In other words, they're not just themes for what I've got, but entirely other solutions that would change my workflow.
Is there a simple way to tell RStudio to use a different template/theme with either ioslides or slidy? For example, can I download a CSS file from somewhere, and point RStudio to it with the css: option in the yaml header of my .rmd file? If so, is there a gallery of such CSS templates?
Here's what I ended up doing. It's not as smooth as I'd like it to be, but it's decent. I'm glad to entertain better answers.
Download any of the style files linked to from this knitrBootstrap code into the same folder as your R Markdown file.
Edit it so that the URL at the start of the file begins with http: so that knitr knows not to look on your filesystem for the additional resources (fonts), but on the Web.
Edit it further to remove any pipes (|) in that URL, because knitr can't handle them (as far as I can tell). (Remove everything after the pipe, too.)
Edit it further to change everything of the form url('../fonts/***.eot'); to now instead be of the form url('https://netdna.boostrapcdn.com/bootswatch/3.0.0/fonts/***.eot');.
Link to it from your R Markdown file with the css: attribute in the YAML header.
Have you tried using the built in Bootswatch themes? They're fairly limited, but in doing so you can quickly customise many elements.
e.g
title: "Test"
author: "John"
date: "5 June 2017"
output:
slidy_presentation:
fig_width: 12.5
font_adjustment: -1
theme: readable
Other themes may be found here Bootswatch.
edit: removed a typo

How to change the position of the table of contents in rmarkdown?

With RStudio and knitr I see that I can add a TOC with the following code in my .rmd file.
----------------
output:
html_document:
toc: yes
-----------------
However, this places the TOC at the very beginning of the HTML document. Is there a way to move the TOC lower on the page? Say after an introductory paragraph?
I tried to use __TOC__ and __FORCETOC__ but it did not change the TOC position.
The position of the TOC is fixed in the R Markdown default HTML template. If you want to change its position in the document, you'll need to modify the template:
Make a copy of the R Markdown HTML template to use as a starting point. You can find it by running this R command: system.file("rmd/h/default.html", package="rmarkdown")
Move the $toc section to where you want the table of contents to appear.
Save the modified template in the same folder as the document you're rendering as e.g. lowertitle.html
Add template: lowertitle.html to the html_document settings.
From the standpoint of the template, all of the document's content is an atomic unit, so it might be necessary to put any content you want to appear before the TOC in the template itself.
You can use JQuery to relocate the TOC to an arbitrary position in the file. Simply insert a heading where you want the TOC to go, and use the ID generated by rendering the R Markdown file. For example:
<script>
// Move TOC to the Table of Contents heading (with id "table-of-contents")
$(function() {
$( "#TOC" ).insertAfter( $( "#table-of-contents" ) );
});
</script>
A heading called "Table of Contents" somewhere in the R Markdown file will receive id "table-of-contents". The TOC has id "TOC". The Jquery bit above selects that TOC, and inserts it after the "Table of contents" heading, wherever in the document it's located.
I tried the proposed answers and although they seem simple enough I could not achieve either. I found an easier solution proposed by #gadenbuie in GitHub:
https://gist.github.com/gadenbuie/c83e078bf8c81b035e32c3fc0cf04ee8
You just need to copy and paste the function to render your TOC in your Rmarkdown file and then recall it in a chunk where you want your TOC to appear. Therefore, you can recall your TOC wherever you want it in the Rmarkdown file.
If you need further explanation, you can check out #gadenbuie's blog:
https://www.garrickadenbuie.com/blog/add-a-generated-table-of-contents-anywhere-in-rmarkdown/
You could add something in a header which will come after title and before toc, e.g. a yaml header like:
And then add whatever paragraph or figures to the file header.md that you want.
As an example, I set this YAML up in the README of brshallo/piececor so that I could have my 'lifecycle' badge show-up prior to my table of contents.

Resources