Output

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__, "outputs"))

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 $texfile`)
    end

    cp(joinpath(dir, "main.pdf"), joinpath(@__DIR__, "outputs", "example.pdf"))
end
note: Running TeX ...
note: downloading SHA256SUM
note: downloading threeparttable.sty
note: downloading multirow.sty
note: downloading booktabs.sty
note: downloading lmroman10-bold.otf
note: Rerunning TeX because "main.aux" changed ...
note: Running xdvipdfmx ...
note: Writing `/tmp/jl_cBKfcC/main.pdf` (10.10 KiB)
note: Skipped writing 1 intermediate files (use --keep-intermediates to keep them)

Download example.pdf:

docx

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__, "outputs"))

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__, "outputs", "example.docx"), doc)

Download example.docx:

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__, "outputs"))

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
    Typst_jll.typst() do bin
        run(`$bin compile $typfile`)
    end

    cp(joinpath(dir, "example.pdf"), joinpath(@__DIR__, "outputs", "example_typst.pdf"))
end

Download example_typst.pdf: