Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

table with long text, bullet points and specific table width

I want a table to have bullet points in one column and to have a specific table width (in order to be placed on one page when rendered to PDF).

How can I achieve this in rmarkdown using one of the many packages out there?


What I have tried and have so far:

---
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')
```

This renders to:

enter image description here

As you can see both options have caveats. The kableExtra version does have a specific table width that fits on one page but does not show bullet points nicely. Whereas the pander solution renders the bullet points nicely but spans multiple pages, because I don't know how to specifiy the table width in pander.

Is there a solution that can do both?

Related questions are for example here and there.

like image 271
symbolrush Avatar asked Oct 24 '18 06:10

symbolrush


People also ask

Can you use bullet points in a table?

Create some bullets in a table, then from the Styles task pane, right-click List Paragraph and click Modify. Click Format at bottom left, then click Paragraph. Select your required Indentation from the Left side of the cell and your required Hanging indent size then click OK.

How do I create a bulleted table in Word?

Click on the word document so that your cursor is inside of the first column and then click "Home" at the top of the screen. Choose "Paragraph" from the resulting options and then choose "Bullets and Numbering" to add your bullet points.

How do you write point wise in LaTex?

You can create a numbered list with LaTex bullet points with the same code we used before, except with \begin{enumerate} and \end{enumerate} around the list items instead of \begin{itemize} and \end{itemize}.


2 Answers

An alternative solution based on kableExtra (optional: with footnotes)

This approach allows to add a caption, to manually add footnotes within the table, and to fix column widths.

To create the bullet point list:

  • "thicker" \cdots from LaTex are employed as bullets (alternatives see here)
  • linebreaks are forced with \n (indentation is therefore not as nice as with the pander approach from @daroczig).

MWE

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

To [improve line spacing[(https://stackoverflow.com/questions/53794142/increase-line-row-spacing-with-kableextra), one can add the following before & after code chunk in the Rmd:

\renewcommand{\arraystretch}{1.5} <!-- increase line spacing for the table -->
RMD CHUNK HERE
\renewcommand{\arraystretch}{1} <!-- reset row hight/line spacing -->

kableExtra table

Comment:

I also tried the pander approach from @daroczig and made the following experiences:

  • Pro: nice line spacing plus a "real bullet list" (compared to the $\boldsymbol{\cdot}$-workaround in the kableExtra approach).
  • Con: a large blank space is included after the bullet list

In addition, when using the pander approach in an Rmd file using the huskydown thesis template the footnotes greatly messed up the table alignment.

like image 108
mavericks Avatar answered Sep 18 '22 03:09

mavericks


Use the split.table parameter of pandoc.table (that is being called by pander in the background) or disable the table splitting in general via panderOptions's table.split.table, eg

pander::pander(df, keep.line.breaks = TRUE, style = 'grid', justify = 'left', split.table = Inf)

or

library(pander)
panderOptions('table.style', 'grid')
panderOptions('table.alignment.default', 'left')
panderOptions('table.split.table', Inf)
panderOptions('keep.line.breaks', TRUE)
pander(df)

pander example

like image 33
daroczig Avatar answered Sep 22 '22 03:09

daroczig