Latexstring + sprintf

Hello!

For plotting I’m often using something similar to

latexstring(@sprintf("a=%.3f \\beta, b=%.3f \\alpha",π, 1-π))

and I was wondering if somebody is using a better option or has an idea which 1) has something like a clear c-style number to string conversion, b) a latex option (which ideally does not need escaping), c) is shorter to write (this expression cannot be turned into a function, since @sprintf is a macro).

Thanks!

I usually prefer to use $ (or %$) in LaTeXStrings, eg

using LaTeXStrings
L"a=%$(round(π; digits = 3)) \beta, b=%$(round(1-π; digits = 3)) \alpha"

but when I am making a lot of plots I define utility functions for rounding, eg

d3(x) = round(x; digits = 3)
L"a=%$(d3(π)) \beta, b=%$(d3(1-π)) \alpha"

(You can skip the extra ()s, just a personal preference.)

4 Likes