How to use Sweave (R) code inside fancyhdr?

I am creating an automatically generated periodic report with Sweave. To create a good header, I use the fancyhdr package, which has worked very well so far. Now, since my report is periodic, I want to dynamically change the header without passing an argument to the function. This is why I wrote a little R function that simply checks which period is the last. Based on this, the title bar is generated in R.

In short, I know that LaTeX has \ today, but I need to use the specific information coming from R, not just the date.

Here is my code:

   \usepackage{fancyhdr}
 \pagestyle{fancy}

\renewcommand{\chaptermark}[1]{%
\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{%
\markright{\thesection\ #1}}
\fancyhf{}
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\rightmark{
<<>>=
print(TexHeader)@
}}
\fancyhead[RE]{\bfseries\leftmark}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{0.5pt}
\fancypagestyle{plain}{%
\fancyhead{}
\renewcommand{\headrulewidth}{0pt}}

which causes the following error:

Package Fancyhdr Warning: \fancyhead `E' option without twoside option is use
less on input line 23.

This is exactly the line where my TexHeader is located.

+3
2

\fancyhead[L]{...} \fancyhead[R]{...}.

, <<results=tex, echo=FALSE>>=. :

\documentclass[a4paper]{report}
\usepackage{fancyhdr}
\usepackage{lipsum}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{%
\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{%
\markright{\thesection\ #1}}
\fancyhf{}
\fancyhead[R]{\bfseries\thepage}
\fancyhead[L]{\rightmark{%
<<results=tex, echo=FALSE>>=
TexHeader <- format(Sys.time(), "%c")
cat(TexHeader)
@
}}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{0.5pt}
\fancypagestyle{plain}{%
\fancyhead{}
\renewcommand{\headrulewidth}{0pt}}

\begin{document}
\lipsum
\end{document}
+2

, . , , , , "twoside" . fancyhdr

+1

Source: https://habr.com/ru/post/1757958/


All Articles