latex - R Markdown : problems with Latex headers

标签 latex r-markdown

当我使用自定义 .sty 文件和 fancyhdr 包设置 header 时, header 文本会自动格式化,并且文本取自 .Rmd 文件中定义的第一个 header ,并且我无法覆盖此行为。例如,我有以下 R markdown 文件

---
output:
  pdf_document:
    includes:
      in_header: report.sty
    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...

```

这是 .sty 文件:

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

\AddToShipoutPicture{%
        \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}
%\addtolength{\swtPos}{0.5\swtLen}

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

\AddToShipoutPicture{\put(5,10){\rotatebox{90}{\scalebox{0.8}{\sidewaysText}}}}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}
\chead{I want this header}
\lfoot{\scriptsize
Here is some footer text.}

但是这是输出:

enter image description here

如您所见,“THIS IS SOME HEADER”以大写字母、斜体显示为标题的一部分,并右对齐。如何避免这种情况?

提前致谢!

最佳答案

加载后fancyhdrreport.sty 中,首先使用 \fancyhf{} 清除所有页眉/页脚。也就是说,

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}% Clear header/footer
\renewcommand{\headrulewidth}{0.4pt}% Header rule
\renewcommand{\footrulewidth}{0.4pt}% Footer rule
\chead{I want this header}% Centre header
\lfoot{\scriptsize Here is some footer text.}% Left footer

您只设置了 \chead\lfoot,因此页眉/页脚的其他四个组成部分(每个三个:left、centre、right)可能仍包含内容规范...无论它们是什么。 \fancyhf{} 清除所有这些,以便您可以根据需要进行设置。

当然,如果你只想清除head,也可以执行\rhead{}

关于latex - R Markdown : problems with Latex headers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38479833/

相关文章:

latex - 如何从 LaTeX 文档中提取重要的文本内容

r - 如何在 R markdown 中向 fig.cap 添加 LaTeX 符号?

r-markdown - 是否有在 r studio 中创建 python block 的快捷方式?

r - 使用紧凑数字引用样式时显示所有使用的引用文献

plot - 在 R Markdown/knitr 中缩略图一些图的简单方法是什么?

emacs - 排除 LaTeX-mode-hook 是个好主意吗?

r - 在苏门答腊 PDF 阅读器中打开编译的 .rnw 的自定义函数?

Report/LaTeX 质量输出包

latex -\newcommand 在 Kritr 中带有参数不起作用

r - 是否有直接在代码中增加 Xaringan 幻灯片的快捷方式?