- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 rmarkdown
包编写报告,但遗憾的是,我的现场报告习惯通常以 MS Word 文档形式提交。因此,我不能总是依赖 LaTeX
的强大功能,并且必须能够将我的 .Rmd 转换为 MS Word。现在,因为我希望能够从同一源文件创建 PDF 和 MS Word 文件,所以我正在尝试找到一种通用方法来执行此操作。我已经使用 apa6
LaTeX-document 类来处理 PDF。创建 Word 文件时,.Rmd 看起来像这样:
---
title: My title
abstract: This is the abstract.
author: John Doe
affiliation: Unknown
note: Nothing to say.
output:
word_document:
reference_docx: myreference.docx
---
Lorem ipsum.
我可以由此创建一个 Word 文档,但由于显而易见的原因,我的自定义 yaml
变量(例如摘要)将不会在文档中呈现。
基本上,我的问题如下:
创建 Word 文档时,如何在文档正文之前添加标题页(包括作者姓名、单位、作者注释等)和仅包含摘要的页面(“Lorem ipsum”)?这里的重点不是创建分页符(还有其他 open questions on this ),而是 **有没有办法让 pandoc
使用自定义 yaml
变量将它们放置在文档的开头并为其指定样式?
rmarkdown
包提供了 include()
函数,但它仅适用于 HTML 和 PDF 文档。
最佳答案
我发现可以在将 rmarkdown
生成的 Markdown 文件提交给 pandoc
之前对其内容进行自定义(例如添加和修改标题页)使用预处理器转换为 DOCX
。假设我们正在尝试在摘要之前添加 YAML 参数 note
中指定的一些信息(同时对摘要的支持已添加到 pandoc
中)。
为此,我们首先需要一个预处理器函数,用于读取输入文件并解析 YAML 前面的内容,并自定义输入文件:
my_pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir, output_dir, from) {
# Identify YAML front matter delimiters
input_text <- readLines(input_file, encoding = "UTF-8")
yaml_delimiters <- grep("^(---|\\.\\.\\.)\\s*$", input_text)
if(length(yaml_delimiters) >= 2 &&
(yaml_delimiters[2] - yaml_delimiters[1] > 1) &&
grepl("^---\\s*$", input_text[yaml_delimiters[1]])) {
yaml_params <- yaml::yaml.load(paste(input_text[(yaml_delimiters[1] + 1):(yaml_delimiters[2] - 1)], collapse = "\n"))
} else yaml_params <- NULL
# Modify title page
custom_lines <- c(
"NOTE:"
, metadata$note
, "\n\n"
, "# Abstract"
, "\n"
, metadata$abstract
, "\n"
)
## Add modified title page components after YAML front matter
augmented_input_text <- c(custom_lines, input_text[(yaml_delimiters[2] + 1):length(input_text)])
# Remove redundant default abstract
yaml_params$abstract <- NULL
# Add modifications to input file
augmented_input_text <- c("---", yaml::as.yaml(yaml_params), "---", augmented_input_text)
input_file_connection <- file(input_file, encoding = "UTF-8")
writeLines(augmented_input_text, input_file_connection)
close(input_file_connection)
NULL
}
现在我们需要定义一个利用预处理器的自定义格式:
my_word_document <- function(...) {
config <- rmarkdown::word_document(...)
# Preprocessor functions are adaptations from the RMarkdown package
# (https://github.com/rstudio/rmarkdown/blob/master/R/pdf_document.R)
pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir, output_dir, from = .from) {
# save files dir (for generating intermediates)
saved_files_dir <<- files_dir
args <- my_pre_processor(metadata, input_file, runtime, knit_meta, files_dir, output_dir, from)
args
}
config$pre_processor <- pre_processor
config
}
现在,您可以在渲染 R Markdown 文档时使用自定义格式,如下所示:
rmarkdown::render("./foo/bar.Rmd", output_format = my_word_document())
关于r - 针织/Rmd : Adding title page and text when converting to MS Word,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27744830/
我正在使用SpringBoot和JPA来调用db,我遇到异常 org.springframework.core.convert.ConverterNotFoundException: No conve
我尝试实现 Spring Converter,但在单元测试中出现错误: Kotlin: Null can not be a value of a non-null type TodoItem 如果我尝
我在 Spring Boot 2.0 示例中使用 Spring Data Redis。在此示例中,我尝试将客户数据 + 学生数据保存在一起。我不太确定这里的数据建模是如何发生的,但假设它与 Mongo
我在 Spring 的 XML 配置文件之一中有以下代码:
我们正在尝试使用 hibernate Converter 来加密/解密通过 hibernate 存储的几列数据 @Convert(attributeName="myattr",converter=Da
我有this我必须实现的功能: protected override ValidationResult IsValid( Object value, ValidationContext
我看到了 std::convert::Into有任何实现 std::convert::From 的实现: impl Into for T where U: From, 在Rust 1.0标准库
Convert.ChangeType 或 Convert.ToInt32 或 int.Parse 之间是否存在性能优势 最佳答案 如果您知道要将 string 转换为 Int32,使用 Convert
我会定期浏览我的家庭作业以供上课。我的扫描仪将原始 jpg 文件导出到 USB,然后我可以从那里使用 gimp 编辑文件并将其另存为 pdf。我发现一种节省时间的方法是将我的多页作业导出为 .mng
Grails版本:2.3.8我在BootStrap.groovy中注册了一个自定义日期编码器,但是当我使用日期填充为Json的Object时,它将引发异常:Exception message is C
我会定期浏览我的家庭作业以供上课。我的扫描仪将原始 jpg 文件导出到 USB,然后我可以从那里使用 gimp 编辑文件并将其另存为 pdf。我发现一种节省时间的方法是将我的多页作业导出为 .mng
我正在尝试制作一个 SKAction,以便我的玩家慢慢地被拉向一个要杀死他的敌人。实际上,问题在于玩家和敌人处于不同的节点,遵循以下层次结构: 场景(SKScene)-PARENT->播放器(SKNo
我通过 xml 设置了 spring data mongo 自定义转换器,如下所示 在自定义读/写转换器中,我想
我正在尝试使用名为 Simple Captcha 的 gem 这需要在机器上安装 ImageMagick。我已经安装了它并且 convert --version 显示了这个 Version: Imag
我正在尝试使用名为 Simple Captcha 的 gem 这需要在机器上安装 ImageMagick。我已经安装了它并且 convert --version 显示了这个 Version: Imag
我正在使用 Spring JPA,我需要有一个 native 查询来调用存储过程。从结果中,我只需要获取两个字段,即代码和消息。我创建了一个包含两个字段代码和消息的类。它不起作用,这是我收到的错误:
我首先有多部分文件,我想将其发送到camel管道并使用原始名称保存该文件。 我的代码: @Autowired ProducerTemplate producerTemplate; ...
我的maven项目使用了spring、hibernate。我得到“没有这样的方法错误”。我相信这是由于依赖项中的版本冲突造成的,但不知道是什么。构建成功。但是在“NetBeans:在 GlassFis
TL;DR:Vaadin 8 中是否有类似于 Vaadin 7 的转换器来更新 UI 中输入字段的表示? IE。在输入字段失去焦点后立即从用户输入中删除所有非数字,或将小数转换为货币? Vaadin
我昨天问了一个问题here关于从匿名对象读取属性并将它们写入类的私有(private)字段。问题解决了。这是一个小故事: 我有一些 json 格式的数据。我将它们反序列化为 ExpandoObject
我是一名优秀的程序员,十分优秀!