# String subscript given as integer

**URL:** <https://discourse.julialang.org/t/string-subscript-given-as-integer/17814>\
**Category:** General Usage\
**Tags:** strings\
**Created:** [November 21, 2018, 2:24pm UTC](https://discourse.julialang.org/t/string-subscript-given-as-integer/17814 "2018-11-21T14:24:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mforets](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mforets/32/298_2.png) [@mforets](https://discourse.julialang.org/u/mforets)\
**Post date:** [November 21, 2018, 2:24pm UTC](https://discourse.julialang.org/t/string-subscript-given-as-integer/17814/1 "2018-11-21T14:24:02Z")

</div>

I’d like to write a function `f(n::Int)::String` such that `f(1) = "x₁"`, `f(2) = "x₂"` and so on (assume `1<=n<=9` for simplicity). Since

```julia
julia> 'x' * '\u2081'
"x₁"

```

Let `n=3`; the syntax `'x' * '\u' * string(n + 2080)` is invalid: `ERROR: syntax: invalid escape sequence`. The backslash can be escaped with “raw”, but how can i get the subscript back?

```julia
julia> 'x' * raw"\u" * "$(n + 2080)"
"x\\u2083"

```

---

<div class="post-metadata">

**Author:** ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)\
**Post date:** [November 21, 2018, 2:40pm UTC](https://discourse.julialang.org/t/string-subscript-given-as-integer/17814/2 "2018-11-21T14:40:11Z")

</div>

This works

```julia
julia> foo(i) = "x" * Char(0x2080 + i)
foo (generic function with 1 method)

julia> foo(1)
"x₁"

julia> foo(2)
"x₂"

julia> foo(3)
"x₃"

```

---

<div class="post-metadata">

**Author:** ![mforets](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mforets/32/298_2.png) [@mforets](https://discourse.julialang.org/u/mforets)\
**Post date:** [November 21, 2018, 2:41pm UTC](https://discourse.julialang.org/t/string-subscript-given-as-integer/17814/3 "2018-11-21T14:41:29Z")

</div>

Great, thanks!

---

<div class="post-metadata">

**Author:** ![adam-srut](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adam-srut/32/32514_2.png) [@adam-srut](https://discourse.julialang.org/u/adam-srut)\
**Post date:** [January 7, 2022, 1:46pm UTC](https://discourse.julialang.org/t/string-subscript-given-as-integer/17814/4 "2022-01-07T13:46:08Z")

</div>

Do you also have a solution for values greater than 9? i.e.:

```julia
julia> foo(12)
"x₁₂"

```

---

<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:** [January 7, 2022, 1:52pm UTC](https://discourse.julialang.org/t/string-subscript-given-as-integer/17814/5 "2022-01-07T13:52:39Z")

</div>

> [@adam-srut](#):
>
> Do you also have a solution for values greater than 9? i.e.:

```julia
subscript(i) = join(Char(0x2080 + d) for d in reverse!(digits(i)))

```

---

<div class="post-metadata">

**Author:** ![adam-srut](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adam-srut/32/32514_2.png) [@adam-srut](https://discourse.julialang.org/u/adam-srut)\
**Post date:** [January 7, 2022, 1:56pm UTC](https://discourse.julialang.org/t/string-subscript-given-as-integer/17814/6 "2022-01-07T13:56:05Z")

</div>

Thanks! I thought about that. However, I do not like the small spaces between the digits in the subscript.

```julia
julia> "x"*subscript(123)
"x₁₂₃"

```

Is there any way to get rid of it? Except for using the LaTeXStrings?

---

<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:** [January 7, 2022, 2:12pm UTC](https://discourse.julialang.org/t/string-subscript-given-as-integer/17814/7 "2022-01-07T14:12:15Z")

</div>

> [@adam-srut](#):
>
> Is there any way to get rid of it?

Don’t use a monospaced font or use a text-rendering engine? Julia has no control over text-rendering of Unicode strings in a terminal.

> Except for using the LaTeXStrings?

LaTeXStrings doesn’t actually do any text rendering either. It relys on you having an environtment (e.g. Jupyter) that supports LaTeX rendering. (In Jupyter, you could also use HTML subscripts with e.g. `Docs.HTML("<sub>123</sub>"))`.)
