rstudio / bookdown

Authoring Books and Technical Documents with R Markdown
https://pkgs.rstudio.com/bookdown/
GNU General Public License v3.0
3.77k stars 1.27k forks source link

cover-image not working #473

Closed eeenilsson closed 7 years ago

eeenilsson commented 7 years ago

Minimal example:

  1. Create the following .rmd file ("memoir.rm"):

title: Sometitle cover-image: test.jpg


Childhood

This is the beginning and the end.



2. Put a "test.jpg" in the same directory as the .rmd

3. Run the following

`bookdown::render_book("memoir.rmd", "bookdown::pdf_book")
`

No cover image appears.
yihui commented 7 years ago

cover-image does not work for PDF output. It is for EPUB only. For LaTeX/PDF output, you have to insert your cover image by yourself. (BTW, I don't know how.)

aldomann commented 5 years ago

I don't know if this is relevant anymore, but playing around, I've found a way of using a image/PDF cover when rendering to PDF format.

We just need to define a new pdf-cover-image (although conver-image would also work, but I prefer to use native PDF files for covers, as they scale better when zooming in) variable on the YAML header on my index.Rmd file:

title: "Some title"
pdf-cover-image: "images/cover.pdf"

Then, on our _output.yml file, we would have something like:

bookdown::pdf_book:
  includes:
    in_header: preamble.tex
  template: template.tex

Our preamble.tex file will need to load the pdfpages package:

\usepackage{pdfpages}

This template.tex file is a modified version of the default template included in RMarkdown, which includes the following change before the \maketitle call:

\begin{document}
$if(pdf-cover-image)$
\includepdf{$pdf-cover-image$}
$endif$
$if(title)$
\maketitle
$endif$

This effectively allows us to use a PDF file (or just an image; I've tested it) as the cover of the rendered PDF book, as shown in the example below:

image

I guess I could make a merge request on rmarkdown itself, as this seems to be a non-breaking implementation; if no pdf-cover-image is defined, it's simply ignored.

hugzee3 commented 4 years ago

@aldomann, I did pursue your proposed solution to having a cover pdf stitched to the book. However, for some weird reason, i kept getting the following error:

! LaTeX Error: The font size command \normalsize is not defined:
               there is probably something wrong with the class file.

I tried modifying the template class to include the documentclassaspect as suggested here

while this error wasn't encountered, I ended up with \title not defined and so on... How do i get to work with the template solution suggested?

Is there any other method available at this point in time in bookdown, otherwise? @yihui

nathansebhastian commented 4 years ago

Hi @hugzee3 Did you found a solution to your errors? I did all the steps mentioned by aldomann and used this as my template.tex to have my cover in PDF.

Hope that helps

cderv commented 4 years ago

@aldomann since a few version now, rmarkdown is using the latex template from Pandoc. So this would be a feature request for pandoc as the current template does not support this.

It only offers to customize the preamble, or a part before the body (but after the \maketitle call).

Otherwise, on the latex world, it seems that the titlepic Tex package can help as you only have to use in the preamble.

This is an example

---
title: My document
output:
  pdf_document:
    keep_tex: true
header-includes: 
  - \usepackage{titlepic}
  - \titlepic{\includegraphics[width=\textwidth]{Rlogo.pdf}}
classoption: titlepage
---

# First title

## Second title

For this to work, the Rlogo.pdf should be available at rendering time. 
This code does that

```{r}
if (!file.exists("Rlogo.pdf")) file.copy(file.path(R.home("doc"), "html", "Rlogo.pdf"), ".")

![image](https://user-images.githubusercontent.com/6791940/89991877-c0ffba00-dc84-11ea-94cc-2707a456ce65.png)

This would not require any change in templates.
aldomann commented 3 years ago

One minor issue with using titlepic is that (at least on my tests with bookdown::pdf_document2) is that it replaces the subtitle specified on the YAML header.

EDIT: actually it seems to redefine the entire title section (font sizes, spacing, etc), as seen here.

cderv commented 3 years ago

Hi @aldomann,

the link you share seems to be about titling and not titlepic. When I use titlepic only, it does not seem to change the title section using bookdown::pdf_document2

Did I missed something ?

aldomann commented 3 years ago

My bad, got my issues mixed up 😞. This was related to https://github.com/yihui/rmarkdown-cookbook/issues/272.

github-actions[bot] commented 3 years ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.