【问题标题】:How to write multi-level (bullet) lists in a table using rmarkdown and pandoc如何使用 rmarkdown 和 pandoc 在表格中编写多级(项目符号)列表
【发布时间】:2015-06-25 11:56:07
【问题描述】:

我希望使用rmarkdownknitrpander 在PDF 文档中创建一个表格。我可以创建一个simple bullet list,但现在需要该列表中的另一个级别。在下表中,我要求“好评如潮”项目符号是“优惠研讨会...”项目符号的子项目符号。

我遇到的问题与Description 列中mytable 数据框中的以下代码行有关。尽管我尝试使用\x20[1] 创建子项目符号所需的 4 个空格,但这些空格不会出现 - 因此没有子项目符号(尽管没有显示错误)。我还尝试了最明显的方法,即在代码中简单地添加 4 个空格,但无济于事。还尝试将我的R 选项设置为包括strip.white = FALSE,但这也没有被证明有帮助(见下文)。

---
title: "xxx"
author: "xxx"
output:
  pdf_document:
    fig_height: 4
    fig_width: 10
    highlight: tango
  word_document: default
geometry: margin=3cm
---

```{r global_options, include=FALSE, echo=FALSE}
require(knitr)
opts_chunk$set(fig.width=8, fig.height=4, fig.path='figs/', dpi=500,
               echo=FALSE, warning=FALSE, message=FALSE, results='hide', strip.white = FALSE)
```

```{r pandoc_options, include=FALSE, echo=FALSE}
require(pander)
panderOptions('digits', 3)
panderOptions('round', 3)
panderOptions('keep.trailing.zeros', TRUE)
panderOptions('keep.line.breaks', TRUE)
```

```{r concepts, echo=FALSE}
mytable = data.frame(
    Concept     = c("Decoded", "XXX"),
    Description = c("* Founded in 2011\ \n* Offers workshops to take people from zero skills and knowledge in programming through to coding a multi-platform app using HTML, CSS and Javascript in a single day\ \n\x20\x20\x20\x20+ Rave reviews", "XXX"),
    Website     = c("http://decoded.com/uk/","XXX"))
```

``` {r concepts_descriptions, results = 'asis'}
pander::pander(mytable, keep.line.breaks = TRUE, style = 'grid', justify = 'left')
```

参考文献

[1] 在尝试\\s\s\ \s\\\s 等无济于事后,我从here 得到了\x20 提示(建议here)。

【问题讨论】:

  • @bergant 谢谢 - 不幸的是,如果我尝试输入\hspace{10pt},pdf 创建将停止(“执行已停止”)。还尝试玩``无济于事。您能否详细说明括号中的含义?
  • 对不起,我检查了它在这种情况下不起作用 - 这就是我删除评论的原因。关于错误:要在常量字符串中使用 \hspace,请使用 \\hspace(双反斜杠)
  • 关于pander 中的空白删除:必须使justify (align) 参数起作用。不幸的是,没有简单的解决方法。

标签: r r-markdown pandoc pander


【解决方案1】:

您可以将项目嵌套在项目符号内:

```{r concepts, echo=FALSE}
mytable = data.frame(
    Concept     = c("Decoded", "XXX"),
    Description = c("* Founded in 2011\ \n
    * Offers workshops to take people from zero skills and knowledge in programming through to coding a multi-platform app using HTML, CSS and Javascript in a single day\ \n
    \\begin{itemize}
      \\item Rave reviews \n
      \\item e.t.c
    \\end{itemize}", "XXX"),
    Website     = c("http://decoded.com/uk/","XXX"))
```

【讨论】:

    【解决方案2】:

    编辑:我没有意识到您使用的是 pdf,但这是一个 html 解决方案

    1. 我会使用矩阵
    2. 我会使用 htmlTable 包,它允许比 kable 或 pander 更高级的表格

    首先为您的列表制作一个包装器

    make_list <- function(...) {
      paste0("<ul>", sprintf('<li>%s</li>', substitute(...())), '</ul>', collapse = '')
    }
    
    make_list(one, two, three)
    
    # [1] "<ul><li>one</li></ul><ul><li>two</li></ul><ul><li>three</li></ul>"
    
    
    library('htmlTable')
    mytable <- matrix(c("Decoded", "XXX",
                        make_list('Founded in 2011', 'Offers workshops to take people from zero skills and knowledge in programming through to coding a multi-platform app using HTML, CSS and Javascript in a single day','Rave reviews'),
                        "XXX",
                        "http://decoded.com/uk/","XXX"), 2,
                      dimnames = list(NULL, c('Concept','Description','Website')))
    
    htmlTable(mytable, align = 'lll')
    

    这很好,但垂直对齐不好并且框太宽。但是我们可以使用htmlTable 的css 参数之一进行修复。垂直对齐将文本推到顶部而不是中心,最大宽度设置文本换行,自动宽度让每个都有自己的宽度

    htmlTable(mytable, align = 'lll', align.header = 'lll',
              css.cell = "width: auto; max-width: 250px; vertical-align: top;")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-22
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      • 2011-12-02
      • 2011-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多