user2641103 - 1 year ago 100
LaTeX Question

# R markdown: problems with Latex headers

When I setup a header by using a custom .sty file and the fancyhdr package, the header text is automatically formatted and the text is taken from the first header defined in the .Rmd file, and I cannot override this behavior. For instance, I have the following R markdown file

``````---
output:
pdf_document:
includes:
keep_tex: yes
geometry: tmargin=2cm, bmargin=2.5cm
classoption: a4paper
---

\pagenumbering{gobble}

# Behavioural profile of your dog

blah... blah...

```{r echo}

# Some R code here...

```
``````

And this is the .sty file:

``````%
% This file must be saved with UTF-8 encoding, otherwise pandoc will complain
% for instance about nordic characters.
%

\usepackage{palatino}
\renewcommand{\familydefault}{\sfdefault} % sans serif
\fontfamily{ppl}\selectfont
\usepackage{blindtext}
\usepackage{eso-pic, rotating, graphicx, calc}
\usepackage[nomessages]{fp}

% Code to add a vertical gray band in the inner margin :
\usepackage{eso-pic}
\definecolor{colorMarge}{RGB}{242,242,245}
\newlength{\distance}
\setlength{\distance}{0.0in} % 0.5in
\newlength{\rulethickness}
\setlength{\rulethickness}{0.3in} % 1pt
\newlength{\ruleheight}
\setlength{\ruleheight}{11in} % Longueur de la ligne
\newlength{\xoffset}
\newlength{\yoffset}
\setlength{\yoffset}{0.5\dimexpr\paperheight-\ruleheight}

\ifodd\value{page}%
\setlength{\xoffset}{\distance}%
\else
\setlength{\xoffset}{\dimexpr\paperwidth-\rulethickness-\distance}%
\fi
\AtPageLowerLeft{\put(\LenToUnit{\xoffset},\LenToUnit{\yoffset}){\color{colorMarge}\rule{\rulethickness}{\ruleheight}}}%
}

\newcommand{\sidewaysText}{R Development Core Team (2008). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. ISBN 3-900051-07-0, URL http://www.R-project.org.}

%\newlength{\swtLen}
%\setlength{\swtLen}{\widthof{\sidewaysText}}
%\newlength{\swtPos}
%\setlength{\swtPos}{\yoffset}

%\newlength{\swtPos}
%\setlength{\swtPos}{350em}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\footrulewidth}{0.4pt}
\lfoot{\scriptsize
Here is some footer text.}
``````

But this is the output:

As you can see, the "THIS IS SOME HEADER" appears as part of the header in capital letters, italics and aligned to the right. How can avoid this?

After loading `fancyhdr` in `report.sty`, clear all the headers/footers first using `\fancyhf{}`. That is,

``````\usepackage{fancyhdr}
\pagestyle{fancy}
You've only set `\chead` and `\lfoot`, so the other four components of the header/footer (three for each: `l`eft, `c`entre, `r`ight) may still contain content specifications... whatever they may be. `\fancyhf{}` clears all of these so you can set them as needed.
Of course, if you only want to clear the `r`ight `head`er, you can also execute `\rhead{}`.