【问题标题】:In R Markdown, create header/footer on every page regardless of output type (pdf, html, docx)在 R Markdown 中,无论输出类型如何(pdf、html、docx),都在每个页面上创建页眉/页脚
【发布时间】:2018-02-12 02:42:42
【问题描述】:

我想补充问题Creating a footer for every page (including first!) using R markdown。那里的代码(也在下面)在编织到 pdf 时对我来说非常好。但我不会得到 html 或 docx 输出的页眉/页脚。

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

---
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]

【问题讨论】:

    标签: r latex yaml r-markdown pandoc


    【解决方案1】:

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

    ---
    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:after_body: 标记在单独的 html 文件中指定页眉和页脚。例如,要获取一个标头后跟一条规则行,header.html 文件可能如下所示:

    This is a header
    <hr>
    

    文字页眉和页脚

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

    【讨论】:

    • 一切都对我有用,除了 html 页脚中的页码。有没有一种简单的方法可以在 html 输出中添加页码?
    • html 输出是单个 html 页面。你想怎么分页?
    • 我想将它们添加到每个打印页面。但这似乎并不简单。例如,this post
    • 嗯,这超出了我的html/css 工资等级,但如果您想出可行的方法,请发布您的解决方案。
    • 这在编织到 PDF 时不起作用。还有什么我必须做的吗?
    猜你喜欢
    • 2020-04-11
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 2015-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多