Passing a format string as a function argument

I am trying to pass a format string as a function argument in the following code -

using Printf

fmt = "%.0e"

function latex_exp_note(flt,fmt)
    s = @sprintf(fmt,flt)
    b,e = split(s,"e")
    e = replace(e,"+"=>"")
    e = replace(e,"0"=>"")
    s = raw"$"*b*raw"\times 10^{"*e*raw"}$"
    s = replace(s,"{}"=>"{0}")
    return latexstring(s)
end

display(latex_exp_note(2.5,fmt))

when I run the code I get this error -

LoadError: ArgumentError: First argument to @sprintf must be a format string.

how do it define fmt to be a format string that can be passed into a function?

Short answer, you can’t pass it as an argument to the @sprintf macro.
Instead use the function the macro call expands to:

s = Printf.format(fmt, fld)

and also the format string is not a string, but needs to be preprocessed via

fmt = Printf.format"%.0e"

See this thread for the discussion of basically the same problem.

Thank you for a solution that worked. There needs to be more documentation on this subject.