从 RMarkdown 生成 Word 文档时混合横向 + 纵向页面 [英] Mixed Landscape + Portrait pages when generating word document from RMarkdown

查看:187
本文介绍了从 RMarkdown 生成 Word 文档时混合横向 + 纵向页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a word document from RMarkdown-so far everything has been great except that I have to output a 4 page report, the first page being a portrait, the following 2 pages with ggplots being landscape and the last page with a ggplot in portrait.

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

I've tried every solution I can think of. I've made 4 pages in a reference docx that have the orientations I need, but that didn't work. I also tried this solution using a header.tex file that would contain this:

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

这个解决方案 我在 StackOverflow 上看到使用 pandoc_args 中的 lua 过滤器.

and this solution I saw on StackOverflow using a lua filter in pandoc_args.

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

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

The solution that would solve my problem would be this argument from the officedown package: https://github.com/davidgohel/officedown

<!---BLOCK_LANDSCAPE_START--->

Blah blah blah.

<!---BLOCK_LANDSCAPE_STOP--->

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

but unfortunately I cannot download it using devtools() off my work RServer where I need it. Is there any way I can use and manipulate some functions in officer (the non-markdown version of officedown) to get a mix of portrait and landscape orientations in my document? For example, the officer package has this function: 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")
}

谢谢!

推荐答案

昨天我能够使用 officedown 包.安装后,您可以转到 File >New File >RMarkdown 并选择 From Template,您可以在其中选择 Advanced Word 文档.他们在模板中为您提供的示例同时显示了纵向和横向.

Hi there I was able to do this yesterday using the officedown package. Once you install it you can go File >New File > RMarkdown and pick From Template where you can select Advanced Word document. The example they give you in the template shows both portrait and landscape.

这篇关于从 RMarkdown 生成 Word 文档时混合横向 + 纵向页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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