I'm sorry if I'm overlooking something obvious, but I've Googled everything that I could think of and didn't find an answer that worked for me.
My problem is that I'm creating figures with the ggplot2 package (with captions created in the chunk options with "fig.cap = xy") and tables with the kable package. Now the thing is, that when knitted to PDF, the captions for the tables are automatically created on top of the table and the captions for the figures on the bottom. I would like a uniform look to my PDF, so I would like to have the captions of the tables and figures both on top.
What has been suggested was using - \usepackage{floatrow}
and - \floatsetup[figure]{capposition=top}
in the yaml, but when I try this I this error when trying to knit to PDF:
"! Package floatrow Error: Do not use float package with floatrow.
(floatrow) The latter will be skipped.
Error: Failed to compile test.tex. See test.log for more info.
Execution stopped"
The log has the following at it's end:
"! ==> Fatal error occurred, no output PDF file produced!"
Those are my "header-includes" in the yaml:
- \usepackage[below]{placeins}
- \usepackage{setspace}
- \usepackage{flafter}
- \usepackage{chngcntr}
- \counterwithout{figure}{section}
- \counterwithout{table}{section}
- \usepackage{microtype}
- \floatplacement{figure}{H}
- \floatplacement{table}{H}
- \usepackage{floatrow}
- \floatsetup[figure]{capposition=top}
And those my packages used:
library(plyr)
library(readxl)
library(xlsx)
library(markdown)
library(rmarkdown)
library(knitr)
library(kableExtra)
library(ggplot2)
library(reshape2)
library(httpuv)
formatlibrary(caTools)
Is there a workaround for this? Or is there another way to get the caption of the figure on top of the figure?
Edit1: I narrowed it down to the package "kableExtra" causing this problem, even though the "float" package doesn't appear as loaded in the sessionInfo(). After removing it and all it's code I ran into another error:
! Undefined control sequence.
<recently read> \floatplacement
After removing
- \floatplacement{figure}{H}
- \floatplacement{table}{H}
from the yaml it knitred successfully. The caption is now on top but everything else is a mess now. I can probably manage without the "kableExtra" package, but not without the "floatplacement" in the yaml.
Every suggestion would be appreciated!
I had this error too. Took a while to figure out, but if you prevent kableExtra
from loading any LaTeX
packages, then there won't be a clash between float
and floatrow
.
So specify YAML header as normal:
output:
bookdown::pdf_document2:
fig_caption: yes
header-includes:
- \usepackage{floatrow}
- \floatsetup[figure]{capposition=top}
- \floatsetup[table]{capposition=top}
- \floatplacement{figure}{H}
- \floatplacement{table}{H}
---
And make sure you set this option before loading kableExtra
options(kableExtra.latex.load_packages = FALSE)
library(kableExtra)
Depending on how you use kableExtra
, you may need to specify additional packages in the YAML header (e.g. if you highlight row colours). A full list of the packages used by kableExtra
can be found on page 3 here.