【问题标题】:Including multiple blank lines in a for loop in r markdown with pdf output在带有pdf输出的r markdown中的for循环中包含多个空行
【发布时间】:2022-01-21 15:44:02
【问题描述】:

我正在使用 r markdown 来创建一些刺激。我打算将 r markdown 输出为 pdf 文件。

因为有 24 种刺激,所以我使用 for 循环遍历刺激以创建输出。

我目前正在尝试做的是我想弄清楚是否可以在 for 循环中为 pdf 输出添加多个空行。

我当前的代码块是这样的

for (row in 1:nrow(D2)){
  a = D2%>% select(Prob) %>% slice(row) %>% pull
  print(a)
  cat(" \n")  
print("Please show your work and write your answer below")
  cat(" \n") 
print("write your answer here")
cat("\n\\newpage\n")
}

pdf 输出类似于:

问题文本

请在下方展示你的作品并写下你的答案

在这里写下你的答案

但我希望拥有的是:

问题文本

请在下方展示你的作品并写下你的答案






在这里写下你的答案


这样文本后面会有一些空格。

我尝试在代码块中包含多个 cat(" \n") 代码,但似乎这只是为了包含一个换行符,并且包含多个不会增加输出中的空行数量。

我尝试使用代码<br>cat(<br>),但它也不起作用。

我想知道是否有任何建议在 for 循环中添加空行。提前谢谢你

【问题讨论】:

    标签: r loops for-loop r-markdown pdf-generation


    【解决方案1】:

    这可能是一种方法:

    使用 Latex vspace 函数根据需要调整垂直间距,该函数采用所有常用的 Latex 单位。

    ```{r, results='asis'}
    
    D2 <- data.frame(Prob = paste("Problem", letters[1:3], "..."))
    
    for (i in 1:nrow(D2)){
      cat(D2[i, "Prob"])
      cat("  \n") 
      cat("Please show your work and write your answer below")
      cat("  \n") 
      cat("\\vspace{3cm}")
      cat("  \n") 
      cat("write your answer here")
      cat("\\newpage  ")
    }
    
    ```
    

    【讨论】:

    • 谢谢你,解决了我的问题!
    猜你喜欢
    • 2022-01-21
    • 2019-03-25
    • 2016-08-18
    • 2016-06-24
    • 2020-10-08
    • 2023-01-28
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    相关资源
    最近更新 更多