10

Short question:

Is there some font encoding or other trick that would allow the unicode character ² (exponent 2) to be correctly interpreted by LaTeX, notably in math mode (i.e. translate it automatically into ^{2})?

(and likewise for ³, ⁴, ...)

Detailed rationale:

Normally, to introduce an exponent in math mode one uses the ^ symbol, as in x^2 or e^{i\pi}. I've been using LaTeX for decades and this feels very natural. However, with my current setup, when I type the characters x ^ 2 on the keyboard, it results in the two unicode characters .

I do have a \RequirePackage[utf8]{inputenc} around the beginning of my personal style file, and Unicode characters in general are interpreted correctly (accented characters and such).

Usually I think about it and do type x ^ ^ 2 which results in x^2, but often I'm typing fast and later have to go back to every line of code that produces a LaTeX Error: Command \texttwosuperior unavailable in en error. This is annoying.

I could implement a workaround at the level of my editor (I use vim, so it would be simple to add a mapping to convert the ² into ^2), but I'm wondering if there's a better editor-independent way to handle this.

13

ConTeXt does it right.

\starttext

x²³ and $x²³$

\stoptext

enter image description here


The same can be achieved with LaTeX and unicode-math.

\documentclass{article}
\usepackage{unicode-math}
\begin{document}
x²³ and $x²³$
\end{document}

In pdfLaTeX you have to use newunicodechar to redefine ² and ³ to make them math-mode aware.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{textcomp} % for \text...superior
\usepackage{newunicodechar}
\newunicodechar{²}{\ifmmode{}^2\else\texttwosuperior\fi}
\newunicodechar{³}{\ifmmode{}^3\else\textthreesuperior\fi}
\begin{document}
x²³ and $x²³$
\end{document}

enter image description here

  • The first LaTeX variant you suggest produces an error message with pdflatex. I was about to comment on that when I saw your edit and 3rd variant. That one is indeed working with pdflatex. Nice workaround! – Dalker Dec 15 '16 at 10:51
  • 3
    @Dalker Of course, the second variant produces an error with pdflatex because it is not a Unicode engine. Long time I was also an advocate of pdflatex (mainly for performance reasons) but not using a Unicode engine in 2016 is just not reasonable. I therefore use lualatex. – Henri Menke Dec 15 '16 at 12:32
  • @Dalker If you like my answer and it was helpful, please consider upvoting (by clicking on the arrows next to the score) and marking it as accepted answer (by clicking on the checkmark ✓). – Henri Menke Dec 15 '16 at 13:55
  • Don't worry, @Henri Menke, I did upvote your answer earlier. As for accepting, I have a general policy in StackExchange sites to wait ~24 hours in case someone from a different time zone wants to contribute a different answer. Regarding lualatex, I guess I should take a thorough look at it sometime. Haven't done so yet mostly for lack of time, as well as to remain 100% compatible with colleagues who use pdflatex. But you have given me arguments to try it soonish. – Dalker Dec 15 '16 at 15:35
  • Did you try $\Big|²³$? Actually a single exponent suffices for seeing that your \ifmmode trick is wrong, because it sets an exponent to an empty formula. – egreg Jan 6 '17 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.