Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting a pdf BEFORE the bookdown frontpage

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
like image 703
Lena Avatar asked Apr 24 '17 12:04

Lena


3 Answers

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.

like image 174
antonio Avatar answered Nov 10 '22 22:11

antonio


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.

like image 43
Sukanya B Avatar answered Nov 10 '22 23:11

Sukanya B


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

like image 41
scoa Avatar answered Nov 10 '22 23:11

scoa