Search code examples
rlatexr-markdownknitrpandoc

How to place a table in a PDF via rmarkdown or format the header text more precisely?


I would like the RMarkdown code below (at the end of the post) to output pages with one of the header options below. The main issue is how to achieve the formatting as I know how to get the date and page numbers.

I have googled for putting tables into the header, but haven't found anything. Formatting the data more precisely is an option too (that is Header Option 2), but I haven't found anything on how to do this. I have played around with formatting but nothing worth putting here that isn't already in the RMarkdown code below.

Question: is it possible to implement one of the options below ? And how can it be done.

Header Option 1: With Table including text formatting (including the : character if possible but that is not absolutely necessary).

enter image description here

Header Option 2: Formatted Similarly without the Table or this can even have a box around it.

enter image description here

Code for Creating a Multi Page Example PDF This is the item I would like to modify the header on to include either the table solution or just the formatted solution without the table.

---
title: "R Markdown Example With Numbered Sections"
output:
  bookdown::pdf_document2:
    toc: true
    toc_depth: 6
    number_sections: true
    includes:
        in_header: header.tex
header-includes:
- \usepackage{fancyhdr}
- \usepackage{lastpage}
- \pagestyle{fancy}
- \fancyhead[RO,RE]{Page \thepage\ of \pageref{LastPage}}
- \fancyhead[LO,LE]{Header Message}
- \fancyfoot[LE,LO]{Footer Messge on the Left}
- \fancyfoot[LE,RO]{Footer Messge on the Right}
---

\thispagestyle{fancy}

# Example R Rarkdown : Numbered Sections

## R Markdown

### Description

Some description text

\newpage

#### Details

Details go here.

\newpage

## Plots

Plots go here

\newpage

Last page


Solution

  • This is one way, no doubt there are others...

    1. Create a new fancy header page style
    2. Define the table style header (and remove some of the default behaviours)
    3. remove the head rule
    4. for some reason the first page does not like a header so force the header on the first page with \thispagestyle(...)
    5. define the subsequent page styles as the tableheader.

    Edit appearance as required...

    ---
    title: "R Markdown Example With Numbered Sections"
    output:
      bookdown::pdf_document2:  
        toc: true
        toc_depth: 4
        number_sections: true
    
    
    header-includes:
    - \usepackage{fancyhdr}
    - \usepackage{lastpage}
    - \usepackage{array}
    - \usepackage{lipsum}
    
    
    
    
    ---
    
    
    \fancypagestyle{tableHeader}{
        
        \fancyhf{}
        
        \setlength\headheight{90pt}
    
    \fancyhead[C]{
    \centering{
    \begin{minipage}{1.1\textwidth}
      \renewcommand{\arraystretch}{2}
        \begin{tabular}{|>{\centering\arraybackslash}m{0.2\textwidth}|>{\centering\arraybackslash}m{0.2\textwidth}|>{\centering\arraybackslash}m{0.2\textwidth}|>{\centering\arraybackslash}m{0.2\textwidth}|}
          \hline
          \multicolumn{4}{|c|}{\textbf{\LARGE{Title}}} \\ 
          \hline
          \textbf{\Large{Field 1:}}  & \textbf{\Large{Field 2:}}  & \textbf{\Large{Date:}}  & \textbf{\Large{Page:}}\\
          \textbf{\Large{Information}}  & \textbf{\Large{Information}}  & \textbf{\Large{`r Sys.Date()`}}  & \textbf{\Large{{\thepage} of \pageref{LastPage}}}\\ 
          \hline
         \end{tabular}
      \end{minipage}
      }}
    }
    
    \renewcommand{\headrulewidth}{0pt}
    
    \thispagestyle{tableHeader}
    
    \pagestyle{tableHeader}
    
    
    
    
    
    
    # Section 1
    
    \lipsum[3]
    
    ## sub section 
    
    
    \newpage
    
    \lipsum[2]
    
    

    enter image description here

    enter image description here