# Variable in legends

**URL:** <https://discourse.julialang.org/t/variable-in-legends/716>\
**Category:** Visualization\
**Tags:** plotting, pyplot\
**Created:** [December 3, 2016, 11:41pm UTC](https://discourse.julialang.org/t/variable-in-legends/716 "2016-12-03T23:41:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 3, 2016, 11:41pm UTC](https://discourse.julialang.org/t/variable-in-legends/716/1 "2016-12-03T23:41:07Z")

</div>

I’m trying to add legends using Latex as follows:

```julia
using PyPlot

m = 100
xx = collect(linspace(-1.0, 1.0, 100))
yy = sin(m*xx)
zz = collect(linspace(-5, 3.0, 100))

figure(1)
clf()

h1, h2 = plot(xx, yy, "b.", xx, zz, "r+")
legend((h1, h2), (L"$\sin( $m x)$", L"Linear"))

```

I’m trying to print the value of m in the legend, but don’t know how to and can’t find any examples. Could anyone give me a hint?

---

<div class="post-metadata">

**Author:** ![tbreloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbreloff/32/68_2.png) [@tbreloff](https://discourse.julialang.org/u/tbreloff)\
**Post date:** [December 3, 2016, 11:54pm UTC](https://discourse.julialang.org/t/variable-in-legends/716/2 "2016-12-03T23:54:02Z")

</div>

I don’t think you need the outside “$”?

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 4, 2016, 10:36am UTC](https://discourse.julialang.org/t/variable-in-legends/716/3 "2016-12-04T10:36:38Z")

</div>

It doesn’t work. I believe the outside “$” must not be absent. Thanks a lot though. Any other hints?

---

<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:** [December 4, 2016, 11:45am UTC](https://discourse.julialang.org/t/variable-in-legends/716/4 "2016-12-04T11:45:27Z")

</div>

If you use `L"..."` to make a LaTeX string, then you can’t use `$` interpolation in the strings. (Technically, `$` interpolation doesn’t occur in Julia string macros.) This is intentional, because `$` has its own meaning in LaTeX.

To do interpolation, just use an ordinary Julia string. This means you have to escape characters like `$` and `\` if you want to use them for LaTeX.

```julia
legend((h1, h2), ("\$\\sin( $m x)\$", "Linear"))

```

(Note also that you wouldn’t want to use `L"Linear"` for a text label like _Linear_, because `L"..."` implicitly puts the contents into an equation.)

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 4, 2016, 1:29pm UTC](https://discourse.julialang.org/t/variable-in-legends/716/5 "2016-12-04T13:29:33Z")

</div>

Ah, I see! Thanks a lot! Now everything is right for me.

---

<div class="post-metadata">

**Author:** ![Daneel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daneel/32/175_2.png) [@Daneel](https://discourse.julialang.org/u/Daneel)\
**Post date:** [December 5, 2016, 11:49am UTC](https://discourse.julialang.org/t/variable-in-legends/716/6 "2016-12-05T11:49:24Z")

</div>

Another option:

`latexstring("\$\\sin(" * string(m) * "x)\$")`

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 5, 2016, 11:54am UTC](https://discourse.julialang.org/t/variable-in-legends/716/7 "2016-12-05T11:54:13Z")

</div>

Thanks for the comments! It’s always good to know an alternative. Can you remind me what is \* for?

---

<div class="post-metadata">

**Author:** ![Daneel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daneel/32/175_2.png) [@Daneel](https://discourse.julialang.org/u/Daneel)\
**Post date:** [December 5, 2016, 11:56am UTC](https://discourse.julialang.org/t/variable-in-legends/716/8 "2016-12-05T11:56:21Z")

</div>

It combines two normal strings.

```julia
x = "Hello"
y = "World"
z = x * " " * y # First string, a space, and the second string
"Hello World"

```

See [\*(s,t)](http://docs.julialang.org/en/release-0.5/stdlib/strings/#Base.*)

Edit: Added a documentation link

---

<div class="post-metadata">

**Author:** ![chobbes](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@chobbes](https://discourse.julialang.org/u/chobbes)\
**Post date:** [December 5, 2016, 1:09pm UTC](https://discourse.julialang.org/t/variable-in-legends/716/9 "2016-12-05T13:09:20Z")

</div>

Thanks a lot!! I remember now.
