# Weird DeclareUnicodeCharacter / siunitx clash

I love declaring unicode characters to write readable formulas. However, I just ran into a strange issue. The following works as expected, with the “×” sign being replaced with the chosen code (“plop”):

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\DeclareUnicodeCharacter{00D7}{plop}

\begin{document}
×
\end{document}


However, if I add the siunitx package, which seems completely unrelated, I get an error stating that \texttimes is undefined:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{siunitx}

\DeclareUnicodeCharacter{00D7}{plop}

\begin{document}
×
\end{document}


It then gets even stranger: if I add the even-more-unrelated package xfrac (the order does not seem to matter), it works again:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{siunitx}
\usepackage{xfrac}

\DeclareUnicodeCharacter{00D7}{plop}

\begin{document}
×
\end{document}


Sure, this could be considered as a solution, but I cannot feel satisfied with something that I don't understand at all. Also, note that with certain combinations of packages that I have yet to pin down, the error turns to “Command \texttimes unavailable in encoding T1.” instead. Furthermore, this does not seem to be limited to “×” at all. The following code:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{siunitx}
%\usepackage{xfrac}

\DeclareUnicodeCharacter{00B2}{\ensuremath{^2}}

\begin{document}
$x²$
\end{document}


gives an “Undefined control sequence.” error for \texttwosuperior unless siunitx is removed or xfrac is added. Note that the same thing seems to occur when using the newunicodechar package to declare the characters instead.

What's wrong with the considered code when using siunitx but not xfrac?

• Very compliments for your question.+1 – Sebastiano Oct 26 '17 at 19:58
• Very compliments for your question.+1 – Sebastiano Oct 26 '17 at 19:58

Internally, siunitx needs to set up some minimal character support from the TS1 range. This is done such that loading textcomp is not forced:

\DeclareFontEncoding{TS1}{}{}
\DeclareFontSubstitution{TS1}{cmr}{m}{n}


followed by a number of declarations for specific symbols. However, this will lead to LaTeX looking for approach TS1 symbols for other characters made active before it was set up. Thus there is a timing issue: you've made the character active before the 'minimal' TS1 support is set up. (The minimal support in siunitx only defines such symbols as are absolutely required.)

You can fix this most easily by loading textcomp, or by altering the definition of the custom chars after loading siunitx. (The xfrac package loads textcomp, which is why it solves the problem.)

• Wow, thanks. I'm not sure I get what you mean by “altering the definition of the custom chars” though. And I never had to explicitly include textcomp so I don't even know what it was written for, haha. texdoc just points me towards encguide.pdf and source2e.pdf. – Alice M. Oct 26 '17 at 11:58
• It is no exactly clear what happens really because the OP's \DeclareUnicodeCharacter usage was after loading siunitx. – user4686 Oct 26 '17 at 12:46
• @jfbu siunitx setups the encoding at begin document. And then the code from inputenc/utf8.def loads ts1enc.dfu also at begin document. As ts1enc.dfu contains \DeclareUnicodeCharacter{00D7}{\texttimes} the other definition is overwritten. (But you are right, loading siunitx early doesn't work, one need to load textcomp). – Ulrike Fischer Oct 26 '17 at 13:40
• @UlrikeFischer then perhaps simply \AtBeginDocument{\DeclareUnicodeCharacter{00B2}{\ensuremath{^2}}} after loading siunitx ? (or same with \DeclareUnicodeCharacter{00D7}{plop}) (does work in my brief testing) – user4686 Oct 26 '17 at 13:46
• (don't copy paste from my comment, the site adds U+200B and U+200C invisible chars -- really irritating) – user4686 Oct 26 '17 at 13:53