Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing variable subscripts in Julia

Tags:

julia

Hey so I know that due to the Unicode support in Julia one, may write for instance the letter a with the subscript 1 by typing a\_1<TAB>. Now, what if I wanted to do something like the following:

for i in [1 2 3]
    println("a\_i")
end

and have the output be written as

a₁
a₂
a₃

How would I go about this without writing out all the possible subscripts myself?

like image 819
Mason Avatar asked Dec 21 '25 14:12

Mason


2 Answers

You could do this (at least in version 0.6):

ltx = Base.REPLCompletions.latex_symbols

for i in 1:3 
    println("a$(ltx["\\_$i"])") 
end
like image 184
Liso Avatar answered Dec 24 '25 11:12

Liso


Bogumił Kamiński's answer seems the neatest, but I needed to reverse the order to get the correct string for two-digit numbers:

subscript(i::Integer) = i<0 ? error("$i is negative") : join('₀'+d for d in reverse(digits(i)))

for i=7:13 println("a"*subscript(i)) end
like image 42
improbable Avatar answered Dec 24 '25 11:12

improbable



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!