Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/davidgohel/flextable/llms.txt

Use this file to discover all available pages before exploring further.

When knitting to PDF, knit_print.flextable() automatically generates LaTeX code. No setup is required beyond a working LaTeX installation.

LaTeX engine requirements

The default pdflatex engine does not support custom font families. If your tables use system fonts (set via set_flextable_defaults(font.family = ...)), you must switch to xelatex or lualatex. Set the engine in the YAML header of your R Markdown or Quarto document:
---
title: "My document"
output:
  pdf_document:
    latex_engine: xelatex
---
Alternatively, suppress the font warning and keep using pdflatex by setting:
set_flextable_defaults(fonts_ignore = TRUE)

Required LaTeX packages

flextable automatically loads the following LaTeX packages via knitr metadata:
  • fontspec (xelatex/lualatex only)
  • multirow, multicol
  • colortbl
  • ulem
  • hhline
  • longtable
  • array
  • hyperref

Adding dependencies manually with add_latex_dep()

When chunk caching is enabled, knitr uses the cached output and skips the chunk that would normally register LaTeX dependencies. Call add_latex_dep() in a non-cached chunk to ensure the dependencies are included:
# In a non-cached setup chunk:
add_latex_dep()
If your tables use float or wrap containers, pass the corresponding arguments:
add_latex_dep(float = TRUE, wrapfig = TRUE)

Chunk options for PDF

Control LaTeX rendering per-chunk or globally:
# Set globally in a setup chunk
knitr::opts_chunk$set(
  ft.tabcolsep = 4,
  ft.arraystretch = 1.8,
  ft.latex.float = "none"
)
Chunk optionDescriptionDefault
ft.tabcolsepSpace (pt) between text and left/right cell border0
ft.arraystretchRow height multiplier1.5
ft.latex.floatFloat container type"none"
These can also be set directly on the table object with set_table_properties():
ft <- set_table_properties(
  ft,
  opts_pdf = list(
    tabcolsep = 4,
    arraystretch = 1.8
  )
)

Float behavior

The ft.latex.float chunk option (or set_table_properties(float = ...)) controls how the LaTeX table is wrapped:
ValueContainerDescription
"none"No containerTable appears inline at its location
"float"float packageTable floats to optimal position
"wrap-r"wrapfig packageText wraps around right side
"wrap-l"wrapfig packageText wraps around left side
"wrap-i"wrapfig packageInner margin (for two-column)
"wrap-o"wrapfig packageOuter margin (for two-column)
Example using a float container:
```{r ft.latex.float="float"}
ft <- flextable(head(iris))
ft <- set_caption(ft, caption = "Iris data sample")
ft
```

PDF limitations

The following formatting properties have no effect in PDF output:
  • padding.top and padding.bottom
  • line_spacing
  • Row height
  • Justified text alignment (converted to left-aligned)
  • Images inside table cells
  • Equations (as_equation()) and hyperlinks
Images inside flextable cells are not rendered in PDF/LaTeX output.

Font families

With xelatex or lualatex, flextable loads the fontspec package and applies font families defined on text runs. The font must be installed on your system. To use the Liberation Sans font (included with the gdtools package):
library(gdtools)
register_liberationsans()
set_flextable_defaults(font.family = "Liberation Sans")
To avoid font-related errors with pdflatex, fall back to LaTeX default fonts:
set_flextable_defaults(fonts_ignore = TRUE)

Build docs developers (and LLMs) love