# How to add unicode special character in Julia

**URL:** https://discourse.julialang.org/t/how-to-add-unicode-special-character-in-julia/71023
**Category:** General Usage
**Tags:** question, latex, unicode, gmt
**Created:** [November 5, 2021, 6:56pm UTC](https://discourse.julialang.org/t/how-to-add-unicode-special-character-in-julia/71023 "2021-11-05T18:56:56Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [November 5, 2021, 6:56pm UTC](https://discourse.julialang.org/t/how-to-add-unicode-special-character-in-julia/71023/1 "2021-11-05T18:56:56Z")

</div>

I’m able to find this list: [Unicode Input · The Julia Language](https://docs.julialang.org/en/v1/manual/unicode-input/).

In Matlab, if I want to draw something in a figure like “μmol”, all you need to do is to code it this way: “\mumol”.

I tried to do the same thing in Julia, but I keep getting the below error:  
`invalid escape sequence`

What am I missing?

---

<div class="post-metadata">

### Author: ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)
#### Post date: [November 5, 2021, 7:01pm UTC](https://discourse.julialang.org/t/how-to-add-unicode-special-character-in-julia/71023/2 "2021-11-05T19:01:38Z")

</div>

You will have to use [raw strings](https://docs.julialang.org/en/v1/manual/strings/#man-raw-string-literals).

---

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [November 5, 2021, 7:19pm UTC](https://discourse.julialang.org/t/how-to-add-unicode-special-character-in-julia/71023/3 "2021-11-05T19:19:53Z")

</div>

Many thanks! Sorry but I’m having difficulty understanding the instructions on the page. So I would need to wrap the special character code with raw"", and make it like the below:  
`raw"\mu"`?

Unfortunately, that does not work.

---

<div class="post-metadata">

### Author: ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)
#### Post date: [November 5, 2021, 7:22pm UTC](https://discourse.julialang.org/t/how-to-add-unicode-special-character-in-julia/71023/4 "2021-11-05T19:22:58Z")

</div>

Yes, that’s right. Within a `raw` string, the backslash is (mostly) ignored, so it won’t try to translate `\m` in `\mu` into something non-existing. Compare for an escape code that does exist, e.g. `"a\nb"` vs `raw"a\nb"`, the latter being equivalent to `"a\\nb"`

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [November 5, 2021, 7:29pm UTC](https://discourse.julialang.org/t/how-to-add-unicode-special-character-in-julia/71023/5 "2021-11-05T19:29:09Z")

</div>

I think you’re trying to make latex for plots.

[https://github.com/stevengj/LaTeXStrings.jl](https://github.com/stevengj/LaTeXStrings.jl)

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [November 5, 2021, 7:29pm UTC](https://discourse.julialang.org/t/how-to-add-unicode-special-character-in-julia/71023/6 "2021-11-05T19:29:17Z")

</div>

If you type `\mu` and then press TAB, this becomes `μ`. Maybe this is what you want to do, I’m not sure having a literal `\mumol` helps you anywhere.

---

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [November 5, 2021, 9:30pm UTC](https://discourse.julialang.org/t/how-to-add-unicode-special-character-in-julia/71023/7 "2021-11-05T21:30:08Z")

</div>

Many thanks! I just installed the LaTeXStrings package.

Could you please help me with a qustion?  
`L"\mu"` will give me the special character I want, but how do I concatenate it with other strings?

The below gives me something weird:  
`string("Variable (", L"\mu", "mol/kg)" )`

Output:  
`"Variable (\$\\mu\$mol/kg)"`

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [November 5, 2021, 10:19pm UTC](https://discourse.julialang.org/t/how-to-add-unicode-special-character-in-julia/71023/8 "2021-11-05T22:19:58Z")

</div>

Maybe try `join(a,b)`.

---

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [November 5, 2021, 10:50pm UTC](https://discourse.julialang.org/t/how-to-add-unicode-special-character-in-julia/71023/9 "2021-11-05T22:50:39Z")

</div>

Unfortunately, it does not work:

`a = L"\mu"`  
𝜇

`b = "mol"`  
“mol”

`C1 = join(a,b)`  
“$mol\molmmolumol$”

`C2 = string(a,b)`  
“$\mu$mol”

`C3 = a * b`  
“$\mu$mol”

`C4 = [a b]`  
1×2 Matrix{AbstractString}:  
L"\mu" “mol”

`C5 = hcat(a,b)`  
1×2 Matrix{AbstractString}:  
L"\mu" “mol”

---

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [November 6, 2021, 1:11am UTC](https://discourse.julialang.org/t/how-to-add-unicode-special-character-in-julia/71023/10 "2021-11-06T01:11:05Z")

</div>

Thanks to @stevengj, I’m able to display what I need, i.e., **𝜇mol** , on Juypter by doing this:  
`LaTeXString(a * b)`.

Unfortunately, when this string is sent to GMT to plot my colorbar label, it became this on my map: “Variable ($\mu$mol)”

Not sure if this is a Julia or GMT issue, but I certainly did not expect such a simple issue would become so complicated. Will it be possible to use the Unicode point # (e.g., U+003BC in this case) to get the desired results, i.e., 𝜇mol?

---

<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: [November 6, 2021, 12:55pm UTC](https://discourse.julialang.org/t/how-to-add-unicode-special-character-in-julia/71023/11 "2021-11-06T12:55:30Z")

</div>

> [@leon](#):
>
> Unfortunately, when this string is sent to GMT to plot my colorbar label, it became this on my map: “Variable ($\mu$mol)”

Presumably GMT doesn’t support LaTeX equations in plots. LaTeXStrings.jl doesn’t do any rendering — it is just a convenient way to type LaTeX equations in Julia strings without lots of escaping — and ultimately the plotting software (or the Jupyter notebook, in the case of cell output) is responsible for detecting that `$...$` is a LaTeX equation and sending it to a math renderer (e.g. the [MathTeXEngine](https://github.com/Kolaru/MathTeXEngine.jl) package or [MathJax](https://www.mathjax.org/)). Only a few plotting packages currently support this.

> [@leon](#):
>
> Will it be possible to use the Unicode point # (e.g., U+003BC in this case) to get the desired results, i.e., 𝜇mol?

Sure, have you tried just using `"µmol"` as your label string? (Note that you can easily type Unicode math symbols in the Julia REPL or Jupyter notebooks by using tab completion, e.g. type `"\mu`, hit the tab key, and then type `mol"`. You can also type `"\u03BCmol"` if you know the codepoint you want, but it’s a lot nicer to type, tab-complete, or cut-and-paste the Unicode directly — Julia has excellent Unicode support!)

This should work as long as your plotting software has fonts that include the Unicode characters you want (or supports fallback fonts for text rendering). However, GMT is apparently based on the old Postscript fonts and [may require some tweaking](https://docs.generic-mapping-tools.org/6.2/cookbook/postscript-fonts.html#character-encoding) to get it to render Unicode characters.

PS. Note that the “micro” symbol is technically distinct from the Greek character “mu” — micro in Unicode is [U+00B5](https://www.fileformat.info/info/unicode/char/00b5/index.htm), and it can be typed on a Mac keyboard by _option-m_. In many fonts, however, mu (`μ`) and micro (`µ`) are rendered with an identical (or nearly identical) glyph.

PPS. Note that `𝜇` is [U+1D707](https://www.fileformat.info/info/unicode/char/1d707/index.htm), italic mu, which is generally _not_ what you would want for a micro symbol, [even in LaTeX](https://tex.stackexchange.com/questions/522/best-way-to-typeset-micrometers).

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 6, 2021, 1:17pm UTC](https://discourse.julialang.org/t/how-to-add-unicode-special-character-in-julia/71023/12 "2021-11-06T13:17:11Z")

</div>

> [@stevengj](#):
>
> Presumably GMT doesn’t support LaTeX equations in plots

According to the master [GMT documentation](https://docs.generic-mapping-tools.org/6.2/cookbook/gmt-latex.html) (not GMT.jl) it should support LaTeX. Copying @joa-quim.

---

<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: [November 6, 2021, 1:19pm UTC](https://discourse.julialang.org/t/how-to-add-unicode-special-character-in-julia/71023/13 "2021-11-06T13:19:51Z")

</div>

> [@rafael.guerra](#):
>
> According to the master [GMT documentation](https://docs.generic-mapping-tools.org/6.2/cookbook/gmt-latex.html) (not GMT.jl) it should support LaTeX.

I see, but the syntax is different — it needs to be `"<math>\\mu mol</math>"`. Presumably GMT.jl could do this transformation for you if the label is a `LaTeXString`?

PS. Note also that it will only work if you have LaTeX installed, since it does the rendering with `latex` and `dvips`.

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [November 6, 2021, 2:07pm UTC](https://discourse.julialang.org/t/how-to-add-unicode-special-character-in-julia/71023/14 "2021-11-06T14:07:44Z")

</div>

Yes, GMT started to support LaTeX since last version (GMT6.2.0) but one need to have LaTeX installed. I had troubles with it on Windows when I refused to install the gigantic full LaTeX package and so never tried it nor tried to get some more julian syntax in GMT.jl. This is something I want o tackle someday but preferably it should be something similar to what Makie start doing also recently. Meanwhile to use LaTeX equations one must use the pure GMT syntax, _i.e._ pass text strings to the `frame` option or in the `text` module.

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [November 6, 2021, 2:12pm UTC](https://discourse.julialang.org/t/how-to-add-unicode-special-character-in-julia/71023/15 "2021-11-06T14:12:08Z")

</div>

And note lso that for simple expression involving Greek letters it’s possible to use solutions provided by PostScript only (that does not support UTF). See example fig  
[https://docs.generic-mapping-tools.org/dev/tutorial/session-2.html#id4](https://docs.generic-mapping-tools.org/dev/tutorial/session-2.html#id4)

---

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [November 7, 2021, 6:26pm UTC](https://discourse.julialang.org/t/how-to-add-unicode-special-character-in-julia/71023/16 "2021-11-07T18:26:07Z")

</div>

Many thanks for sharing! Great to know that I can directly type special characters into my Juno editor.

---

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [November 7, 2021, 8:32pm UTC](https://discourse.julialang.org/t/how-to-add-unicode-special-character-in-julia/71023/17 "2021-11-07T20:32:46Z")

</div>

Thanks again for the help @stevengj! Unfortunately, it did not work quite as expected.

Here is the unit in my Juno Editor:  
`(µmol kg⁻¹)`

Attached is what my color bar label looks like in my GMT mapping plot:  
 ![test2](https://global.discourse-cdn.com/julialang/original/3X/0/c/0c268654ab94a0555f7eff354bbba95d1705c90d.png)

---

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [November 8, 2021, 5:37pm UTC](https://discourse.julialang.org/t/how-to-add-unicode-special-character-in-julia/71023/18 "2021-11-08T17:37:44Z")

</div>

It turns out to be a GMT issue. I finally figured this out, with some help from the GMT experts:  
You want to use `@~` for special characters, `@+` for superscripts and `@-` for subscripts. In my example, it would be written this way: `(@~\155@~mol kg@+-1@+)` .

Here is the list of Octal codes:  
[https://docs.generic-mapping-tools.org/6.2/cookbook/octal-codes.html](https://docs.generic-mapping-tools.org/6.2/cookbook/octal-codes.html)
