Multiple headers/footers when using custom TeX in r bookdown - r

Loved bookdown. However, I'm porting my book from LaTeX into bookdown, and would like some help with headers/footers.
I have a custom code that generates headers and footers in my LaTeX:
% HEADER AND FOOTER MANIPULATION
% for normal pages
\nouppercaseheads
\headsep = 0.16in
\makepagestyle{mystyle}
\setlength{\headwidth}{\dimexpr\textwidth+\marginparsep+\marginparwidth\relax}
\makerunningwidth{mystyle}{\headwidth}
\makeevenhead{mystyle}{}{\textls[100]{\textsf{\small\scshape\thetitle}}}{}
\makeoddhead{mystyle}{}{\textls[100]{\textsf{\small\scshape\leftmark}}}{}
\makeevenfoot{mystyle}{}{\textls[100]{\textsf{\small\thepage}}}{}
\makeoddfoot{mystyle}{}{\textls[100]{\textsf{\small\thepage}}}{}
\clearmark{section} % removing section in the headers
\makeatletter
\makepsmarks{mystyle}{%
\createmark{chapter}{left}{nonumber}{\#chapapp\ }{.\ }}
\makeatother
% for pages where chapters begin
\makepagestyle{plain}
\makerunningwidth{plain}{\headwidth}
\makeevenfoot{plain}{}{}{}
\makeoddfoot{plain}{}{}{}
\pagestyle{mystyle}
% END HEADER AND FOOTER MANIPULATION
I placed my entire LaTeX preamble in preamble.tex (and I've not added content to the book just yet). It renders the following:
The render should actually look like this:
Also, here's what's in the YAML in index.Rmd.
documentclass: memoir
papersize: smalldemyvo
indent: yes
microtypeoptions:
- protrusion
- tracking
fontfamily: Alegreya
fontfamilyoptions:
- osf
fontsize: 11pt
output:
bookdown::pdf_book:
template: null
I'm new to bookdown, but I did read the documentation. I feel I need help with two things.
How do I remove the auto-generated headers and footers?
How do I make "Contents" disappear from the ToC table?
Thank you!

By default, bookdown uses some built-in template(s). If you need more adjustments, you could make your own .tex files (preamble.tex, before_body.tex, after_body.tex, template.tex) and specify them in the yaml part of index.Rmd like this:
bookdown::pdf_book:
includes:
in_header: preamble.tex
before_body: before_body.tex
after_body: after_body.tex
template: template.tex
bookdown merges preamble.tex, before_body.tex, and after_body.tex with the main body (marked as $body$ in template.tex) of your book into one .tex file in the structure under the control of template.tex, and compile it into a pdf book with pandoc and LaTex.
In your case, you have to insert your custom code into template.tex in the right way. #yihui's demo shows a good example.
For more examples to customize these .tex files, I would suggest that you could either look into the 'bookdownplus' package I developed, which gives you 19 examples showing how to tailor a LaTeX template into bookdown's frame, or read Chapter 8.3 Create Your Own Templates in the bookdownplus textbook.

Edited answer
bookdown has built-in templates (decided mostly by documentclass and/or Pandoc). Refer the Theming and Templates sections of bookdown documentation.
An example to learn from, as #pzhao suggested, is the template in the bookdown-chinese repo. Observe the variables that bring in data from the Rmd files. They're enclosed between $$, such as $body$. _output.yml has a reference to this template. This way, you override the default template, thereby eliminating duplicate headers/footers. Your LaTeX template decides what forms part of your headers/footers. An example is the LaTeX code in my question.
The appearance of Contents in the ToC table can also be controlled by tweaking your custom LaTeX template. I use the memoir class, and help to remove self-reference can be found here. My custom LaTeX template already took care of it, so it fixed itself as soon as I figured out how to use my custom template.

Related

R markdown landscape-only word document

I am trying to knit an .Rmd file to a word document that is entirely in landscape format.
Based on this post, I have tried using a reference doc. I created a Word document, changed the layout to landscape and saved it in the same folder as the .Rmd. However, this doesn't seem to work for me. Maybe I am missing something and saving a file in landscape format is not enough?
Previously, I was using the HTML comments/tags <!---BLOCK_LANDSCAPE_START---> and <!---BLOCK_LANDSCAPE_STOP---> from {officedown}, but since those can only be placed before and after code chunks, the title of the document would end up on a separate page that is portrait. Also, it gives a last page that is blank and portrait and that I don't know how to get rid of.
The current YAML header looks as follows:
---
title: "Summary Report"
output:
officedown::rdocx_document:
reference_docx: landscape_template.docx
---
I am also sure that the reference file is used because I get a warning in the console about not having set a Figure style:
Warning message:
paragraph style for plots "Figure" has not been found in the reference_docx document. Style 'Normal' will be used instead.
Does anyone have any suggestions on what I am missing?
After reaching out to the developer of {officedown} on GitHub, here is the very simple solution to the problem:
YAML header of .Rmd file:
---
output:
officedown::rdocx_document:
page_size:
orient: "landscape"
---

Bookdown in-chapter formatting: different pages per paragraph

We - bookdown noobs - are making a bookdown with a lot of text per paragraph (using the minimal bookdown as a template, we only added new Rmd files from here). At the moment, one has to scroll a long time before reaching the end of a chapter, because we have many paragraphs per chapter and all paragraphs are pasted on 1 page. In the bookdown example however, each paragraph appears on a separate page. I have looked in the code of the example and tried to copy some index code to ours, but that still pastes everything to one page when knitting to html.
How can we make sure each paragraph (e.g, 3.1, 3.2) appears on a separate page, instead of all paragraphs appearing on the same page (chapter 3 in its entirety)?
This is our index.Rmd code (don't know whether of any use):
site: bookdown::bookdown_site
documentclass: book
output:
bookdown::gitbook: default
R version 3.6.2 (2019-12-12); Bookdown version 0.18
Thanks very much in advance. I am a Latex/Pandoc noob so this may be a very stupid question.

How to add feature or thumbnail image for post in .Rmd file

I'm currently trying to setup a hugo blog with blogdown and can't find a way to add feature or thumbnail images to posts from within .Rmd files, which would like this with the tranquilpeak theme:
As far as I understand, it is easy to do in .md files by just adding some syntax like this:
+++
featuredImage = "img/foobar.jpg"
+++
I found this in the minos theme and in a discourse post. But how can I do this inside a .Rmd file?
It does not matter whether you use .md or .Rmd: if the theme supports the featuredImage option, you can also use it in .Rmd. The only thing you need to make sure is to write metadata in YAML instead of TOML if the post format is .Rmd (see documentation), i.e.,
---
featuredImage: "img/foobar.jpg"
---
One option is to change the .Rmd filename to index.Rmd and use the featured.png/jpg image in the page's folder.
Works fine for me.

outline/toc on the left and contents on the right layout using rmarkdown

I was checking out how to include a nice table of contents / document outline in my rmarkdown document (with html output).
Well, the "standard" approach:
html_document:
toc: true
It works, but I do not like it (not even tweaking options and themes).
But actually, in several pages of the documentation (e.g. http://rmarkdown.rstudio.com/html_document_format.html), they use a very nice layout that includes a table of contents / document outline to the left of the page and the contents of the right (and the outline syncs with the contents part, Highlighting where on the contents you currenly are).
This is EXACTLY what I want. And I guess they did it using rmarkdown. But I cannot find how to do it.
I've check that documentation page, as well as rmarkdown gallery, flex dashboard, web sites among others. But I can't seem to figure it out how to do it.
Well, using web sites I would be able to do it, but it seems overly complicated for what I want (you need to have several pages, and at least index.Rmd file and the _site.yml file).
Instead, I guess there should be an option to simply tell rmarkdown to take my single document and put the section headers in a nice frame to the left, as in the example page from RStudio.
Have you tried this approach? It works for me and it's also on the homepage, you provided:
---
title: "Untitled"
output:
html_document:
toc: true
toc_float: true
---
# header 1
# header 2
# header 3
With a little bit of text, it looks like that:

how to remove the title slide from Rstudio ioslides presentation

I am using R Studio and R Markdown to create an ioslides presentation. I would like to eliminate the title slide, and begin the presentation with a normal slide. So far, I have tried removing the title from the YAML options, but this just results in a blank title page.
How can I remove the title page altogether?
my YAML options
---
output:
ioslides_presentation:
widescreen: true
---
You can try to customise the template file. You can find the path of the default one in your system by typing rmarkdown:::rmarkdown_system_file("rmd/ioslides/default.html") in the console.
The easiest way to play with it would be to copy it to the directory of your project, rename it and put additional YAML option like template: custom_template.html. Even though this might not be enough to remove the title slide completely, you can always customise it so it looks like the first slide of your presentation.

Resources