# How to let the user specify the variable name?

**URL:** <https://discourse.julialang.org/t/how-to-let-the-user-specify-the-variable-name/114467>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [May 20, 2024, 6:14am UTC](https://discourse.julialang.org/t/how-to-let-the-user-specify-the-variable-name/114467 "2024-05-20T06:14:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [May 20, 2024, 6:14am UTC](https://discourse.julialang.org/t/how-to-let-the-user-specify-the-variable-name/114467/1 "2024-05-20T06:14:52Z")

</div>

For example, I want the user to specify the variable name of the JuMP mode. How to do?

```julia
using JuMP

function fun(var::Symbol)
    model = Model()
    @variable(model, var[1:3])
end

```

Thanks.

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [May 20, 2024, 6:39am UTC](https://discourse.julialang.org/t/how-to-let-the-user-specify-the-variable-name/114467/2 "2024-05-20T06:39:26Z")

</div>

With the `base_name` keyword argument

```julia
using JuMP

function fun(var::Symbol)
    model = Model()
    @variable(model, [1:3], base_name=var)
end

```

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [May 20, 2024, 6:41am UTC](https://discourse.julialang.org/t/how-to-let-the-user-specify-the-variable-name/114467/3 "2024-05-20T06:41:26Z")

</div>

Tkanks!!!

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [May 20, 2024, 6:44am UTC](https://discourse.julialang.org/t/how-to-let-the-user-specify-the-variable-name/114467/4 "2024-05-20T06:44:29Z")

</div>

I’ve tried this but failed.

```julia
function fun(var::Symbol)
    model = Model()
    @eval @variable(model, $(var)[1:3])
end

```

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [May 20, 2024, 6:50am UTC](https://discourse.julialang.org/t/how-to-let-the-user-specify-the-variable-name/114467/5 "2024-05-20T06:50:14Z")

</div>

> [@oxinabox](#):
>
> `@variable(model, [1:3], base_name=var)`

It seems that `var` will not be a registered variable name of the model?

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [May 20, 2024, 6:59am UTC](https://discourse.julialang.org/t/how-to-let-the-user-specify-the-variable-name/114467/6 "2024-05-20T06:59:31Z")

</div>

> [@oxinabox](#):
>
> `@variable(model, [1:3], base_name=var)`

I’ll fail to extract the variable of the model if so

```julia
using JuMP

function fun(var::Symbol)
    model = Model()
    @variable(model, [1:3], base_name=var)
end

var = :x
model = fun(var)
y = model[:x]
@constraint(model, y .>= 0)

```

---

<div class="post-metadata">

**Author:** ![sstroemer](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@sstroemer](https://discourse.julialang.org/u/sstroemer)\
**Post date:** [May 20, 2024, 7:11am UTC](https://discourse.julialang.org/t/how-to-let-the-user-specify-the-variable-name/114467/7 "2024-05-20T07:11:16Z")

</div>

You need to manually register the variable after setting the name (see the [JuMP docs](https://jump.dev/JuMP.jl/stable/manual/variables/#variable_names_and_bindings) on that), e.g. by using

```julia
using JuMP

function fun(var::Symbol)
    model = Model()
    model[var] = @variable(model, [1:3], base_name=var)
    return model
end

var = :x

model = fun(var)
@constraint(model, model[:x] .>= 0)

```

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [May 20, 2024, 7:19am UTC](https://discourse.julialang.org/t/how-to-let-the-user-specify-the-variable-name/114467/8 "2024-05-20T07:19:18Z")

</div>

I see!!! Nice! Didn’t quite understand before. Thanks!
