# How to add unicode special character in Julia

I’m able to find this list: Unicode Input · The Julia Language.

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?

You will have to use raw strings.

1 Like

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.

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"

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

1 Like

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.

1 Like

Many thanks! I just installed the LaTeXStrings package.

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)"

Maybe try join(a,b).

1 Like

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”

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?

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 package or MathJax). Only a few plotting packages currently support this.

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 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, 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, italic mu, which is generally not what you would want for a micro symbol, even in LaTeX.

2 Likes

According to the master GMT documentation (not GMT.jl) it should support LaTeX. Copying @joa-quim.

1 Like

I see, but the syntax is different — it needs to be "$\\mu mol$". 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.

1 Like

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.

2 Likes

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

1 Like

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

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:

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

1 Like