在 R Markdown 中,无论输出类型如何(pdf、html、docx),都在每个页面上创建页眉/页脚 [英] In R Markdown, create header/footer on every page regardless of output type (pdf, html, docx)

查看:33
本文介绍了在 R Markdown 中,无论输出类型如何(pdf、html、docx),都在每个页面上创建页眉/页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想补充一个问题 使用 R markdown 为每个页面(包括第一页!)创建页脚.那里的代码(也在下面)在编织到 pdf 时对我来说非常好.但我不会得到 html 或 docx 输出的页眉/页脚.

I'd like to add to the question Creating a footer for every page (including first!) using R markdown. The code there (also, below) works perfectly fine for me when knitting to pdf. But I won't get header/footers for html or docx output.

在 R Markdown 中,无论输出文档的类型如何,如何为输出文档的每一页生成页眉/页脚?

In R Markdown, what can I do to generate header/footers for every page of an output doc regardless of the type of output doc?

---
title: "Test"
author: "Author Name"
header-includes:
- \usepackage{fancyhdr}
- \usepackage{lipsum}
- \pagestyle{fancy}
- \fancyhead[CO,CE]{This is fancy header}
- \fancyfoot[CO,CE]{And this is a fancy footer}
- \fancyfoot[LE,RO]{\thepage}
output: pdf_document
---
\lipsum[1-30]

推荐答案

您可以为文档的 html 和 Word 版本中的页眉和页脚添加 YAML 指令.下面是 YAML 的样子.解释如下.

You can add YAML instructions for headers and footers in html and Word versions of the document. Below is what the YAML looks like. Explanations follow.

---
title: "Test"
author: "Author Name"
output:
  html_document:
    include:
      before_body: header.html
      after_body: footer.html
  pdf_document: 
  word_document: 
   reference_docx: template.docx
header-includes:
  - \usepackage{fancyhdr}
  - \usepackage{lipsum}
  - \pagestyle{fancy}
  - \fancyhead[CO,CE]{This is fancy header}
  - \fancyfoot[CO,CE]{And this is a fancy footer}
  - \fancyfoot[LE,RO]{\thepage}
---

html 页眉和页脚

如上面的 YAML 所示,对于 html 输出,您可以使用 before_body: 在单独的 html 文件中指定页眉和页脚after_body: 标签.例如,要获取标题后跟规则行,header.html 文件可能如下所示:

html header and footer

As shown in the YAML above, for html output you can specify the header and footer in separate html files using the before_body: and after_body: tags. For example, to get a header followed by a rule line, the header.html file could look like this:

This is a header
<hr>

Word 页眉和页脚

knitr 的作者 Yihui Xie 在这里解释了如何做到这一点另请参阅此 SO 答案).您创建一个具有所需样式的 Word 文件,然后将该文件保存在本地目录中(或者,如果该文件在另一个目录中,您可以提供该文件的路径).然后使用 reference_docx: YAML 标记将 knitr 指向该文档.我刚刚打开了一个新的 Word 文件并添加了页眉和页脚,然后将文件保存为本地目录中的 template.docx.

Word header and footer

Yihui Xie, author of knitr, explains how to do this here (also see this SO answer). You create a Word file with the styles you want and then save that file in the local directory (or you can provide a path to the file if it's in another directory). Then you use the reference_docx: YAML tag to point knitr to that document. I just opened a new Word file and added a header and footer and then saved the file as template.docx in the local directory.

这篇关于在 R Markdown 中,无论输出类型如何(pdf、html、docx),都在每个页面上创建页眉/页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆