# Using @lift with @sprintf for animation in Makie.jl

**URL:** <https://discourse.julialang.org/t/using-lift-with-sprintf-for-animation-in-makie-jl/86746>\
**Category:** General Usage\
**Tags:** strings, makie\
**Created:** [September 4, 2022, 12:25am UTC](https://discourse.julialang.org/t/using-lift-with-sprintf-for-animation-in-makie-jl/86746 "2022-09-04T00:25:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yhchang96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yhchang96/32/36499_2.png) [@yhchang96](https://discourse.julialang.org/u/yhchang96)\
**Post date:** [September 4, 2022, 12:25am UTC](https://discourse.julialang.org/t/using-lift-with-sprintf-for-animation-in-makie-jl/86746/1 "2022-09-04T00:25:33Z")

</div>

I want to display a text box which acts as a counter in an animation. I followed the tutorial [here](https://makie.juliaplots.org/stable/documentation/animation/#animations_using_observables), but the text I want to display consists of an integer and a really small number (which is visually better displayed using scientific notation with fixed decimal places with `@sprintf`).

The following works:

```julia
iter_idx = Observable(1)
text_str = @lift("iteration : $($iter_idx-1)\ncost function : $(trace_J[$iter_idx])")
text!(ax1, Point2f(0, 0), text=text_str, align=(:left, :top))

```

but then the value of cost function displayed will have fluctuating number of digits and switch between floating point numbers and scientific notation for display.

I tried the following

```julia
text_str = @lift(@sprintf("iteration : %.0f\ncost function : %.3e",$iter_idx, $(trace_J[$iter_idx])))

```

but I get the error

```julia
ERROR: syntax: "$" expression outside quote around ...

```

I also tried

```julia
current_iter = @lift($iter_idx-1)
current_J = @lift(trace_J[$iter_idx])
text_str = @sprintf("iteration : %.0f\ncost function : %.3e", current_iter, current_J)

```

which gives the error

```julia
ERROR: MethodError: no method matching Float64(::Observable{Int64})
Closest candidates are:
  (::Type{T})(::AbstractChar) where T<:Union{AbstractChar, Number} at char.jl:50
  (::Type{T})(::Base.TwicePrecision) where T<:Number at twiceprecision.jl:266
  (::Type{T})(::Complex) where T<:Real at complex.jl:44

```

So what’s the proper way to handle this where I want string formatting for a text which is being updated at each frame of the animation?

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [September 4, 2022, 12:39am UTC](https://discourse.julialang.org/t/using-lift-with-sprintf-for-animation-in-makie-jl/86746/2 "2022-09-04T00:39:47Z")

</div>

More than one macro confuses Julia. What I have ended up doing is getting rid of one macro call and burying the other one.

Something like

```julia
function title_string(current_iter, current_J) 
    @sprintf "iteration : %.0f\ncost function : %.3e" current_iter current_J 
end

iter_idx = Observable(1)

text_itr = lift(t -> title_string(trace_J[t], t - 1), iter_idx) #Avoid the macro call here

text!(ax1, Point2f(0, 0), text=text_str, align=(:left, :top))

```

(You may need to adjust to make it run but that is the idea)
