Humberto R
Humberto R

Reputation: 79

R Markdown - YAML Header with Template PDF

I have a PDF design with cool colors that I want to use for my R Markdown output.

I asked Chat GPT and is still not working.

In the YAML header I have

---
title: "My Report"
author: "Truancer"
date: "`r Sys.Date()`"
output: 
  pdf_document:
    includes:
      in_header: "template.tex"
---

In the template.text I have:

\usepackage{pdfpages}
\usepackage{everypage}

\AfterEndEnvironment{longtable}{\clearpage}

\AtBeginDocument{
    \AddEverypageHook{
        \includepdf[pages=-, angle=0, scale=1, offset=0 0]{datamex.pdf}
    }
}

And the output error is giving me is:

! Misplaced \noalign.
\newpage ->\noalign 
                    {\break }
l.230 \end{longtable}

Error: LaTeX failed to compile beneficios_financieros_2.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See beneficios_financieros_2.log for more info.
Ejecución interrumpida

This is the PDF I want to use for the R Markdown output:

PDF

Any idea of how to solve this?

Upvotes: 0

Views: 33

Answers (1)

the-mad-statter
the-mad-statter

Reputation: 8886

The background package seems up to the task.

Notes

  1. If the header includes are succinct enough, using header-includes is a good choice.
  2. If you want the image on all pages set firstpage=false.
  3. See here about the everypage warnings.

YAML

---
title: "Example of Using LaTeX background Package"
output: pdf_document
date: "2025-02-13"
header-includes: |
  \usepackage[firstpage=true]{background} 
  \backgroundsetup{
    scale=1,
    angle=0,
    opacity=1,
    contents={
      \includegraphics[width=\paperwidth,height=\paperheight]{datamex.pdf}
    }
  }
---

Output

rendered document

Reprex files hosted with on GitHub

Upvotes: 0

Related Questions