lde*_*cco 6 r r-markdown bookdown
有没有办法通过 R 降价文件添加 MS Word“评论”?我正在使用 reference_docx,并且熟悉添加自定义样式......但还没有想出如何让评论显示在这样的一边:
澄清一下:我想在我的纯文本 Rmd 文件中添加一个标签(或其他什么?),这样当我“编织”生成的 MS Word 文档时,就会有一个呈现的评论。
小智 6
我想扩展@JBGruber 的好答案:
此代码适用于 html 和 word 作为输出:
```{css, echo=FALSE}
span.comment-start{
background: #e0f3db;
}
span.comment-end{
background: #e0f3db;
}
span.comment-start::after{
content: " (" attr(author) ", " attr(date) ") [";
}
span.comment-end::before{
content: "]"
}
span.comment-text{
background: #fdbb84;
}
```
```{r, echo=FALSE}
commentN <- 0
cmt <- function(txt, cm, author, date = "2021-01-01", comN = commentN){
cmt_str <- paste0('<span class="comment-text">[',cm,']{.comment-start id="', comN, '" author="', author, '" date="', date, '"}', txt, '[]{.comment-end id="',commentN,'"}</span>')
assign("commentN", commentN + 1, envir = .GlobalEnv)
return(cmt_str)
}
```
Run Code Online (Sandbox Code Playgroud)
您可以通过致电在文本中添加评论
`r cmt("Text to be commented", "The comment itself", "myName", "Date")`
Run Code Online (Sandbox Code Playgroud)
实际上,这是可能的。是的,Markdown(和 RMarkdown)用于纯文本书写,但它们是使用 pandoc 进行翻译的。所以我用谷歌搜索了这个并找到了以下代码,效果很好:
---
title: "test"
output:
word_document: default
---
This text contains a [This is the comment]{.comment-start id="0" author="Johannes G." date="2020-01-13T10:12:00Z"}comment.[]{.comment-end id="0"}.
Run Code Online (Sandbox Code Playgroud)
当编织到其他格式时,这会造成一些混乱,因此您可能需要考虑使用R函数:
```{r echo=FALSE}
word_comment <- function(comment, highlight = "") {
if (isTRUE(knitr:::pandoc_to() == "docx")) {
paste0('[', comment, ']{.comment-start id="0" author="Johannes G."',
'date="2020-01-13T10:12:00Z"}', highlight, '[]{.comment-end id="0"}')
}
}
```
This text contains a `r word_comment("This is the comment", "comment.")`.
Run Code Online (Sandbox Code Playgroud)
代码可能会得到改进,但我找不到创建注释的块的文档,所以目前它工作得很好。