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
Add this outside of Rmarkdown code chuncks after you set the variable
\fancyhead[L]{`r variable_name`}
A combination of eipi10 's comment to my question (specifically, his link to the Rmarkdown/Rstudio webpage) and this post is what finally worked for me:
https://stackoverflow.com/a/35044316/3276842
Related
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).
Header Option 2: Formatted Similarly without the Table or this can even have a box around it.
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
This is one way, no doubt there are others...
Create a new fancy header page style
Define the table style header (and remove some of the default behaviours)
remove the head rule
for some reason the first page does not like a header so force the header on the first page with \thispagestyle(...)
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]
I am trying to learn Rmarkdown, but there is something that's quite elusive. I am trying to put my name at the top of every page of the PDF I print, however I also get the various headers from the text in the markdown file in the header, and it often overlaps and makes everything unreadable. I learned the basics from another thread on here, but still struggling. I only want my name in the header. I tried to modify it like what you see below, but then I don't get anything at all. This is my YAML:
---
title: "Something"
author: "something"
date: "42 42 42"
output: pdf_document
includes:
in_header: {My name}
fontsize: 12pt
header-includes:
\usepackage{setspace}
\onehalfspacing
---
Anyone who could write it so that I get only my name at the top of the page? Also, the onehalfspacing is the same as setting spacing to 1.5 in word right?
I think there is a conceptual problem: The things you put in includes: in_header: in the yaml are the files included in the preamble of your tex document, similar to what you have in header-includes:. This is not in any way related to the headline of your document.
If you like to change this, have a look at the fancyhdr latex package
Please also note that the syntax for multiline header-includes: is wrong in your example. You need to prefix each line with -
---
title: "Something"
author: "something"
date: "42 42 42"
output: pdf_document
fontsize: 12pt
header-includes:
- \usepackage{setspace}
- \onehalfspacing
- \usepackage{fancyhdr}
- \fancyhead[c]{your name}
- \pagestyle{fancy}
- \setlength{\headheight}{15pt}
---
test
\newpage
test
I'm currently having an issue with latex in Rmarkdown (using fancyhdr package). I am trying to make a data report with a certain template style. But as of right now, my heading1 for each page is not aligned properly. I assume it has something to do with my \headheight, but I am not sure how to resolve it.
This is currently done in R-markdown, and I am quite confident that the issue has something to do with my YAML. I would be very appreciated if anyone could tell me how to fix my YAML coding for \headheight or overall improving my coding for YAML to fix the overlapping issue.
There are no errors when I run this code, but this is the warnings that they provide me (but I have no idea how to resolve the warning)
Package Fancyhdr Warning: \headheight is too small (12.0pt):
Make it at least 32.08571pt.
We now make it that large for the rest of the document.
This may cause the page layout to be inconsistent, however.
This is my YAML:
---
output:
pdf_document:
latex_engine: pdflatex
header-includes:|
\usepackage{graphicx}
\usepackage{fancyhdr}
\pagestyle{fancy}
\newcommand{\helv}{%
\fontfamily{phv}\fontseries{b}\fontsize{9}{11}\selectfont}
\fancyhead[R]{\includegraphics[width=3cm]{logo.png}}
\fancyhead[L]{\textbf\selectfont\sffamily{Daily Report}}
\fancyfoot[L]{\textbf\selectfont\sffamily{NOT INTENDED FOR FORWARDING}}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}
classoption: a4paper
---
And these are in the rest of the codes:
```r-markdown
## \selectfont\sffamily{EQUITY 1}
# content page 1
\newpage
## \selectfont\sffamily{EQUITY 2}
# content page 2
I posted an image of the pdf output on TeXStackExchange: https://tex.stackexchange.com/questions/482525/having-problems-with-latex-coding-in-rmarkdown-fancyhdr-headheight
I would like all of them to be aligned with each other. I do not know if I have done my coding in YAML correctly.
The warning is pretty explicit that the headheight needs to be larger than 32.08571pt. This requirement can be fulfilled by adding e.g. \setlength{\headheight}{32.09pt} to your header-includes.
---
output:
pdf_document:
latex_engine: pdflatex
header-includes:|
\usepackage{graphicx}
\usepackage{fancyhdr}
\pagestyle{fancy}
\newcommand{\helv}{%
\fontfamily{phv}\fontseries{b}\fontsize{9}{11}\selectfont}
\fancyhead[R]{\includegraphics[width=3cm]{logo.png}}
\fancyhead[L]{\textbf\selectfont\sffamily{Daily Report}}
\fancyfoot[L]{\textbf\selectfont\sffamily{NOT INTENDED FOR FORWARDING}}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}
\setlength{\headheight}{32.09pt}
classoption: a4paper
---
I am trying to generate dynamic PDF reports from Rmarkdown with dynamic titles. To do this I use the function rmarkdown::render() in my R script and I pass in the argument params the title parametre as set.title = title.
The YAML of my Rmarkdown script I have the next code:
author: "Author test"
date: "`r Sys.setlocale('LC_TIME','C');format(Sys.Date(),'%B %d, %Y')`"
output:
pdf_document:
toc: yes
toc_depth: 5
keep_tex: yes
html_document:
theme: united
toc: yes
classoption: table
header-includes:
- \usepackage{array}
- \usepackage{float}
- \usepackage{xcolor}
- \usepackage{caption}
- \usepackage{longtable}
#- \usepackage{mulicol}
params:
set.title: title
title: "`r params$set.title`"
Once I run my code everything goes well and generate the different pdfs that I expect, the problem is that neither of these PDFs show the title, author and date. I am working with version 1.8 of rmarkdown package.
Can anyone help me with this issue?
For closing the conversation of this question I write how has been solved this question.
At the beginning I was thinking this issue was related with the code in YAML (where the title was informed from parameters) but eventually I discovered that there was nothing to do that. The problem was that I was using a the function cat() in the varible title that I pass as a parametre and this function changes the value of variable title into NULL.
For knowing exactly all the explanation about how to write dynamic titles in rmarkdown documents redirect yourself to the next entrance: Setting document title in Rmarkdown from parameters
I have some .Rmd files in which I need to make some changes to the layout in subsections. This example shows what I need to do.
This is an example Rmd code:
---
title: "test"
output:
pdf_document:
latex_engine: xelatex
number_sections: yes
toc: yes
toc_depth: 3
html_document: default
mainfont: Calibri Light
header-includes:
- \usepackage[dutch]{babel}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyfoot[LE,RO]{test}
- \usepackage{floatrow}
- \floatsetup[table]{capposition=top}
- \usepackage{dcolumn}
- \usepackage{here}
- \usepackage{caption}
- \captionsetup{labelsep=space,justification=justified,singlelinecheck=off}
---
# Article
## title
#### subsubsubtitle {-}
Here is some text
The result if generating pdf is:
But what I really want is:
So I want the text to be starting on the next line instead of right after the section header. (Also I don't want the subsection to be numbered and, that's why I put the {-} right after it.)
Does someone know how to manage this?
This addresses the Latex side of the problem.
The \paragraph, which is what \subsubsubsection is, just has different formatting and presentation. One of the things is that it doesn't start a new line. This is in pure Latex.
Ways around it:
The package titlesec allows you to customize the title appearance a lot. See this post.
Do it in Latex itself -- [re]define how \paragraph works -- see the above post, and/or this post.
Tweak it in the next itself, adding a newline. See below.
There may be a more direct way in .Rmd but I am not familiar with it and this has the Latex tag.
I have to address another aspect of this, without debating poster's purpose. Such deep hierarchy may indicate a need to rethink the structure. Does it help in making the document easier and more intuitive to use, or does it do the opposite?
With that out of the way, here are some direct ways of making it add a line, as requested.
Tweak it in the text itself, so that it takes a newline (\newline alone doesn't work).
\paragraph{title_text}
\mbox{ }\\
paragraph text here
Another way
\paragraph{title_text} \hspace{0pt} \\
paragraph text here
With titlesec package you can redefine the \paragraph, by changing [runin] to [hung]
\usepackage{titlesec}
\titleformat{\paragraph}[hung] % default is [runin]
{\normalfont\normalsize\bfseries}
{\theparagraph}{1em}{}
Or, can explicitly change the spacings
\usepackage{titlesec}
\titleformat{\paragraph}
{\normalfont\normalsize\bfseries}{\theparagraph}{1em}{}
\titlespacing*{\paragraph}
{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
For a succinct summary of the titlesec package see this post.
All this is straight-up Latex and I am not sure how it works with .Rmd.