我希望表格的一列中有项目符号,并具有特定的表格宽度(以便在呈现为PDF时放置在一页上)。
如何使用众多软件包之一在rmarkdown
中实现此目标?
到目前为止我尝试过的事情:
---
output: pdf_document
---
```{r, include = FALSE}
df <- data.frame(col1 = "Some really long text here. I mean some reeeeeaaly loooong text. So long, it should be wrapped. Really.",
col2 = "* bullet point 1\n * bullet point 2", col3 = "Yes, there is still another column.")
```
# Attempt 1: kableExtra
```{r, echo = FALSE, warning = FALSE}
library(kableExtra)
df1 <- df
df1$col2 <- linebreak(df1$col2)
knitr::kable(df1, escape = FALSE) %>% column_spec(1, width = "15em")
```
# Attempt 2: pander
```{r, echo = FALSE}
pander::pander(df, keep.line.breaks = TRUE, style = 'grid', justify = 'left')
```
这将呈现为:
如您所见,这两个选项都有警告。 kableExtra
版本的确具有特定的表宽度,该宽度可以容纳在一页上,但不能很好地显示项目符号。 pander
解决方案很好地呈现了项目符号要点,但是却跨越了多个页面,因为我不知道如何在pander
中指定表格宽度。
是否有一个可以同时做到的解决方案?
答案 0 :(得分:2)
这种方法允许添加标题,在表格中手动添加脚注并固定列宽。
要创建项目符号点列表,请执行以下操作:
\cdots
被用作子弹(alternatives see here)\n
强制换行(因此缩进不如@daroczig的pander
方法那样)。library(stringi); library(kableExtra); library(dplyr)
string_short <- "Descriptor"
string_long <- substr(stri_rand_lipsum(1), 1, 50)
# add footnotes manually within table
string_bulletlist <- "$\\boldsymbol{\\cdot}$ bullet point 1: foo$^{a}$ \n $\\boldsymbol{\\cdot}$ bullet point 2: bar$^{b}$"
df <- data.frame(col1 = c(string_short, string_short),
col2 = c(string_bulletlist, string_bulletlist),
col3 = c(string_long, string_long)
)
col_names <- c("Descriptor", "Description with list", "Some comment")
# header: bold column names
colnames(df) <- paste0("\\textbf{", col_names,"}")
# add footnote with kableExtra commands
names(df)[1] <- paste0(names(df)[1], footnote_marker_symbol(1))
df %>%
mutate_all(linebreak) %>% # required for linebreaks to work
kable(
"latex",
escape = F,
booktabs=TRUE,
align = "l",
caption = 'kableTable with bullet list and footnote') %>%
# kable_styling(full_width = F) %>% # makes no difference here
footnote(general = "General comment of the table.",
alphabet = c("Footnote A;", "Footnote B;"),
symbol = c("Footnote Symbol 1")) %>%
column_spec(1, width = "5em") %>% # fix width column 1
column_spec(2, width = "10em") %>% # fix width column 2
column_spec(3, width = "15em") # fix width column 3
要[提高行距[(https://stackoverflow.com/questions/53794142/increase-line-row-spacing-with-kableextra),可以在Rmd中的代码块前后添加以下内容:< / p>
\renewcommand{\arraystretch}{1.5} <!-- increase line spacing for the table -->
RMD CHUNK HERE
\renewcommand{\arraystretch}{1} <!-- reset row hight/line spacing -->
我还尝试了@daroczig的pander
方法,并获得了以下经验:
$\boldsymbol{\cdot}$
方法中的kableExtra
-解决方法相比)。此外,在使用huskydown thesis template的Rmd文件中使用pander
方法时,脚注会极大地弄乱表的对齐方式。
答案 1 :(得分:1)