UnitfulLatexify.jl

A glue package for Unitful.jl and Latexify.jl, allowing easy and pretty latexification of Unitful quantities, numbers and units.

The default usage is pretty intuitive:

using Unitful, Latexify, UnitfulLatexify

a = 9.82u"m/s^2"
t = 4u"s"
x = a*t^2

latexify(x)
$157.1\;\mathrm{m}$

or more usefully:

latexify(:(x = a*t^2 = $x))
$x = a \cdot t^{2} = 157.1\;\mathrm{m}$

This of course also works for Units objects by themselves:

latexify(u"kg*m")
$\mathrm{kg}\,\mathrm{m}$

Some more usage examples:

Arrays

Because Latexify is recursive, an array of unitful quantities is shown as expected:

latexify([12u"m", 1u"m^2", 4u"m^3"])
\begin{equation} \left[ \begin{array}{c} 12\;\mathrm{m} \\ 1\;\mathrm{m}^{2} \\ 4\;\mathrm{m}^{3} \\ \end{array} \right] \end{equation}

A special case is an array where all elements have the same unit, and here UnitfulLatexify does some extra work:

latexify([1, 2, 3]u"cm")
\begin{equation} \left[ \begin{array}{c} 1 \\ 2 \\ 3 \\ \end{array} \right]\;\mathrm{cm} \end{equation}

siunitx.sty

If you are exporting your numbers to an actual LaTeX document, you will of course want to use the commands from siunitx.sty rather than the \mathrm style used by default. To this end, UnitfulLatexify introduces a keyword argument unitformat which is :mathrm per default, but can be set to :siunitx for \SI{8}{\second\meter\per\kilo\gram} style and :siunitxsimple for \SI{8}{s.m/kg}. Like other Latexify keywords, this can be set to be a default by using set_default(unitformat=:siunitx), or given with each latexification command:

latexify(612.2u"nm"; unitformat=:siunitx) # This will not render right without the `siunitx` package
\SI{612.2}{\nano\meter}

One

siunitx can also render unitless numbers nicely by putting them in \num commands. As the Unitful NoUnits is so fragile, UnitfulLatexify exports a unit u"one" which stays with a quantity until it's combined with another unit. You'll have to invoke this manually:

latexify(2e6u"one"; unitformat=:siunitx)
\num{2.0e6}

Ranges and lists

Another thing that siunitx does uniquely is lists and ranges of quantities. If you want the default behaviour of tuples and ranges to be printed as arrays, use collect(x) or [x...] to explicitly turn them into arrays first.

string.([
latexify((1:5)u"m"),
latexify((1:5)u"m"; unitformat=:siunitx),
latexify(collect((1:5)u"m"); unitformat=:siunitx),
latexify((1u"m", 2u"m", 3u"m"); unitformat=:siunitx),
latexify((1:5)u"one"; unitformat=:siunitx),
])
5-element Vector{LaTeXString}:
 L"$\left[
\begin{array}{c}
1 \\
2 \\
3 \\
4 \\
5 \\
\end{array}
\right]\;\mathrm{m}$"
 L"\SIrange{1}{5}{\meter}"
 L"\begin{equation}
\left[
\begin{array}{c}
\num{1} \\
\num{2} \\
\num{3} \\
\num{4} \\
\num{5} \\
\end{array}
\right]\;\si{\meter}
\end{equation}
"
 L"\SIlist{1;2;3}{\meter}"
 L"\numrange{1}{5}"

Plots labels

UnitfulLatexify also interfaces with UnitfulRecipes.jl by way of implementing a two-argument (label, unit) recipe:

latexify("v", u"km/s")
$v\;/\;\mathrm{km}\,\mathrm{s}^{-1}$

This enables this dreamlike example:

using Unitful, Plots, Latexify, UnitfulRecipes, UnitfulLatexify
gr()
default(fontfamily="Computer Modern")

m = randn(10)u"kg"
v = randn(10)u"m/s"
plot(m, v; xguide="\\mathrm{mass}", yguide="v_x", unitformat=latexify)
−2 −1 0 1 −2 −1 0 1 y1

(note that this keyword argument unitformat is named the same by coincidence, but unrelated to the unitformat of latexify)

This format, $v_x\;/\;\mathrm{m}\,\mathrm{s}^{-1}$, is subject to personal preference. UnitfulLatexify offers a couple of other formats, and you can simply provide any two-argument function that turns a label and a unit into a string:

args = (m, v)
kwargs = (xguide="\\mathrm{mass}", yguide="v_x", legend=false)
plot(
	plot(args...; kwargs..., unitformat=latexslashunitlabel),
	plot(args...; kwargs..., unitformat=latexroundunitlabel),
	plot(args...; kwargs..., unitformat=latexsquareunitlabel),
	plot(args...; kwargs..., unitformat=latexfracunitlabel),
	plot(args...; kwargs..., unitformat=(l, u)->string("\$", l, " \\rightarrow ", latexraw(u), "\$")),
)
−2 −1 0 1 −2 −1 0 1 −2 −1 0 1 −2 −1 0 1 −2 −1 0 1 −2 −1 0 1 −2 −1 0 1 −2 −1 0 1 −2 −1 0 1 −2 −1 0 1

A more complete list of defined units

Below is a poorly scraped list of units defined in siunitx and what comes out if you run it through latexify. Feel free to create an issue if there's a unit missing or being incorrectly rendered (and suggest a better $\LaTeX$ representation if you know one).