Indexing within double quotation mark

Hi community,
Julia is amazing in “$a”. One step further, are there any way to achieve this?

plot(…,label=[“a1 is $a[:,1], a2 is $a[:,2].”])

Or other command like ‘legend!(a,b)’ to define which parameters will be specified in legend?

1 Like

It could be done by enclosing the expression with parentheses, e.g. "a1 is $(a[:,1])".

3 Likes

Thank you. It works.
plot(c,label=["a1= $(a[:,1])" , " a2=$(a[:,2])"])

1 Like

An extra addition for getting >=1 parameters presented for each series in legend.
As "$(a[1,2])" cannot function with a commas betwen parameters included like "a1=$(a[1,2]), a2=$(a[2,2])" , """ """ would nail it, which is:

scatter(a,simu_mtx[a,:], label=["""s0=$(b[1,1])", "2Nμ=$(b[1,2])""" , """s0=$(b[2,1])", "2Nμ=$(b[2,2])"""])

One following question:
Compared to specify the legend by listing, I tried to use for loop.

legend=String[]
for i in 1:2, j in 1:2
  push!(legend,"""s0=$(ndx_mtx[j,i])", "2Nμ=$(ndx_mtx[j,i])""")
end
println(legend)

#on REPL print
["s0=0.05 \",\"2Nμ=0.05 ", "s0=1.0 \",\"2Nμ=1.0 ", "s0=0.005 \",\"2Nμ=0.005 ", "s0=1.0 \",\"2Nμ=1.0 "]

The \ in printed output is unwanted. Is there a way to leave it while keeping "s0=0.05 \",\"2Nμ=0.05 " as one element pushed to legend string vector?

The \ is an escape character for the single quotes. Try adding a dot after the println command, i.e. println.(legend), and you should see that there are single quotes without backslashes.

It wasn’t clear to my why triple quotes (""") were being used. I think that the line in the loop could be:

push!(legend,"s0=$(ndx_mtx[j,i]), 2Nμ=$(ndx_mtx[j,i])")

and that should work.

1 Like

It works now :slight_smile: Thank you.

The program demands a to be defined

I would like tu know how I could introduce Latex symbols inside the label section while indexing.

Hi @Alex_Manners and welcome to the forum :slight_smile:

Please consider opening a new topic – it makes it easier to find and answer your question.

But on the topic: You could try to type the characters directly, e.g. in the way described here:
https://docs.julialang.org/en/v1/manual/unicode-input/

Or have a look at this package if you need full LaTeX support: