Trouble using the @time module

I want to use the @time begin ... end module, but am encountering the following error

ERROR: LoadError: UndefVarError: @L_str not defined

This error occurs only, it seems, when I have defined my code to be within a module. Why?

MWE:

@time begin

using GLMakie,LaTeXStrings, MathTeXEngine
GLMakie.activate!()

scatter_data = [1,2,3,4,5,6,7,8]
x=1:8
fig = Figure(backgroundcolor=:snow2)

axis = Axis(fig[1,1], xlabel="Example scatter", title=L"\mathbb{Z}")
scatter!(axis, x, scatter_data)

display(fig)
end
end

I think your issue is with the title value, title=L"\mathbb{Z}". I’m assuming the L string macro comes from LatexStrings. Are you using a version of LatexStrings that defines that macro? Is it exported?

All my packages are up to date: https://i.imgur.com/zjNMGjv.png

@L_str is exported from here:

  1. How are your files structured?
  2. Can you elaborate how you are using modules?
  3. Are you building a package, which may have a distinct Project.toml and Manifest.toml which is being loaded?
  4. Can you provide a complete example including the module declaration?
  5. Are you explicitly using environments?
  6. Is @time necessary to create the error? If you remove it dows it work?

OP is mis-using the term “module”. There are no modules involved. The MWE problem is entirely below.

This works:

(@v1.9) pkg> activate --temp

(jl_IcBFB8) pkg> add GLMakie, LaTeXStrings, MathTeXEngine

julia> using GLMakie,LaTeXStrings, MathTeXEngine
       GLMakie.activate!()

       scatter_data = [1,2,3,4,5,6,7,8]
       x=1:8
       fig = Figure(backgroundcolor=:snow2)

       axis = Axis(fig[1,1], xlabel="Example scatter", title=L"\mathbb{Z}")
       scatter!(axis, x, scatter_data)

       display(fig)
GLMakie.Screen(...)

This errors:

(@v1.9) pkg> activate --temp

(jl_IcBFB8) pkg> add GLMakie, LaTeXStrings, MathTeXEngine

julia> @time begin

       using GLMakie,LaTeXStrings, MathTeXEngine
       GLMakie.activate!()

       scatter_data = [1,2,3,4,5,6,7,8]
       x=1:8
       fig = Figure(backgroundcolor=:snow2)

       axis = Axis(fig[1,1], xlabel="Example scatter", title=L"\mathbb{Z}")
       scatter!(axis, x, scatter_data)

       display(fig)
       end
ERROR: LoadError: UndefVarError: `@L_str` not defined
in expression starting at REPL[3]:10

The @time is irrelevant. I think using statements cannot be inside the same begin end block where you use them macros (edited).

julia>     begin

              using GLMakie,LaTeXStrings, MathTeXEngine
              GLMakie.activate!()

              scatter_data = [1,2,3,4,5,6,7,8]
              x=1:8
              fig = Figure(backgroundcolor=:snow2)

              axis = Axis(fig[1,1], xlabel="Example scatter", title=L"\mathbb{Z}")
              scatter!(axis, x, scatter_data)

              display(fig)
           end
ERROR: LoadError: UndefVarError: `@L_str` not defined
in expression starting at REPL[3]:10

julia>     begin
              using GLMakie,LaTeXStrings, MathTeXEngine
           end

julia> begin
              GLMakie.activate!()

              scatter_data = [1,2,3,4,5,6,7,8]
              x=1:8
              fig = Figure(backgroundcolor=:snow2)

              axis = Axis(fig[1,1], xlabel="Example scatter", title=L"\mathbb{Z}")
              scatter!(axis, x, scatter_data)

              display(fig)
        end
GLMakie.Screen(...)

You will have to time them separately and then add the times.

julia> t1 = @elapsed begin
       using GLMakie, LaTeXStrings, MathTeXEngine
       end
0.0034929

julia> t2 = @elapsed begin
              GLMakie.activate!()

              scatter_data = [1,2,3,4,5,6,7,8]
              x=1:8
              fig = Figure(backgroundcolor=:snow2)

              axis = Axis(fig[1,1], xlabel="Example scatter", title=L"\mathbb{Z}")
              scatter!(axis, x, scatter_data)

              display(fig)
       end
0.0995245

julia> t = t1 + t2 # seconds
0.10301740000000001
2 Likes

I think the problem is specifically using macros defined in modules that are used/imported within the same begin/end block. Not generically about the using statements.

I believe this is because Julia first tries to compile all macros in the begin…end before executing it. I had this happen to me some times in Pluto

2 Likes

Aren’t you supposed to @time @eval begin... anyway when timing using plus first call latency to avoid Julia compiling anything outside the timing operation? The @eval might also help with the macro bug here

1 Like

Thank you for pointing this out! I also ran into this issue (when loading DimensionalData and defining a new Dimension using the @dim macro within the same @everywhere begin ... end block). I eventually figured out I needed two separate @everywhere blocks to make it work, but didn’t realize it was a general issue. Is this gotcha documented anywhere?

1 Like