如何使用 rmarkdown 和 pandoc 在 table 中编写多级(项目符号)列表
How to write multi-level (bullet) lists in a table using rmarkdown and pandoc
我希望使用 rmarkdown
、knitr
和 pander
在 PDF 文档中创建 table。我能够创建 但现在需要该列表中的另一个级别。在下面显示的 table 中,我要求“Rave reviews”项目符号是“Offers workshops...”项目符号的子项目符号。
我遇到的问题与 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] 我从 here, after attempting \s
, \s
, \ \s
, \\s
, etc. to no avail (suggested here 得到了 \x20
提示。
您可以将项目嵌套在项目符号中:
```{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"))
```
编辑:我没有意识到你使用的是 pdf,但这里有一个 html 解决方案
- 我会使用矩阵
- 我会使用 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 参数之一来修复。垂直对齐将文本推到顶部而不是居中,max-width 设置文本换行,自动宽度让每个文本都有自己的宽度
htmlTable(mytable, align = 'lll', align.header = 'lll',
css.cell = "width: auto; max-width: 250px; vertical-align: top;")
我希望使用 rmarkdown
、knitr
和 pander
在 PDF 文档中创建 table。我能够创建
我遇到的问题与 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] 我从 here, after attempting \s
, \s
, \ \s
, \\s
, etc. to no avail (suggested here 得到了 \x20
提示。
您可以将项目嵌套在项目符号中:
```{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"))
```
编辑:我没有意识到你使用的是 pdf,但这里有一个 html 解决方案
- 我会使用矩阵
- 我会使用 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 参数之一来修复。垂直对齐将文本推到顶部而不是居中,max-width 设置文本换行,自动宽度让每个文本都有自己的宽度
htmlTable(mytable, align = 'lll', align.header = 'lll',
css.cell = "width: auto; max-width: 250px; vertical-align: top;")