Renderers
HTML
In IDEs that support the MIME"text/html" or MIME"juliavscode/html" types, just displaying a Table will render it in HTML for you. All examples in this documentation are rendered this way. Alternatively, you can print HTML to any IO object via show(io, MIME"text/html"(), table).
LaTeX
You can print LaTeX code to any IO via show(io, MIME"text/latex"(), table). Keep in mind that the threeparttable, multirow and booktabs packages need to separately be included in your preamble due to the way LaTeX documents are structured.
using SummaryTables
using DataFrames
using tectonic_jll
mkpath(joinpath(@__DIR__, "..", "public"))
data = DataFrame(
sex = ["m", "m", "m", "m", "f", "f", "f", "f", "f", "f"],
age = [27, 45, 34, 85, 55, 44, 24, 29, 37, 76],
blood_type = ["A", "0", "B", "B", "B", "A", "0", "A", "A", "B"],
smoker = [true, false, false, false, true, true, true, false, false, false],
)
tbl = table_one(
data,
[:age => "Age (years)", :blood_type => "Blood type", :smoker => "Smoker"],
groupby = :sex => "Sex",
show_n = true
)
# render latex in a temp directory
mktempdir() do dir
texfile = joinpath(dir, "main.tex")
open(texfile, "w") do io
# add the necessary packages in the preamble
println(io, raw"""
\documentclass{article}
\usepackage{threeparttable}
\usepackage{multirow}
\usepackage{booktabs}
\begin{document}
""")
# print the table as latex code
show(io, MIME"text/latex"(), tbl)
println(io, raw"\end{document}")
end
# render the tex file to pdf
tectonic_jll.tectonic() do bin
run(`$bin --chatter=minimal $texfile`)
end
cp(joinpath(dir, "main.pdf"), joinpath(@__DIR__, "..", "public", "example.pdf"))
enddocx
To get docx output, you need to use the WriteDocx.jl package because this format is not plain-text like LaTeX or HTML. The table node you get out of the to_docx function can be placed into sections on the same level as paragraphs.
using SummaryTables
using DataFrames
import WriteDocx as W
mkpath(joinpath(@__DIR__, "..", "public"))
data = DataFrame(
sex = ["m", "m", "m", "m", "f", "f", "f", "f", "f", "f"],
age = [27, 45, 34, 85, 55, 44, 24, 29, 37, 76],
blood_type = ["A", "0", "B", "B", "B", "A", "0", "A", "A", "B"],
smoker = [true, false, false, false, true, true, true, false, false, false],
)
tbl = table_one(
data,
[:age => "Age (years)", :blood_type => "Blood type", :smoker => "Smoker"],
groupby = :sex => "Sex",
show_n = true
)
doc = W.Document(
W.Body([
W.Section([
SummaryTables.to_docx(tbl)
])
])
)
W.save(joinpath(@__DIR__, "..", "public", "example.docx"), doc)Because Word cannot express font-relative lengths, any em lengths in the table (rule widths, paddings, footnote_size, cell indent, row and column gaps) are converted to absolute points when exporting. The reference font size for this conversion is the surrounding document's font size, which defaults to 10pt and can be set with the optional DocxDefaults argument of to_docx:
using SummaryTables: pt
SummaryTables.to_docx(tbl, SummaryTables.DocxDefaults(base_fontsize = 12pt))It can also be set globally with SummaryTables.defaults!(docx = SummaryTables.DocxDefaults(base_fontsize = 12pt)).
Typst
You can print Typst table code to any IO via show(io, MIME"text/typst"(), table). From SummaryTables v2.0 on, the Typst backend is using the native table functionality in Typst v0.11. Previous versions used the tablex package.
using SummaryTables
using DataFrames
using Typst_jll
mkpath(joinpath(@__DIR__, "..", "public"))
data = DataFrame(
sex = ["m", "m", "m", "m", "f", "f", "f", "f", "f", "f"],
age = [27, 45, 34, 85, 55, 44, 24, 29, 37, 76],
blood_type = ["A", "0", "B", "B", "B", "A", "0", "A", "A", "B"],
smoker = [true, false, false, false, true, true, true, false, false, false],
)
tbl = table_one(
data,
[:age => "Age (years)", :blood_type => "Blood type", :smoker => "Smoker"],
groupby = :sex => "Sex",
show_n = true
)
# render latex in a temp directory
mktempdir() do dir
typfile = joinpath(dir, "example.typ")
open(typfile, "w") do io
# print the table as latex code
show(io, MIME"text/typst"(), tbl)
end
# render the tex file to pdf
run(`$(typst()) compile $typfile`)
cp(joinpath(dir, "example.pdf"), joinpath(@__DIR__, "..", "public", "example_typst.pdf"))
endPlain text
You can print a plain text rendering of a table to any IO via show(io, MIME"text/plain"(), table), which is also what the REPL uses to display a Table. This backend is meant for quick inspection in a terminal or in logs, so it only approximates the visual features of the other backends: cell borders and rules are drawn with box-drawing characters, em and pt lengths are rounded to whole characters, and bold, italic, underline and color formatting is dropped. Long cell contents are not wrapped, but footnotes are wrapped at spaces to the width of the table (or at least 40 characters). In the REPL, lines wider than the terminal are cut off and end in …, while output to a plain IO object is always printed in full.
using SummaryTables
using DataFrames
data = DataFrame(
sex = ["m", "m", "m", "m", "f", "f", "f", "f", "f", "f"],
age = [27, 45, 34, 85, 55, 44, 24, 29, 37, 76],
blood_type = ["A", "0", "B", "B", "B", "A", "0", "A", "A", "B"],
smoker = [true, false, false, false, true, true, true, false, false, false],
)
tbl = table_one(
data,
[:age => "Age (years)", :blood_type => "Blood type", :smoker => "Smoker"],
groupby = :sex => "Sex",
show_n = true
)
show(stdout, MIME"text/plain"(), tbl)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Sex
────────────────────────────
Total f m
(n=10) (n=6) (n=4)
────────────────────────────────────────────────────────────────
Age (years)
Mean (SD) 45.6 (20.7) 44.2 (19.1) 47.8 (25.9)
Median [Min, Max] 40.5 [24, 85] 40.5 [24, 76] 39.5 [27, 85]
Blood type
0 2 (20%) 1 (16.7%) 1 (25%)
A 4 (40%) 3 (50%) 1 (25%)
B 4 (40%) 2 (33.3%) 2 (50%)
Smoker
false 6 (60%) 3 (50%) 3 (75%)
true 4 (40%) 3 (50%) 1 (25%)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━