Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

r - How to stop bookdown tables from floating to bottom of the page in pdf?

I am using bookdown to create pdf reports, but my tables are all floating down to the bottom of the page, regardless of how much space there is. See this example:

---
title: "test_doc"
author: "Jake Thompson"
date: "6/30/2017"
output:
  bookdown::pdf_document2:
    toc: false
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, collapse = TRUE)
library(tidyverse)
```

# Test heading

Let make a data frame and print it in Table @ref(tab:test-table)

```{r test-table}
data_frame(col_a = seq_len(5), col_b = rnorm(5), col_c = runif(5)) %>%
  knitr::kable(caption = "This is a test")
```

The resulting pdf looks like this:

pdf-output

Why does the table go to the bottom of the page? And is there a way to prevent this behavior?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can solve this problem with kableExtra by

data_frame(col_a = seq_len(5), col_b = rnorm(5), col_c = runif(5)) %>%
  knitr::kable(caption = "This is a test") %>%
  kableExtra::kable_styling(latex_options = "hold_position")

It basically insert a [!h] to the LaTeX table environment which will prevent the floating behavior and pin the table at current location.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...