# Printing a function

**URL:** <https://discourse.julialang.org/t/printing-a-function/119957>\
**Category:** General Usage\
**Created:** [September 27, 2024, 12:25pm UTC](https://discourse.julialang.org/t/printing-a-function/119957 "2024-09-27T12:25:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Warburton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_warburton/32/206802_2.png) [@Paul\_Warburton](https://discourse.julialang.org/u/Paul_Warburton)\
**Post date:** [September 27, 2024, 12:25pm UTC](https://discourse.julialang.org/t/printing-a-function/119957/1 "2024-09-27T12:25:07Z")

</div>

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

how to I display it in a function like  
title!=f(x)

---

<div class="post-metadata">

**Author:** ![zgornel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zgornel/32/217487_2.png) [@zgornel](https://discourse.julialang.org/u/zgornel)\
**Post date:** [September 27, 2024, 12:37pm UTC](https://discourse.julialang.org/t/printing-a-function/119957/2 "2024-09-27T12:37:59Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [September 27, 2024, 3:13pm UTC](https://discourse.julialang.org/t/printing-a-function/119957/3 "2024-09-27T15:13:55Z")

</div>

`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-repl
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)"

```

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [September 27, 2024, 3:14pm UTC](https://discourse.julialang.org/t/printing-a-function/119957/4 "2024-09-27T15:14:25Z")

</div>

You may want to elaborate what you actually want to achieve. This sounds like a potential [XY Problem](https://xyproblem.info/).

---

<div class="post-metadata">

**Author:** ![Paul\_Warburton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_warburton/32/206802_2.png) [@Paul\_Warburton](https://discourse.julialang.org/u/Paul_Warburton)\
**Post date:** [September 28, 2024, 12:48am UTC](https://discourse.julialang.org/t/printing-a-function/119957/5 "2024-09-28T00:48:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 28, 2024, 1:12am UTC](https://discourse.julialang.org/t/printing-a-function/119957/6 "2024-09-28T01:12:33Z")

</div>

> [@Paul\_Warburton](#):
>
> I want to be able to do things like  
> s= string(g(x)) and get s = “s^2 + log(x)”

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?](https://github.com/timholy/CodeTracking.jl)

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](https://github.com/JuliaLang/julia/issues/24347)

---

<div class="post-metadata">

**Author:** ![Paul\_Warburton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_warburton/32/206802_2.png) [@Paul\_Warburton](https://discourse.julialang.org/u/Paul_Warburton)\
**Post date:** [September 28, 2024, 2:16am UTC](https://discourse.julialang.org/t/printing-a-function/119957/7 "2024-09-28T02:16:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 28, 2024, 4:19am UTC](https://discourse.julialang.org/t/printing-a-function/119957/8 "2024-09-28T04:19:37Z")

</div>

> [@Paul\_Warburton](#):
>
> if I change the original g(x), I don’t want to have to explicitly set it to it’s string value every time.

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

1. `f(x) = x^2 + log(x)`
2. `f(x::Integer) = x * log2(x)`

> [@Paul\_Warburton](#):
>
> entering it in a string and converting this to be a function.

Watch out for code injection.

---

<div class="post-metadata">

**Author:** ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)\
**Post date:** [September 28, 2024, 4:27am UTC](https://discourse.julialang.org/t/printing-a-function/119957/9 "2024-09-28T04:27:04Z")

</div>

> [@stevengj](#):
>
> 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?](https://github.com/timholy/CodeTracking.jl)

Just to demonstrate how this alternative would work:

```julia-repl
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)"

```

---

<div class="post-metadata">

**Author:** ![co1emi11er](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/co1emi11er/32/206849_2.png) [@co1emi11er](https://discourse.julialang.org/u/co1emi11er)\
**Post date:** [September 28, 2024, 2:27pm UTC](https://discourse.julialang.org/t/printing-a-function/119957/10 "2024-09-28T14:27:30Z")

</div>

You may find [Handcalcs.jl](https://co1emi11er2.github.io/Handcalcs.jl/stable/tutorial/#Function-Examples) 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:

```Julia
using Plots, Handcalcs
pgfplotsx()

function g(x)
   g = x^2 + log(x)
end

latex = @handcalc f = g(5)

plot(g, title=latex)

```

 ![CleanShot 2024-09-28 at 09.42.31@2x](https://global.discourse-cdn.com/julialang/original/3X/4/5/45074e543c2734f6f951860fbb9414abc0243444.png)
