# Writing a simple macro for pretty differentiation

**URL:** <https://discourse.julialang.org/t/writing-a-simple-macro-for-pretty-differentiation/113247>\
**Category:** New to Julia\
**Created:** [April 19, 2024, 3:47pm UTC](https://discourse.julialang.org/t/writing-a-simple-macro-for-pretty-differentiation/113247 "2024-04-19T15:47:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bizzi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bizzi/32/51484_2.png) [@Bizzi](https://discourse.julialang.org/u/Bizzi)\
**Post date:** [April 19, 2024, 3:47pm UTC](https://discourse.julialang.org/t/writing-a-simple-macro-for-pretty-differentiation/113247/1 "2024-04-19T15:47:20Z")

</div>

Trying to write my first macro and I’m surprised at just how hard it really is.

My goal is simple. I would like to be able to write something like the following

```julia
@∂(t)f(t,x,y)

```

and have it mean

```julia
 AD.derivative(z -> f(z,x,y), t)

```

where `AD` stands for the `AbstractDifferentiation` module and `z` stands for some macro-hygienic anonymous variable.

I’ve not been able to make this syntax work, however. I’ve tried to have `@∂(t)` translate to some other macro that would then apply the necessary transformations to `f`, but the way macros are processed has left me deeply confused.

This seems like a pretty simple and obvious use for a macro, right? Can anyone share any guidance on how I could do it?

---

<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:** [April 19, 2024, 4:21pm UTC](https://discourse.julialang.org/t/writing-a-simple-macro-for-pretty-differentiation/113247/2 "2024-04-19T16:21:14Z")

</div>

> [@Bizzi](#):
>
> `@∂(t)f(t,x,y)`

This won’t work because it parses as two expressions that are multiplied:

```julia
julia> :(@∂(t)f(t,x,y))
:(#= REPL[16]:1 =# @∂(t) * f(t, x, y))

```

You need something after the macro call `@∂` that parses as a single expression.
