Inserting a pdf BEFORE the bookdown frontpage
Asked Answered
L

4

11

I'm writing my thesis with Bookdown in Rmarkdown (and I will export it to PDF format eventually). To hand in my thesis, I am required to include a few scanned documents with handwritten signatures BEFORE the first page of my thesis.

Bookdown is amazing in combining different Rmd files, but I have not managed to include a PDF document BEFORE the title page.

I have tried this:

bookdown::pdf_book: 
  includes:
    in_header: preamble.tex
    before_body: beforebody.tex

With in preamble.tex :

\usepackage{pdfpages}

and in beforebody.tex:

\includepdf[pages={5}, scale=1]{OfficialFrontpages.pdf}
\newpage

However, then it first creates the Bookdown titlepage, and only inserts these pages before the Table of Contents. Is there any way to force this document to be included BEFORE the first page of the final PDF document?

Inclusion of these commands as the lines of the first .Rmd files also doesn't work:

![Caption](OfficialFrontpages.pdf) 
\raggedright 
\clearpage
Logsdon answered 24/4, 2017 at 12:41 Comment(0)
Q
2

Option 1: You can try something like this

includes:
    after_body:Title_page.tex

Option 2: Since I am more comfortable in latex, I would generate the main thesis and the official pages as two separate pdf documents, then use latex package pdfpages to combine these two.

Something like

\usepackage{pdfpages}

\begin{document}

\includepdf[pages={5}, scale=1]{OfficialFrontpages.pdf}
\newpage
\includepdf[pages={-}, scale=1]{MyThesis.pdf}

\end{document}

I would finally compile with pdflatex.

Let me know if this was useful.

Quadrisect answered 4/6, 2017 at 3:50 Comment(0)
M
2

One strategy is to edit the latex template so that you call your \includepdf is evaluated before \maketitle. It seems like bookdown is using the regular rmarkdown latex template. Let's make a copy and edit it. There are several alternative ways to do this:

  1. You could remove this part altogether:

    $if(title)$
    \maketitle
    $endif$
    

    And then write in your Rmd document:

    \includepdf[pages={5}, scale=1]{OfficialFrontpages.pdf}
    \maketitle
    
  2. Or you could move that part, the one that takes care of the before_body:

    $for(include-before)$
    $include-before$
    $endfor$ 
    

    before \maketitle, and use your current strategy with before_body.

Either way, you'll need to tell knitr to use your custom template:

bookdown::pdf_book:
  template: mytemplate.tex

And you may have to adjust some other parameters since knitr doesn't behave exactly the same when used with custom templates (for instance, you will probably need to redefine the margin size yourself).

Mali answered 4/6, 2017 at 12:42 Comment(2)
The link for rmarkdown latex template is broken. My bookdown project folder doesn't seem to have a .tex file by default that I can see. Can you repair link?Political
This link has some suggestions for location and what your default latex file might be called. linkPolitical
E
2

If you attach external PDFs as part of your Bookdown workflow, you add an unnecessary overhead to its build process (especially for scanned documents). Instead, you need the merge only when handling the product to admin staff.

One solution is to create a separate TeX and merge the documents via the LaTeX package pdfpages. This has already been posted here, but to me it seems a bit overkilling for two PDFs. I would simply go with:

pdftk OfficialFrontpages.pdf MyThesis.pdf cat output final.pdf

Depending on the size of the documents (figures and such), you might also benefit from adding compress as the last argument.

Of course, if you don't want to leave the comfort of the R console, nothing prevents from defining the function:

pdfMerge <- function(doc1, doc2)
    system2("pdftk", paste(doc1, doc2, "cat output final.pdf"))

pdftk is just one among many cross-platform utilities (Windows, Mac, Linux) to merge PDFs. An alternative is qpdf.

Exotoxin answered 10/6, 2017 at 10:50 Comment(0)
P
1

This is a simple approach but keeping everything within rmarkdown, combining the best of scoa's answer and using some tips from this other stackoverflow example.

It begins from the bookdown demo preamble.tex, two lines added at the bottom.

\usepackage{booktabs}
\usepackage{pdfpages}
\usepackage{amsthm}
\makeatletter
\def\thm@space@setup{%
  \thm@preskip=8pt plus 2pt minus 4pt
  \thm@postskip=\thm@preskip
}
\makeatother
\let\oldmaketitle\maketitle
\AtBeginDocument{\let\maketitle\relax}

Make this new file, before_body.tex, where you want page 1 of the pdf called cover_pdf.pdf as the cover for your document.

\includepdf[pages={1}, scale=1]{cover_pdf.pdf}
\newpage

\let\maketitle\oldmaketitle
\maketitle

Edit _output.yml to include your before_body.tex, resulting in this:

bookdown::gitbook:
  css: style.css
  config:
    toc:
      before: |
        <li><a href="./">A Minimal Book Example</a></li>
      after: |
        <li><a href="https://github.com/rstudio/bookdown" target="blank">Published with bookdown</a></li>
    edit: https://github.com/rstudio/bookdown-demo/edit/master/%s
    download: ["pdf", "epub"]
bookdown::pdf_book:
  includes:
    in_header: preamble.tex
    before_body: before_body.tex
  latex_engine: xelatex
  citation_package: natbib
  keep_tex: yes
bookdown::epub_book: default

Then you should be sweet!

If you have trouble, you can download or clone a working example from this github repo branch

Political answered 17/3, 2020 at 0:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.