# Creating a variable inside a macro with specified name

**URL:** <https://discourse.julialang.org/t/creating-a-variable-inside-a-macro-with-specified-name/102187>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, macros, macro-hygiene\
**Created:** [July 28, 2023, 8:34am UTC](https://discourse.julialang.org/t/creating-a-variable-inside-a-macro-with-specified-name/102187 "2023-07-28T08:34:48Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)\
**Post date:** [July 28, 2023, 1:47pm UTC](https://discourse.julialang.org/t/creating-a-variable-inside-a-macro-with-specified-name/102187/5 "2023-07-28T13:47:36Z")

</div>

I think it might be useful to have a read at this other discourse post:

> [@Escaping in a macro](https://discourse.julialang.org/t/escaping-in-a-macro/14543):
>
> In the documentation of Julia v1.0 there is almost nothing about escaping in a macro. I tried a simple debug function, which can be inserted at critical points of a function. It should print an expression or variable name, and its current value. I defined a macro in a module, but I get weird messages (b = c =) at loading the module, and within a function only the last value of several calls to the macro is actually shown in REPL. If I type in REPL @db(1+2) or a = falses(2); @db(a) I get the de…

to have a better understanding of escaping in macros.

As written there, each input generally has to be escaped **once and exactly once**.

So for your added example, you also just avoid escaping `model` (like done for `args` in the previous answer) inside this line:

```julia
ref = :(add_variable($(esc(model)),$(args)))

```

as it will anyhow be escaped once you escape the quote returned by the macro.

---

_[View the full topic](https://discourse.julialang.org/t/creating-a-variable-inside-a-macro-with-specified-name/102187)._
