8

I try to create a table using the format "markdown" of the function kable(), but the spaces between the columns are so wide that the table extends over the page. Is there anyway to adjust the cell size so that a table in markdown format does not extend over the page? In latex format it stays within the page, but I do not want this format nor do I want html. I want the output file to be .pdf. I know that a similar question has been asked here, but my question is specific to the format markdown. If you feel this is a duplicate, please merge the questions.

Reproducible example:

---
title: "Example"
author: "JAQuent"
date: "7 Juni 2017"
output: pdf_document
---

\tiny

```{r results='asis', echo = FALSE, warning = FALSE}
library(knitr)

table1 <- data.frame(Factor1 = c('level 1', 'level 1', 'level 2', 'level 2'),
                     Factor2 = c('level 1', 'level 2', 'level 1', 'level 2'),
                     Parameter1 = sample(1000000:9999999, 2),
                     Parameter2 = sample(1000000:9999999, 2),
                     Parameter3 = sample(1000000:9999999, 2),
                     Parameter4 = sample(1000000:9999999, 2),
                     Parameter5 = sample(1000000:9999999, 2),
                     Parameter6 = sample(1000000:9999999, 2),
                     Parameter7 = sample(1000000:9999999, 2))

names(table1) <- c('Factor1', 'Factor2', 'Parameter1', 'Parameter2', 'Parameter3', 'Parameter4', 'Parameter5', 'Parameter6', 'Parameter7')


kable(table1, format = 'markdown')
kable(table1, format = 'latex')
```

enter image description here

lmo
  • 37,904
  • 9
  • 56
  • 69
JAQuent
  • 1,137
  • 11
  • 25
  • 1
    Just using `kable(table1)` actually gives a fitting table for me (removing the format argument) – timfaber Jun 07 '17 at 12:06
  • Your solution combined with using \tiny works perfectly. – JAQuent Jun 07 '17 at 12:13
  • [For Hao's answer, see for specifics on using kableExtra package.](https://stackoverflow.com/questions/56254631/table-way-too-wide-to-fit-in-markdown-generated-pdf) – phargart May 22 '19 at 22:31

2 Answers2

6

A couple of the examples in ?kable contain a padding argument. Feeding it 0L gets you closer, but it turns out that this argument will take negative integers, so

kable(table1, format = 'markdown', padding=-1L)

will produce something closer to what your are looking for.

lmo
  • 37,904
  • 9
  • 56
  • 69
  • 1
    Although the OP did ask for the markdown format, it's worth noting that the padding argument is not useable with the latex format – MBorg Dec 09 '22 at 06:29
1

I think what you are looking for is actually kable(table, "latex", booktabs = T). Also, you can check out the kableExtra package which makes it easy to modify the kable outputs.

Hao
  • 7,476
  • 1
  • 38
  • 59