首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在fancyhdr中使用Sweave (R)代码?

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

Stack Overflow用户
提问于 2010-08-04 15:58:08
回答 2查看 919关注 0票数 3

我正在使用Sweave生成一个自动生成的定期报告。为了创建一个漂亮的头文件,我使用了fancyhdr包,到目前为止它工作得非常好。现在,因为我的报告是周期性的,所以我想动态地更改标题,而不将参数传递给函数。这就是为什么我写了一个小的R函数,它只是检查哪个周期是最新的。基于此,在R中生成报头串。

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

下面是我的代码:

代码语言:javascript
复制
   \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}}

这会导致以下错误:

代码语言:javascript
复制
Package Fancyhdr Warning: \fancyhead's `E' option without twoside option is use
less on input line 23.

这正是我的TexHeader所在的行。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-08-04 16:18:07

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

此外,在这种情况下,最好使用<<results=tex, echo=FALSE>>=。下面是一个例子:

代码语言:javascript
复制
\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
EN

Stack Overflow用户

发布于 2010-08-04 16:16:53

这只是一个警告,而不是一个错误。警告是关于您为偶数页添加了格式的事实,只有当您使用双面输出时,这才相关,在您的文档类中使用'twoside‘选项激活。否则,fancyhdr会将所有页面视为奇数

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3403629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档