Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic headers in rmarkdown pdf output

I'm creating reports using what I think is a combination of Rmarkdown/Knitr, YAML, LaTeX and Pandoc (I don't fully understand which parts of my code pertain to what because I'm not well versed in anything except R).

I want to know how to add a "dynamic" header, where the contents of the header can be called from variables I read in, for eg., a value from a dataframe I import into the R session.

Specifically, I want to add, say, a unique ID# for every report, to be printed on every page of the report (not necessarily in the header), with a new unique ID# for every new report I generate, without having to manually change this information in the header section each time I make a new report.

This is my current header:

header-includes: 
   \usepackage{fancyhdr} 
   \usepackage{graphicx}
   \usepackage{eurosym} 
   \usepackage{booktabs} 
   \pagestyle{fancy} 
   \fancyhf{} 
   \addtolength{\headheight}{1.0cm}
   \rfoot{Page \thepage} 
   \usepackage{setspace}
   \setlength{\topsep}{0pt}
   \fancypagestyle{plain}{\pagestyle{fancy}}
   \renewcommand{\footrulewidth}{0.4pt}
   \iffalse
   \fi
   \rhead{\fontsize{28}{12}\selectfont My Report} 
   \lhead{\includegraphics[width=6cm]{path/to/my/png.png}} 
    \cfoot{some information about footer goes here }
    \usepackage{floatrow}
    \floatsetup[table]{capposition=top, objectset=centering}
    \setlength\parindent{0 pt}\setlength{\parskip}{0.0pt plus 0.0pt}
output: 
  pdf_document: 
    keep_tex: yes
    latex_engine: xelatex
geometry: "left=2cm,right=2cm,top=2.5cm,bottom=3cm"
mainfont: Calibri
fontsize: 12pt
like image 654
AHegde Avatar asked Nov 01 '25 09:11

AHegde


1 Answers

Add this outside of Rmarkdown code chuncks after you set the variable

\fancyhead[L]{`r variable_name`}
like image 81
Mnl Avatar answered Nov 04 '25 00:11

Mnl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!