Search code examples
rbookdown

Multiple headers/footers when using custom TeX in r bookdown


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:

How bookdown renders the LaTeX

The render should actually look like this:

The actual LaTeX render

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.

  1. How do I remove the auto-generated headers and footers?
  2. How do I make "Contents" disappear from the ToC table?

Thank you!


Solution

  • 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.