Search code examples
latexr-markdown

How to set the page color of pdf output of Rmarkdown?


I am searching around here to add a page color to pdf output of Rmarkdown but couldn'tfind any possible way to do. Is it possible to change the page color of pdf output of Rmarkdown? If yes, how to add color to the page? Thanks for any help.


Solution

  • If you want to change page background color, you can do this by using xcolor LaTex package. Before the section header, for example, here before the line ## R Markdown, you can specify the yellow as the background color of that page using \pagecolor{yellow}. Also you can define hex color in header-includes using \definecolor{color-name}{HTML}{hex-code} and use that defined color as page background color, as I defined mybgcolor here for the next page. (Note that, hex-color-code must contain all letters in CAPITAL)

        ---
        title: "Page color"
        author: "Shafee"
        date: '2022-07-15'
        output: pdf_document
        header-includes:
          - \usepackage{xcolor}
          - \definecolor{mybgcolor}{HTML}{97F0AF}
        ---
        
        ```{r setup, include=FALSE}
        knitr::opts_chunk$set(echo = TRUE)
        ```
        
        \pagecolor{yellow}
        ## R Markdown
        
        This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
        
        \newpage
        
        \pagecolor{mybgcolor}
        ## Including Plots
        
        You can also embed plots, for example:
        
        ```{r pressure, echo=FALSE}
        plot(pressure)
        ```
        
        Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
    
    

    Rendered pdf output

    Now 1st page looks like this

    yellow-color-background

    and 2nd page looks like this

    greenish custom defined color background

    Hope this helps.