How to stop bookdown tables from floating to bottom of the page in pdf?
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.
How to prevent a kable from splitting between pages?
If you're only exporting to PDF, then try this:
knitr::kable(
my_data,
format = "latex",
longtable = FALSE
)
A longtable table allows page breaks between rows. Looking at the code for knitr:::kable_latex
, which kable
calls, the default should be longtable = FALSE
. But explicitly setting this argument makes sure you're not making longtables.
How do I make knitr::kable tables stop floating?
Use the argument longtable = T
in knitr::kable()
and add \ usepackage{longtable}
in the YAML header:
---
title: "Using longtable in RMD"
output: pdf_document
date: "`r format(Sys.time(), '%d %B %Y')`"
author: Author
header-includes:
- \usepackage{longtable}
---
```{r}
knitr::kable(cars, longtable = T)
```
Rmarkdown setting the position of kable
You can replace "hold_position" from claudius answer with "HOLD_position":
```{r}
kable(cars %>% filter(cars$speed>=23), caption = "Speed vs distance") %>%
kable_styling(latex_options = "HOLD_position")
```
As mentionned in the kableExtra package:
if you find hold_position is not powerful enough to literally PIN your table in the exact position, you may want to use HOLD_position, which is a more powerful version of this feature. For those who are familiar with TeX, hold_position uses[!h] and HOLD_position uses [H] and the float package.
ref: https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf
You may also want to control figure position by adding fig.pos='H' to the figure chunk header.
How to break a knitr table in multiples column in PDF output? - Rmarkdown
It would have been really better if you had given a sample of your table.
---
title: "Wrapping Table"
output:
pdf_document:
keep_tex: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
mtcars_long <- rbind(mtcars, mtcars, mtcars, mtcars)
```
## R Markdown
\newpage
```{r results='asis', echo=FALSE}
cat('\\begin{center}')
cat('\\begin{tabular}{ c c }')
cat('\\fontsize{7}{10}\\selectfont')
print(knitr::kable(mtcars_long[1:60, 1:2], format = 'latex'))
cat('&')
cat('\\fontsize{7}{10}\\selectfont')
print(knitr::kable(mtcars_long[61:120, 1:2], format = 'latex'))
cat('\\end{tabular}')
cat('\\end{center}')
```
rendered Output
How to hold figure position with figure caption in pdf output of knitr?
As Yihui mentioned in his answer (Figure position in markdown when converting to PDF with knitr and pandoc), we cannot expect too much about formatting from mardown. To workaround this problem, just write some R scripts to replace htbp
to H
.
Compared with knit
from knitr package, I found render
from rmarkdown is better to export a tex
file. Just remember to add keep_tex: yes
in the yaml header of your rmarkdown file.
library(rmarkdown)
render('filepath.Rmd')
x <- readLines('filepath.tex')
pos <- grep('begin\\{figure\\}\\[htbp\\]', x)
x[pos] <- gsub('htbp', 'H', x[pos])
writeLines(x, 'filepath.tex')
tools::texi2pdf('filepath.tex', clean = TRUE) # gives foo.pdf
file.remove('filepath.tex')
Figure position in markdown when converting to PDF with knitr and pandoc
I'm not aware of such an option for pandoc
to set the floating option of figures when converting a Markdown document to LaTeX. If you choose Markdown for its simplicity, you should not expect too much power from it, even with powerful tools like pandoc
. Bottom line: Markdown is not LaTeX. It was designed for HTML instead of LaTeX.
Two ways to go:
use the Rnw syntax (R + LaTeX) instead of Rmd (R Markdown) (examples); then you will be able to use the chunk option
fig.pos='H'
after you\usepackage{float}
in the preamble; in this case, you have full power of LaTeX, and pandoc will not be involvedhack at the LaTeX document generated by pandoc, e.g. something like
library(knitr)
knit('foo.Rmd') # gives foo.md
pandoc('foo.md', format='latex') # gives foo.tex
x = readLines('foo.tex')
# insert the float package
x = sub('(\\\\begin\\{document\\})', '\\\\usepackage{float}\n\\1', x)
# add the H option for all figures
x = gsub('(\\\\begin\\{figure\\})', '\\1[H]', x)
# write the processed tex file back
writeLines(x, 'foo.tex')
# compile to pdf
tools::texi2pdf('foo.tex') # gives foo.pdf
If you do not like these solutions, consider requesting a new feature to pandoc on Github, then sit back and wait.
Related Topics
R - What Algorithm Does Geom_Density() Use and How to Extract Points/Equation of Curves
Label X Axis in Time Series Plot Using R
Special Characters and Superscripts on Plot Axis Titles
Merging More Than 2 Dataframes in R by Rownames
Reproduce Table and Plot from Journal
Dplyr - Groupby on Multiple Columns Using Variable Names
Setting Work Directory in Knitr Using Opts_Chunk$Set(Root.Dir = ...) Doesn't Work
Are There Global Variables in R Shiny
Ggplot2 for Grayscale Printouts
Create New Column Based on 4 Values in Another Column
R How to Calculate Difference Between Rows in a Data Frame
The Condition Has Length ≫ 1 and Only the First Element Will Be Used
Ggplot X-Axis Labels with All X-Axis Values
How to Modify an Existing a Sheet in an Excel Workbook Using Openxlsx Package in R