【问题标题】:Text formatting of lists in Pandoc's MarkdownPandoc 的 Markdown 中列表的文本格式
【发布时间】:2020-07-31 22:54:04
【问题描述】:

我在 sampleList 元素中添加了星号和新的换行符,以创建项目符号列表格式:


sampleList <- list(1, 2, 3)

# Create bulleted list
createPoints = function(list) {
  # Pre-allocate list
  setList <- vector(mode = "list", length = length(list))

  # Add elements to each line
  for (i in seq_along(list)) {
    line = sprintf("* %s  \n", list[[i]])
    setList[[i]] <- line
  }
  
  return(setList)
}

finalList = createPoints(sampleList)

输出:

[[1]]
[1] "* 1  \n"

[[2]]
[1] "* 2  \n"

[[3]]
[1] "* 3  \n"

如何打印项目符号子列表中的各个元素?

这不起作用:

  • 项目符号 1
    • r finalList

我的输出带有额外的逗号,子列表没有项目符号:

  • 项目符号 1

    • 1

    , * 2

    , * 3

我希望它看起来像这样:

  • 项目符号 1
    • 1
    • 2
    • 3

【问题讨论】:

  • 您可以安装pander 并使用它,即pander::pander(sampleList)

标签: r for-loop r-markdown pandoc word


【解决方案1】:

取消列出您的 finalList 对象并将所有元素折叠在一起以避免逗号

sampleList <- list(1, 2, 3)

# Create bulleted list
createPoints = function(list) {
  # Pre-allocate list
  setList <- vector(mode = "list", length = length(list))

  # Add elements to each line
  for (i in seq_along(list)) {
    line = sprintf("* %s  \n", list[[i]])
    setList[[i]] <- line
  }
  
  return(setList)
}

finalList = unlist(createPoints(sampleList))

r paste(finalList, collapse = " ")

【讨论】:

  • 任何关于我的回答@SRL的反馈
  • 这很有帮助,谢谢!为了正确缩进,我必须调整 sprintf 以在星号前面有两个空格。