2010-08-04 37 views
3

我正在使用Sweave生成自動生成的定期報告。爲了創建一個漂亮的頭文件,我使用了迄今爲止效果很好的fancyhdr軟件包。現在,由於我的報告是週期性的,我想動態更改標題而不傳遞參數給函數。這就是爲什麼我寫了一個R函數來檢查哪一段是最新的。 基於此,在R中生成標題字符串。如何在fancyhdr中使用Sweave(R)代碼?

長話短說,我知道在LaTeX中有\今天,但我需要使用來自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放置線路。

回答

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} 
+0

恩,TexHeader在我的情況下返回一個字符串。只是沒有得到這個字符串從R到PDF ... – 2010-08-04 10:00:34

+0

這只是一個例子,'TeXHeader'也是一個字符串,可以被全局環境中的任何其他字符串替換。 – rcs 2010-08-04 11:41:09

+0

對不起大家的噪音。我的文檔又出現了編碼問題,因爲[expletive] [expletive] TeXShop在Mac OS Roman中編碼了文檔。無論如何+1有助於提高我的代碼! – 2010-08-04 22:32:03

1

這只是一個警告,不是一個錯誤。該警告指出,您爲偶數頁添加了格式,只有在您使用雙面輸出時才適用,使用文檔類中的「雙面」選項激活。否則所有頁面被視爲奇數由fancyhdr

+0

謝謝,你說得對。這不是真的導致問題的錯誤。 – 2010-08-04 22:32:32