如何在 fancyhdr 中使用 Sweave (R) 代码?

发布于 2024-09-12 18:21:07 字数 914 浏览 0 评论 0 原文

我正在使用 Sweave 生成​​自动生成的定期报告。为了创建一个漂亮的标头,我使用 fancyhdr 包,到目前为止效果非常好。现在,由于我的报告是定期的,我想动态更改标题而不向函数传递参数。这就是为什么我写了一个小 R 函数来检查哪个时期是最新的。 基于此,在 R 中生成标题字符串。

长话短说,我知道 LaTeX 中有 \today,但我需要使用来自 R 的特定信息,而不仅仅是日期。

这是我的代码:

   \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}}

这会导致以下错误:

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

这正是放置我的 TexHeader 的行。

I am generating an automatically generated periodical report with Sweave. In order to create a nice header I use the fancyhdr package which works really well so far. Now, since my report is periodical I want to change the header dynamically without passing an argument to the function. That's why I wrote a little R function that just checks which period is the latest.
Based on this a Header String is generated in R.

Long story short, I know that there is \today in LaTeX but I need to use the specific information coming from R, not just the date.

Here's 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's `E' option without twoside option is use
less on input line 23.

This is exactly the line where my TexHeader is placed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无人接听 2024-09-19 18:21:07

对于单面文档,您可以使用 \fancyhead[L]{...}\fancyhead[R]{...}

此外,在这种情况下,最好使用 <>=
这是一个例子:

\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}

For one-sided documents you can use \fancyhead[L]{...} and \fancyhead[R]{...}.

Furthermore, in this case, it's better to use <<results=tex, echo=FALSE>>=.
Here's an example:

\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}
几度春秋 2024-09-19 18:21:07

这只是一个警告,而不是错误。该警告涉及您为偶数页添加了格式的事实,这仅在您使用双面输出(使用文档类中的“twoside”选项激活)时才相关。否则所有页面都会被 fancyhdr 视为奇数

This is just a warning, not an error. The warning regards the fact that you have added formatting for your even pages, which is only relevant if you are using double sided output, activated using the 'twoside' option in your document class. otherwise all pages are treated as odd by fancyhdr

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文