从 Rmarkdown 中允许换行和转义 Markdown 和 Latex 命令的函数打印

Printing from a function in Rmarkdown that allows newlines and escaping Markdown and Latex commands

我正在尝试编写一个在 Rmarkdown 中使用的函数,它允许换行、使用变量和嵌套函数以及用于输出 pdf 的 Markdown 和 Latex 命令。例如,该函数将用于在 Rmarkdown 文档中生成格式化的部分(我意识到我可以使用 Latex' \titleformat{\section} 在部分的特定情况下实现这一点,但我也希望能够显示,例如,JSON 在 Rmarkdown 中具有特定格式的数据)。

这是一个无法运行的函数示例,但它有望说明我想要实现的目标:

---
title: "Printing from function with newlines and escaped Markdown/Latex commands"
author: "Jono3030"
output: pdf_document
---

thirdLineFunction <- function() {
 thirdLine <- "**This is the third line in bold**" 
 return(thirdLine)
}

newLinePrinting <- function(exampleVar) {
  firstLine <- "This is the first line"
  secondLine <- sprintf("This is the %s line", exampleVar)
  thirdLine <- thirdLineFunction()
  fourthLine <- "\textit{This is the fourth line in italics}"
  cat(firstLine, "\n\n", secondLine, "\n\n", thirdLine, "\n\n", fourthLine)
}

使用 cat 并包含换行符会导致不打印任何字符串,即只有标题和作者出现在 pdf 中。然而,即使使用 cat 打印有效,Markdown 和 Latex 代码也不会被转义。使用 printsprintfpaste 而不是 cat 不会打印换行符,其中一些方法包括索引([1] 等)和引号不需要的输出。

从 Rmarkdown 正确呈现为 pdf 的 所需输出 将是:

This is the first line

This is the second line

**This is the third line in bold**

\textit{This is the fourth line in italics}

我还尝试使用 for 循环从函数打印:

test <- function() {
  o1 <- "This is text"
  o2 <- "This is \textbf{also} text"
  o3 <- thirdLineFunction()
  o4 <- "This is *text*"
  newline <- ""
  listvar <- c(o1, newline, o2, newline, o3, newline, o4)
  for (i in listvar) {
    print(noquote(i))
  }
}

虽然更接近我想要的,但它也不起作用,因为我无法摆脱索引:

[1] This is text
[1] 
[1] This is \textbf{also} text
[1] 
[1] **This is the third line in bold**
[1] 
[1] This is *text*

使用 cat 省略索引导致 Markdown 和 Latex 命令未被转义。

将上面的 for 循环与 pastecat 组合使用也不起作用:

for (i in listvar) {
    paste(i, collapse = '\n') %>% cat()
}

值得注意的是,当我在控制台中执行代码时,cat 确实有效,但输出未显示在呈现的 pdf 中。我还尝试渲染到 html,但这也不起作用。

同样,最终我希望能够使用函数打印这样的内容:

## Section Title

\vspace{1em}

\hrulefill

或:

This is the **first** line printed from a JSON file

\vspace{1em}

This is the \textbf{second} line printed from a JSON file

\vspace{1em}

然后输出应正确呈现为 pdf,符合 Markdown 和 Latex 格式。

提前感谢您的宝贵时间和帮助!

我找到了一种使用 paste():

来做我想做的事情的方法
Whosebug <- function() {
  paste(c(
    "This is the **first** line printed from a JSON file",
    "\vspace{1em}",
    "\n",
    "This is the \textbf{second} line printed from a JSON file",
    "\n",
    "\vspace{1em}"
  ), collapse = "\n")
}

使用 collapse = "\n" 将多行输出放入嵌套在 paste() 中的列表中,执行时会产生以下结果:

[1] "This is the **first** line printed from a JSON file\n\vspace{1em}\nThis is the \textbf{second} line printed from a JSON file\n\vspace{1em}"

正确呈现:

它不像 Rmarkdown 那样完美,例如,它需要在末尾有两个空格或文本之间有一个空行才能将其换行,因此我添加了额外的 "\n" 个字符。然而,现在它有效,所以我将其标记为正确答案,希望它能帮助其他人。如果有人能提出更好的解决方案,我会很乐意将他们的解决方案标记为正确。

编辑:

Markdown 所需的额外 line/two 空格在大多数情况下可以通过用两个换行符折叠来解决,如下所示:"\n\n"

这会将上面的代码更改为:

Whosebug <- function() {
  paste(c(
    "This is the **first** line printed from a JSON file",
    "\vspace{1em}",
    "This is the \textbf{second} line printed from a JSON file",
    "\vspace{1em}"
  ), collapse = "\n\n")
}