# Using the unicode prime character outside math mode with unicode-math and newunicodechar

I am trying to use unicode-math, newunicodechar and LuaLaTeX/XeLaTeX to write my LaTeX code making use of unicode characters in the source text.

This works fine, except for the prime character; if I try to use \newunicodechar for it, it introduces errors if the prime is outside math mode.

\documentclass{article}

\usepackage{unicode-math}
\setmathfont{XITS Math}
\usepackage{newunicodechar}
\newunicodechar{′}{any replacement character}

\begin{document}
This prime causes “Missing \$inserted”: ′ \end{document}  Unfortunately, I can't restrict my use of the prime character to just math mode; I am writing literate Agda and wish to use the prime character in my code, and the LaTeX source produced by Agda is not in math mode. Is there any workaround for this problem? As it stands, my only solutions seem to be avoiding prime in my code or running a script to replace instances of prime in the Agda generated LaTeX source. • unicode-math redefines ′ at begin document. Do \AtBeginDocument{\newunicodechar{′}{any replacement character}} if you want to override it. – egreg Apr 22 at 21:59 • What are you hoping to use the prime symbol for in your source document? If you want a "text" meaning for it, I could add a hook into unicode-math for you to define one. If you want a "math" meaning for it, either (a) unicode-math should provide something appropriate and if it doesn't maybe it should, OR (b) since it already has pretty clear semantics in a maths context, I'd suggest it shouldn't be changed. – Will Robertson Apr 22 at 23:51 • @WillRobertson I use it for names of related entities in my Agda code; for instance, I might have two arguments to a function named x and x′. So it is a "math" meaning, but I am not using it in math mode, because the Agda LaTeX backend does not place it (or any of my code) in math mode. – Mark Armstrong Apr 23 at 1:42 • why don't you use for your code a font that has U+2032? – Ulrike Fischer Apr 23 at 7:47 ## 2 Answers Note that (as mentioned by Ulrike in the comments) if you just want to typeset a prime outside of maths mode all you need to do is load a font that contains that glyph: \documentclass{article} \usepackage{unicode-math} \setmainfont{texgyrepagella-regular.otf} \begin{document} This prime: ′ \end{document}  • This seems to be the best solution. Specifically, my Agda code snippets are actually using the sans font, so \setsansfont{Free Sans} solves the issue. – Mark Armstrong Apr 24 at 21:24 As @egreg pointed out, unicode-math redefines the prime character at begin document, so wrapping the \newunicodechar invocation in \AtBeginDocument solves the issue. An important note is that while I usually use the form \newunicodechar{α}{\ensuremath{\mathrm{α}}}  for defining unicode characters, doing so with prime causes a crash. So make sure to use a single quote instead. \documentclass{article} \usepackage{unicode-math} \setmathfont{XITS Math} \usepackage{newunicodechar} \AtBeginDocument{\newunicodechar{′}{\ensuremath{\mathrm{'}}}} \AtBeginDocument{\newunicodechar{″}{\ensuremath{\mathrm{''}}}} \begin{document} Now I can write a prime: A′. Double prime had the same problem, and is resolved the same way: A″. \end{document}  • Beware that now you cannot use ′ in math mode. – egreg Apr 22 at 22:12 • It seems to still work in math mode. One thing I can't do is use \newunicodechar to define prime using prime. – Mark Armstrong Apr 22 at 22:27 • Things like $A′′′′\$ will no longer behave as expected. Prime collapsing has been lost. – Henri Menke Apr 22 at 22:43
• Right; I see it now. Still an improvement for my purposes, but it is unfortunate to lose that. – Mark Armstrong Apr 22 at 22:52