Unicode for mathematics symbols in LaTeX

Håkon Robbestad Gylterud

We know how to input all the fancy Unicode mathematics symbols, but a simple text file in markdown still does not have nearly the sophistication of a Latex document — and for the most part that is a good thing. When complexity is not needed, it is nice to keep it at a safe distance. Sometimes, however, you really need to typeset some complicated equation; with sums indexing over uncountable sets and beautiful fractions whose denominator tend to zero. Or maybe you have a killer, commutative diagram which you have to typeset in xypic?

In any case you might happen to be typing and by new habit you happen to type the unicode character ℝ in stead of \mathbb{R}. What happens next is that your latex compiler starts yelling at you.

! Package inputenc Error: Unicode char \u8:ℝ not set up for use with LaTeX.

Why is this? You dutyfully had \usepackage[utf8]{inputenc}. Is that not enough? Clearly, not.

Turns out, this is a call for action. If you want to use fancy UTF-8 symbols in Latex, you must configure how they are interpreted. This is a good thing, because you can let your own personal preferences decide how it should work.

WikiBooks helpfully explains how this is done. All you need to do is to declare each symbol you want to use at the top of your document. The declations look like this:

\DeclareUnicodeCharacter{211D}{\ensuremath{\mathbb{R}}}

The 211D is the Unicode code point of ℝ.

Notice that we use \ensuremath{} in order to make sure that the symbol is used in math mode. This allows us to type a naked ℝ in the middle of the text without surrounding it with dollar signs.

What? Is your preamble already too crowded to find your \author declaration? Then better put all the symbol declarations in a .sty file and include it as a package.

\usepackage{utf8-symbols}

To make life easier for myself, I wrote this little ten-line rc-script to add new entries in such a sty-file. Just run it, type your symbol and the associated Latex command, and it will generate an entry as described in the wikibook. The script uses the XCompose file from this article to look up the code of the symbol you typed, so make sure to have it before running the script.

You can download my current utf8-symbols.sty if you want a starting point for your own.

Here is an example latex file which uses it:

\documentclass[a4paper,12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage{utf8-symbols}

\usepackage{amsmath}
\usepackage{amssymb}

\title{Lorem ip-∑}

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor, π, incididunt ut labore $2^n = ∑_{0 ≤ i < n} 2^i + 1 $ et dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur $0 = 1 + e^{π i}$. Excepteur sint occaecat 
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim
id est laborum.

\begin{align}
 &f : ℝ → ℝ² \\
 &f(x) ≔ (x² , \lim_{a → x⁺} e^{-\frac 1 a})
\end{align}

\end{document}

Reading it still takes a bit of Latex knowledge. However, it makes it a bit easier to skim a formula.

Exercise: Write a script that reads the assignments in utf8-symbols.sty and automatically replaces the latex commands with the corresponding Unicode symbols. This is useful for updating old files.


Expecting a comment section? Feel free to e-mail me your comments, or otherwise contact me to discuss the content of this site. See my contact info. You can also write your opinion on your own website, and link back here! ☺