| table2tex | R Documentation |
Converts a data frame, data.table, or matrix to LaTeX source code suitable for
inclusion in LaTeX documents. Generates publication-quality table markup with
extensive formatting options including booktabs styling, color schemes, and
hierarchical displays. Output can be directly \input{} or \include{}
into LaTeX manuscripts. Requires xtable for export.
table2tex(
table,
file,
format_headers = TRUE,
variable_padding = FALSE,
cell_padding = "normal",
bold_significant = TRUE,
bold_variables = FALSE,
p_threshold = 0.05,
align = NULL,
indent_groups = FALSE,
condense_table = FALSE,
condense_quantitative = FALSE,
booktabs = FALSE,
zebra_stripes = FALSE,
stripe_color = "gray!20",
dark_header = FALSE,
caption = NULL,
caption_size = NULL,
label = NULL,
show_logs = FALSE,
...
)
table |
Data frame, data.table, or matrix to export. Can be output from
|
file |
Character string specifying the output |
format_headers |
Logical. If |
variable_padding |
Logical. If |
cell_padding |
Character string or numeric. Vertical padding within cells:
|
bold_significant |
Logical. If |
bold_variables |
Logical. If |
p_threshold |
Numeric. Threshold for bold p-value formatting. Only
used when |
align |
Character string or vector specifying column alignment:
If |
indent_groups |
Logical. If |
condense_table |
Logical. If |
condense_quantitative |
Logical. If |
booktabs |
Logical. If |
zebra_stripes |
Logical. If |
stripe_color |
Character string. LaTeX color specification for zebra
stripes (e.g., |
dark_header |
Logical. If |
caption |
Character string. Table caption for LaTeX caption command.
Supports multi-line captions using double backslash. Default is |
caption_size |
Numeric. Caption font size in points. If |
label |
Character string. LaTeX label for cross-references.
Example: |
show_logs |
Logical. If |
... |
Additional arguments passed to |
Output Format:
The function generates a standalone LaTeX tabular environment that can be:
Included in documents with \input command
Embedded in table/figure environments
Used in manuscript classes (article, report, etc.)
The output includes:
Complete tabular environment with proper alignment
Horizontal rules (\hline or booktabs rules)
Column headers with optional formatting
Data rows with automatic escaping of special characters
Optional caption and label commands
Required LaTeX Packages:
Add these to your LaTeX document preamble:
Always required:
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{array}
\usepackage{graphicx}
Optional (based on parameters):
\usepackage{booktabs}
\usepackage[table]{xcolor}
Booktabs Style:
When booktabs = TRUE, the table uses publication-quality rules:
\toprule - Heavy rule at top
\midrule - Medium rule below headers
\bottomrule - Heavy rule at bottom
No vertical rules (booktabs style)
Better spacing around rules
This is the preferred style for most academic journals.
Color Features:
Zebra Stripes: Creates alternating background colors for visual grouping:
zebra_stripes = TRUE stripe_color = "gray!20" # 20% gray stripe_color = "blue!10" # 10% blue
Dark Header: Creates high-contrast header row:
dark_header = TRUE # Black background, white text
Both require the xcolor package with table option in your document.
Integration with LaTeX Documents:
Basic inclusion:
\begin{table}[htbp]
\centering
\caption{Regression Results}
\label{tab:regression}
\input{results.tex}
\end{table}
With resizing:
\begin{table}[htbp]
\centering
\caption{Results}
\resizebox{\textwidth}{!}{\input{results.tex}}
\end{table}
Landscape orientation:
\usepackage{pdflscape}
\begin{landscape}
\begin{table}[htbp]
\centering
\input{wide_results.tex}
\end{table}
\end{landscape}
Caption Formatting:
Captions in the caption parameter are written as LaTeX comments in
the output file for reference. For actual LaTeX captions, wrap the table
in a table environment (see examples above).
Special Characters:
The function automatically escapes LaTeX special characters in your data:
Ampersand, percent, dollar sign, hash, underscore
Left and right braces
Tilde and caret (using textasciitilde and textasciicircum)
Variable names and labels should not include these characters unless intentionally using LaTeX commands.
Invisibly returns NULL. Creates a .tex file at the specified
location containing a LaTeX tabular environment.
autotable for automatic format detection,
table2pdf for direct PDF output,
table2html for HTML tables,
table2docx for Word documents,
table2pptx for PowerPoint,
table2rtf for Rich Text Format,
fit for regression tables,
desctable for descriptive tables
Other export functions:
autotable(),
table2docx(),
table2html(),
table2pdf(),
table2pptx(),
table2rtf()
data(clintrial)
data(clintrial_labels)
# Create example table
results <- fit(
data = clintrial,
outcome = "os_status",
predictors = c("age", "sex", "treatment", "stage"),
labels = clintrial_labels
)
# Example 1: Basic LaTeX export
if (requireNamespace("xtable", quietly = TRUE)) {
table2tex(results, file.path(tempdir(), "basic.tex"))
}
# Example 2: With booktabs for publication
table2tex(results, file.path(tempdir(), "publication.tex"),
booktabs = TRUE,
caption = "Multivariable logistic regression results",
label = "tab:regression")
# Example 3: Multi-line caption with abbreviations
table2tex(results, file.path(tempdir(), "detailed.tex"),
booktabs = TRUE,
caption = "Table 1: Risk Factors for Mortality\\\\
aOR = adjusted odds ratio; CI = confidence interval\\\\
Model adjusted for age, sex, treatment, and disease stage",
label = "tab:mortality")
# Example 4: Hierarchical display with indentation
table2tex(results, file.path(tempdir(), "indented.tex"),
indent_groups = TRUE,
booktabs = TRUE)
# Example 5: Condensed table (reduced height)
table2tex(results, file.path(tempdir(), "condensed.tex"),
condense_table = TRUE,
booktabs = TRUE)
# Example 6: With zebra stripes
table2tex(results, file.path(tempdir(), "striped.tex"),
zebra_stripes = TRUE,
stripe_color = "gray!15",
booktabs = TRUE)
# Remember to add \usepackage[table]{xcolor} to the LaTeX document
# Example 7: Dark header style
table2tex(results, file.path(tempdir(), "dark_header.tex"),
dark_header = TRUE,
booktabs = TRUE)
# Requires \usepackage[table]{xcolor}
# Example 8: Custom cell padding
table2tex(results, file.path(tempdir(), "relaxed.tex"),
cell_padding = "relaxed",
booktabs = TRUE)
# Example 9: Custom column alignment (auto-detected by default)
table2tex(results, file.path(tempdir(), "custom_align.tex"),
align = c("c", "c", "c", "c", "c", "c", "c"))
# Example 10: No header formatting (keep original names)
table2tex(results, file.path(tempdir(), "raw_headers.tex"),
format_headers = FALSE)
# Example 11: Disable significance bolding
table2tex(results, file.path(tempdir(), "no_bold.tex"),
bold_significant = FALSE,
booktabs = TRUE)
# Example 12: Stricter significance threshold
table2tex(results, file.path(tempdir(), "strict_sig.tex"),
bold_significant = TRUE,
p_threshold = 0.01, # Bold only if p < 0.01
booktabs = TRUE)
# Example 13: With caption size control
table2tex(results, file.path(tempdir(), "caption_size.tex"),
caption_size = 6,
caption = "Table 1 - Results with Compact Caption\\\\
Smaller caption fits better on constrained pages")
# Example 14: Complete publication-ready table
table2tex(results, file.path(tempdir(), "final_table1.tex"),
booktabs = TRUE,
caption = "Table 1: Multivariable Analysis of Mortality Risk Factors",
label = "tab:main_results",
indent_groups = TRUE,
zebra_stripes = FALSE, # Many journals prefer no stripes
bold_significant = TRUE,
cell_padding = "normal")
# Example 15: Descriptive statistics table
desc_table <- desctable(clintrial, by = "treatment",
variables = c("age", "sex", "bmi"), labels = clintrial_labels)
table2tex(desc_table, file.path(tempdir(), "table1_descriptive.tex"),
booktabs = TRUE,
caption = "Table 1: Baseline Characteristics",
label = "tab:baseline")
# Example 16: Model comparison table
models <- list(
base = c("age", "sex"),
full = c("age", "sex", "treatment", "stage")
)
comparison <- compfit(
data = clintrial,
outcome = "os_status",
model_list = models
)
table2tex(comparison, file.path(tempdir(), "model_comparison.tex"),
booktabs = TRUE,
caption = "Model Comparison Statistics",
label = "tab:models")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.