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: connecting to https://relay.fullyjustified.net/default_bundle_v33.tar
note: resolved to https://data1.fullyjustified.net/tlextras-2022.0r0.tar
note: downloading index https://data1.fullyjustified.net/tlextras-2022.0r0.tar.index.gz
note: downloading SHA256SUM
note: generating format "latex"
note: downloading tectonic-format-latex.tex
note: downloading xelatex.ini
note: downloading latex.ltx
note: downloading texsys.cfg
note: downloading expl3.ltx
note: downloading expl3-code.tex
note: downloading cmr10.tfm
note: downloading UnicodeData.txt
note: downloading CaseFolding.txt
note: downloading SpecialCasing.txt
note: downloading fonttext.cfg
note: downloading fonttext.ltx
note: downloading omlenc.def
note: downloading omsenc.def
note: downloading ot1enc.def
note: downloading t1enc.def
note: downloading ts1enc.def
note: downloading tuenc.def
note: downloading tulmr.fd
note: downloading tulmss.fd
note: downloading tulmtt.fd
note: downloading ts1lmr.fd
note: downloading t1cmr.fd
note: downloading ot1cmr.fd
note: downloading ot1cmss.fd
note: downloading ot1cmtt.fd
note: downloading fontmath.cfg
note: downloading fontmath.ltx
note: downloading omlcmm.fd
note: downloading omscmsy.fd
note: downloading omxcmex.fd
note: downloading ucmr.fd
note: downloading preload.cfg
note: downloading preload.ltx
note: downloading cmex10.tfm
note: downloading line10.tfm
note: downloading linew10.tfm
note: downloading lcircle10.tfm
note: downloading lcirclew10.tfm
note: downloading cmr5.tfm
note: downloading cmr7.tfm
note: downloading cmmi5.tfm
note: downloading cmmi7.tfm
note: downloading cmmi10.tfm
note: downloading cmsy5.tfm
note: downloading cmsy7.tfm
note: downloading cmsy10.tfm
note: downloading atveryend.sty
note: downloading atveryend-ltx.sty
note: downloading atbegshi.sty
note: downloading atbegshi-ltx.sty
note: downloading load-unicode-data.tex
note: downloading hyphen.cfg
note: downloading xebabel.def
note: downloading language.dat
note: downloading hyphen.tex
note: downloading dumyhyph.tex
note: downloading zerohyph.tex
note: downloading dehypht-x-2022-03-16.tex
note: downloading dehypht-x-2022-03-16.pat
note: downloading dehyphn-x-2022-03-16.tex
note: downloading dehyphn-x-2022-03-16.pat
note: downloading loadhyph-af.tex
note: downloading hyph-af.tex
note: downloading hyph-quote-af.tex
note: downloading loadhyph-grc.tex
note: downloading hyph-grc.tex
note: downloading ibyhyph.tex
note: downloading loadhyph-hy.tex
note: downloading hyph-hy.tex
note: downloading loadhyph-eu.tex
note: downloading hyph-eu.tex
note: downloading loadhyph-be.tex
note: downloading hyph-be.tex
note: downloading hyph-quote-be.tex
note: downloading loadhyph-bg.tex
note: downloading hyph-bg.tex
note: downloading loadhyph-ca.tex
note: downloading hyph-ca.tex
note: downloading loadhyph-zh-latn-pinyin.tex
note: downloading hyph-zh-latn-pinyin.tex
note: downloading loadhyph-cu.tex
note: downloading hyph-cu.tex
note: downloading loadhyph-cop.tex
note: downloading hyph-cop.tex
note: downloading loadhyph-hr.tex
note: downloading hyph-hr.tex
note: downloading loadhyph-cs.tex
note: downloading hyph-cs.tex
note: downloading loadhyph-da.tex
note: downloading hyph-da.tex
note: downloading loadhyph-nl.tex
note: downloading hyph-nl.tex
note: downloading loadhyph-en-gb.tex
note: downloading hyph-en-gb.tex
note: downloading loadhyph-en-us.tex
note: downloading hyph-en-us.tex
note: downloading loadhyph-eo.tex
note: downloading hyph-eo.tex
note: downloading loadhyph-et.tex
note: downloading hyph-et.tex
note: downloading loadhyph-mul-ethi.tex
note: downloading hyph-mul-ethi.tex
note: downloading loadhyph-fi.tex
note: downloading hyph-fi.tex
note: downloading loadhyph-fi-x-school.tex
note: downloading hyph-fi-x-school.tex
note: downloading loadhyph-fr.tex
note: downloading hyph-fr.tex
note: downloading hyph-quote-fr.tex
note: downloading loadhyph-fur.tex
note: downloading hyph-fur.tex
note: downloading hyph-quote-fur.tex
note: downloading loadhyph-gl.tex
note: downloading hyph-gl.tex
note: downloading loadhyph-ka.tex
note: downloading hyph-ka.tex
note: downloading loadhyph-de-1901.tex
note: downloading hyph-de-1901.tex
note: downloading loadhyph-de-1996.tex
note: downloading hyph-de-1996.tex
note: downloading loadhyph-de-ch-1901.tex
note: downloading hyph-de-ch-1901.tex
note: downloading loadhyph-el-polyton.tex
note: downloading hyph-el-polyton.tex
note: downloading loadhyph-el-monoton.tex
note: downloading hyph-el-monoton.tex
note: downloading loadhyph-hu.tex
note: downloading hyph-hu.tex
note: downloading loadhyph-is.tex
note: downloading hyph-is.tex
note: downloading loadhyph-as.tex
note: downloading hyph-as.tex
note: downloading loadhyph-bn.tex
note: downloading hyph-bn.tex
note: downloading loadhyph-gu.tex
note: downloading hyph-gu.tex
note: downloading loadhyph-hi.tex
note: downloading hyph-hi.tex
note: downloading loadhyph-kn.tex
note: downloading hyph-kn.tex
note: downloading loadhyph-ml.tex
note: downloading hyph-ml.tex
note: downloading loadhyph-mr.tex
note: downloading hyph-mr.tex
note: downloading loadhyph-or.tex
note: downloading hyph-or.tex
note: downloading loadhyph-pi.tex
note: downloading hyph-pi.tex
note: downloading loadhyph-pa.tex
note: downloading hyph-pa.tex
note: downloading loadhyph-ta.tex
note: downloading hyph-ta.tex
note: downloading loadhyph-te.tex
note: downloading hyph-te.tex
note: downloading loadhyph-id.tex
note: downloading hyph-id.tex
note: downloading loadhyph-ia.tex
note: downloading hyph-ia.tex
note: downloading loadhyph-ga.tex
note: downloading hyph-ga.tex
note: downloading loadhyph-it.tex
note: downloading hyph-it.tex
note: downloading hyph-quote-it.tex
note: downloading loadhyph-kmr.tex
note: downloading hyph-kmr.tex
note: downloading loadhyph-la-x-classic.tex
note: downloading hyph-la-x-classic.tex
note: downloading loadhyph-la.tex
note: downloading hyph-la.tex
note: downloading loadhyph-la-x-liturgic.tex
note: downloading hyph-la-x-liturgic.tex
note: downloading loadhyph-lv.tex
note: downloading hyph-lv.tex
note: downloading loadhyph-lt.tex
note: downloading hyph-lt.tex
note: downloading loadhyph-mk.tex
note: downloading hyph-mk.tex
note: downloading loadhyph-mn-cyrl.tex
note: downloading hyph-mn-cyrl.tex
note: downloading loadhyph-mn-cyrl-x-lmc.tex
note: downloading hyph-mn-cyrl-x-lmc.tex
note: downloading loadhyph-nb.tex
note: downloading hyph-nb.tex
note: downloading hyph-no.tex
note: downloading loadhyph-nn.tex
note: downloading hyph-nn.tex
note: downloading loadhyph-oc.tex
note: downloading hyph-oc.tex
note: downloading hyph-quote-oc.tex
note: downloading loadhyph-pms.tex
note: downloading hyph-pms.tex
note: downloading hyph-quote-pms.tex
note: downloading loadhyph-pl.tex
note: downloading hyph-pl.tex
note: downloading loadhyph-pt.tex
note: downloading hyph-pt.tex
note: downloading loadhyph-ro.tex
note: downloading hyph-ro.tex
note: downloading loadhyph-rm.tex
note: downloading hyph-rm.tex
note: downloading hyph-quote-rm.tex
note: downloading loadhyph-ru.tex
note: downloading hyph-ru.tex
note: downloading loadhyph-sa.tex
note: downloading hyph-sa.tex
note: downloading loadhyph-sr-latn.tex
note: downloading hyph-sh-latn.tex
note: downloading hyph-sh-cyrl.tex
note: downloading loadhyph-sr-cyrl.tex
note: downloading loadhyph-sk.tex
note: downloading hyph-sk.tex
note: downloading loadhyph-sl.tex
note: downloading hyph-sl.tex
note: downloading loadhyph-es.tex
note: downloading hyph-es.tex
note: downloading loadhyph-sv.tex
note: downloading hyph-sv.tex
note: downloading loadhyph-th.tex
note: downloading hyph-th.tex
note: downloading loadhyph-tr.tex
note: downloading hyph-tr.tex
note: downloading loadhyph-tk.tex
note: downloading hyph-tk.tex
note: downloading loadhyph-uk.tex
note: downloading hyph-uk.tex
note: downloading hyph-quote-uk.tex
note: downloading loadhyph-hsb.tex
note: downloading hyph-hsb.tex
note: downloading loadhyph-cy.tex
note: downloading hyph-cy.tex
note: downloading latex2e-first-aid-for-external-files.ltx
note: Running TeX ...
note: downloading article.cls
note: downloading size10.clo
note: downloading lmroman10-regular.otf
note: downloading tex-text.tec
note: downloading threeparttable.sty
note: downloading multirow.sty
note: downloading booktabs.sty
note: downloading l3backend-xetex.def
note: downloading ts1cmr.fd
note: downloading lmroman10-bold.otf
note: Rerunning TeX because "main.aux" changed ...
note: Running xdvipdfmx ...
note: downloading pdftex.map
note: downloading kanjix.map
note: downloading ckx.map
note: downloading texglyphlist.txt
note: downloading pdfglyphlist.txt
note: downloading glyphlist.txt
note: Writing `/tmp/jl_zRtbt1/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). The Typst backend is using the tablex package. Due to the way Typst's package manager works, you do not have to add any other information to your .typ file to make SummaryTables's typst code work.

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
downloading @preview/tablex:0.0.8
 41.8 KiB /  41.8 KiB (100 %)  41.8 KiB/s in  0s ETA:  0s

Download example_typst.pdf: