【问题标题】:Mixed Landscape + Portrait pages when generating word document from RMarkdown从 RMarkdown 生成 word 文档时混合横向 + 纵向页面
【发布时间】:2020-07-23 21:57:29
【问题描述】:

我正在尝试从 RMarkdown 创建一个 word 文档——到目前为止一切都很好,除了我必须输出一个 4 页的报告,第一页是肖像,接下来的 2 页 ggplots 是横向,最后一页纵向显示 ggplot 的页面。

我已经尝试了所有我能想到的解决方案。我在参考 docx 中制作了 4 页,其中包含我需要的方向,但这不起作用。我还尝试使用包含以下内容的 header.tex 文件 this solution

\usepackage{lscape}
\newcommand{\blandscape}{\begin{landscape}}
\newcommand{\elandscape}{\end{landscape}}

this solution 我在 StackOverflow 上看到使用 pandoc_args 中的 lua 过滤器。

---
title: "Example"
output: 
  word_document:
    pandoc_args:
     '--lua-filter=page-break.lua'
---

解决我的问题的解决方案是 officedown 包中的这个参数:https://github.com/davidgohel/officedown

<!---BLOCK_LANDSCAPE_START--->

Blah blah blah.

<!---BLOCK_LANDSCAPE_STOP--->

但不幸的是,我无法在需要它的工作 RServer 上使用 devtools() 下载它。有什么方法可以使用和操作officer中的某些功能(officedown的非降价版本)来在我的文档中混合纵向和横向?比如officer包就有这个功能: https://github.com/davidgohel/officer/blob/master/R/docx_section.R

body_end_section_landscape <- function( x, w = 21 / 2.54, h = 29.7 / 2.54 ){
  w = w * 20 * 72
  h = h * 20 * 72
  pgsz_str <- "<w:pgSz w:orient=\"landscape\" w:w=\"%.0f\" w:h=\"%.0f\"/>"
  pgsz_str <- sprintf(pgsz_str, h, w )
  str <- sprintf( "<w:pPr><w:sectPr><w:officersection/>%s</w:sectPr></w:pPr>", pgsz_str)
  str <- paste0( wp_ns_yes, str, "</w:p>")
  as_xml_document(str)
  body_add_xml(x, str = str, pos = "after")
}

#' @export
#' @rdname sections
body_end_section_portrait <- function( x, w = 21 / 2.54, h = 29.7 / 2.54 ){
  w = w * 20 * 72
  h = h * 20 * 72
  pgsz_str <- "<w:pgSz w:orient=\"portrait\" w:w=\"%.0f\" w:h=\"%.0f\"/>"
  pgsz_str <- sprintf(pgsz_str, w, h )
  str <- sprintf( "<w:pPr><w:sectPr><w:officersection/>%s</w:sectPr></w:pPr>", pgsz_str)
  str <- paste0( wp_ns_yes, str, "</w:p>")
  body_add_xml(x, str = str, pos = "after")
}

谢谢!

【问题讨论】:

标签: r r-markdown pandoc tex officer


【解决方案1】:

您好,我昨天使用officedown 包能够做到这一点。安装后,您可以转到File &gt;New File &gt; RMarkdown 并选择From Template,您可以在其中选择Advanced Word document。他们在模板中为您提供的示例显示纵向和横向。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多