set_caption() attaches a caption to a flextable. When the output is Word, the caption is associated with a paragraph style and can use auto-numbering. When the output is HTML, CSS classes can be applied. PowerPoint ignores captions.
In Quarto documents, captions and cross-references are managed by Quarto, not flextable.
set_caption() settings are overridden by tbl-cap and label chunk options.Function signature
Parameters
A
flextable object.The caption text. Either a plain character string or a call to
as_paragraph() for formatted content (bold, italic, mixed styles, images, or equations). Plain strings do not support Markdown syntax.An auto-numbering sequence object created by
officer::run_autonum(). Affects Word output (defines the Word auto-number) and HTML/PDF output (provides the bookmark identifier). When provided, the caption is preceded by an auto-number sequence.Word paragraph style name to associate with the caption. Available style names can be retrieved with
officer::styles_info(). Alias: style (deprecated).Paragraph formatting properties from
officer::fp_par(). Applied to HTML and Word outputs when possible.If
TRUE, the caption alignment follows the flextable alignment. If FALSE, alignment is taken from fp_p directly.One or more CSS class names to apply to the caption element in HTML output.
If
TRUE, HTML entities in the caption are escaped so the caption is safe to use as text or an attribute value in an HTML document.Return value
The modifiedflextable object.
Examples
Simple string caption:as_paragraph():
R Markdown chunk options
You can set captions via knitr chunk options instead of callingset_caption() directly:
| Option | Description |
|---|---|
tab.id | Caption ID or bookmark |
tab.cap | Caption text |
tab.cap.style | Word style name for the caption |
tab.topcaption | Display caption above the table (TRUE) |
tab.lp | Caption sequence identifier (default "tab:") |
tab.cap.pre | Prefix before auto-number (default "Table ") |
tab.cap.sep | Separator after auto-number (default " :") |
See also
officer::run_autonum()— create auto-numbering sequencesofficer::fp_par()— paragraph formatting propertiesas_paragraph()— build formatted captions