[ANN] UnitfulLatexify.jl

Announcing UnitfulLatexify.jl – A glue package between Unitful and Latexify (surprise!).

Creates LaTeXStrings from Unitful quantities and units, either on the form 35\;\mathrm{cm} or \SI{35}{\centi\meter}.

I think it’s pretty cool.


I like it! Is there a way to get

latexify(1.12345e6*u, unitformat=:mathrm)
L"$1.12345 \times 10^6\;\mathrm{kg}\mathrm{m}\mathrm{s}^{-2}$"

instead of



That would be great :slight_smile:

I haven’t had the opportunity to use UnitfulLatexify (yet!) but it appears to use the optionally use the siunitx package which will do as you desire.
siunitx is great for ensuring numbers with units are formatted correctly including scientific notation and spacing rather than commas as the thousands-place separators. That is the “\SI{35}{\centi\meter}” example.

Yes. Apart from the siunitx route, you can use all the formatting options from Latexify.jl, so latexify(1.2e16u"m",fmt="%.2e") should do something like what you want.

Great idea for a package and welcome to discourse!

And it even also works with siunitx – great! I definitely keep that in mind for my latex exports!

edit: I just saw, that Unitful has NoUnits, would that maybe be a good replacement for u"one"?

In principle yes. I actually didn’t find that one when looking through Unitful. It has the big disadvantage that from what I see the only way to get a Quantity with NoUnits that doesn’t immediately simplify to a number is to run something like Quantity{Int64,NoDims,Unitful.FreeUnits{(),NoDims,nothing}}(28), and then it’s not “safe” to perform any arithmetic with. typeof(24u"NoUnits") == Int64. But maybe there’s a way around this, I shall investigate. Thanks for the tip!

1 Like

Thanks for the insight – I was just wondering, since u"one"looked as Unitful had no NoUnits type, that’s why I was looking for that. Also, there are surely numbers that really just have no unit.

Hmmm your example does a normal printf format, so checking the docs of Latexify.jl I found

latexify(1.2e16; fmt=FancyNumberFormatter(3))
L"$1.2 \cdot 10^{16}$"

nice! But when combined with the unit this does not work:

latexify(1.2e16u"m"; fmt=FancyNumberFormatter(3))

Everyone should always be using siunitx(!), it’s one of the absolute core packages that I/we use in everything we do. Strongly recommended.

Not sure how you generate the units, but it’s perhaps nice to be aware that siunitx understands ‘simple’ units, so you can just write \SI{35}{cm/kg^2} instead of \SI{35}{\centi\meter\per\kilo\gram\squared}.

1 Like

My bad. Here I go advertising features from my internal wish list.

I’m working on a fix for this, and making good head way. I must have just dreamed about this working the way I thought.

1 Like

No it doesn’t? Siunitx will allow you to enter literal text into the argument of \si, but it will not do proper siunitx stuff to it. Try \si{cm s / m^2} \si{\centi\meter\second\per\meter\squared} and you’ll see the difference.

Yes, it will:


    \si{\centi\meter\second\per\meter\squared} \\
    \si{cm.s.m^{-2}} \\

The macro versions are for programming purposes, and are not needed during ordinary editing.

Huh, good to know. I’ve never seen that. I wonder if it was introduced after I learned siunitx or if I just missed it.

I’ll probably keep using the macros, it feels more TeX philosophic, and I absolutely detest the product period.

v1.1.2 has fixed this. The default fmt for unitformat=:mathrm is FancyNumberFormatter().

Oh, I really quite like that. Perhaps this is configurable, not sure.

One of my favourite things is that you can easily write the micro sign like this

\SI{35}{\us}  # or \um or \uJ, or whatever.

1.2 has unitformat=:siunitxsimple which uses the abbreviation version.

1 Like

This package made my day… thanks so much… :hugs: