If I declare a function like f(x) = x^2 + log(x)

how to I display it in a function like

title!=f(x)

If I declare a function like f(x) = x^2 + log(x)

how to I display it in a function like

title!=f(x)

```
foo(x) = x^2 + log(x)
@assert nameof(foo) == :foo # nameof(foo) returns Symbol :foo
```

`string(f)`

may be a little more directly useful than `nameof(f)`

since you intend to make this a plot title.

Unless you want the string to be `"x^2 + log(x)"`

. In that case, I would suggest you just make a function to give a string version of the function

```
julia> f(x) = x^2 + log(x)
x^2 + log(x)
julia> stringof(::typeof(f)) = "x^2 + log(x)"
stringof (generic function with 1 method)
julia> stringof(f)
"x^2 + log(x)"
```

You may want to elaborate what you actually want to achieve. This sounds like a potential XY Problem.

1 Like

Ok, thanks for your help but I think I need to elaborate as @giordano has requested.

If I write

g(x) = x^2 + log(x) #for example

I want to be able to do things like

s= string(g(x)) and get s = “s^2 + log(x)”

and therefore be able to type

println(s) and get s^2 + log(x)

The point is that if I change the original g(x), I don’t want to have to explicitly set it to it’s string value every time.

Why? What is you underlying goal here?

If you really want to do this, you can use GitHub - timholy/CodeTracking.jl: It's editing-time, do you know where your methods are?

There is a longstanding request to build something like this into Julia, e.g. for debugging purposes: Request: Store the Expr behind each Method object · Issue #24347 · JuliaLang/julia · GitHub

1 Like

My goal is that I want to display the function string as the title of a plot of the function, as well as plot it. Currently I need to put it in twice, once as a function and once as a string.

This code is for others to use, and I only want them to need to enter g once.

I have solved it in reverse now, by entering it in a string and converting this to be a function.

2 Likes

What’s intended to happen when multiple coexisting methods are given? Say the user inputs:

`f(x) = x^2 + log(x)`

`f(x::Integer) = x * log2(x)`

Watch out for code injection.

Just to demonstrate how this alternative would work:

```
julia> using CodeTracking
julia> g(x) = x^2 + log(x)
g (generic function with 1 method)
julia> @code_string g(1.0) # macro form
"g(x) = x^2 + log(x)"
julia> code_string(g, Tuple{Float64}) # function form
"g(x) = x^2 + log(x)"
```

3 Likes

You may find Handcalcs.jl helpful as well if you want the LaTeX version of the function.

This uses CodeTracking under the hood. Note though that this function needs to be in a separate package. One of the limitations of CodeTracking. However, I think it works if you are just using the REPL.

Actually. After looking a little closer. Handcalcs probably isn’t the best option since it will render a numerical substitution as well. I do want to add symbolic option though.

This is what I came up with:

```
using Plots, Handcalcs
pgfplotsx()
function g(x)
g = x^2 + log(x)
end
latex = @handcalc f = g(5)
plot(g, title=latex)
```

3 Likes