# Error: The newtxmath and tensor packages with subscriptcorrection

Compiling the following Minimal Broken Example

\documentclass{article}
\usepackage{newtxtext}
\usepackage[subscriptcorrection]{newtxmath}
%\usepackage[lite,subscriptcorrection]{mtpro2}
\usepackage{tensor}
\begin{document}
Consider the following:
$A_f = R\indices{_\mu^{\sigma\nu}}$
\end{document}


leads to the following error messages:

! Package tensor Error: Sub/Superscript items out of order on input line 9,
(tensor)                some index tokens may now have been lost.

See the tensor package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9 A_f = R\indices{_\mu^{\sigma\nu}}

?


However, the code compiles perfectly fine if

• one uses \usepackage{newtxmath}, i.e., without subscriptcorrection; OR
• one uses \usepackage[lite]{mtpro2}; OR
• one uses \usepackage[lite,subscriptcorrection]{mtpro2}.

What is going on here? Is there a fix to this problem?

Added: I have noticed that both newtxmath and mtpro2 implement subscriptcorrection by making the underscore _ active (although it is more likely that newtxmath is inspired by mtpro2).

P.S. The working output if one uses \usepackage[lite,subscriptcorrection]{mtpro2}:

The tensor package uses a chain of \ifx comparisons to work out what type of index has been given. That works of course if the catcode of _ (and ^) when tensor is loaded is the same as when \indices is used. However, here the catcode of _ in the preamble is different from the document body: when using math-active _, it's catcode is set to 12 rather than 8. The easiest work-around is to load tensor with the correct catcode applied:

\documentclass{article}
\usepackage{newtxtext}
\usepackage[subscriptcorrection]{newtxmath}
\catcode\_=12 %
\usepackage{tensor}
\catcode\_=8 %
\begin{document}
Consider the following:
$A_f = R\indices{_\mu^{\sigma\nu}}$
\end{document}


Presumably the mtpro2 package sets the catcode of _ immediately: the newtxmath package delays until the start of the document.

A slightly fancier version which automates whether to set the catcode:

\documentclass{article}
\usepackage{newtxtext}
\usepackage[subscriptcorrection]{newtxmath}
\makeatletter
\edef\savedcodes{\catcode\noexpand\_=\the\catcode\_}
\edef\@tempa{\csname opt@newtxmath.sty\endcsname}
\def\@tempb{{subscriptcorrection}}
\expandafter\expandafter\expandafter
\in@\expandafter\@tempb\expandafter{\@tempa}
\ifin@
\catcode\_=12 %
\fi
\makeatother
\usepackage{tensor}
\savedcodes
\begin{document}
Consider the following:
$A_f = R\indices{_\mu^{\sigma\nu}}$
\end{document}

• If tensor used a set of \if tests, it would work in a catcode-agnostic way. Arguably it should as really the character is important here, not how TeX is interpreting it. – Joseph Wright Aug 21 '18 at 15:34
• Wow! I am floored by your updated answer. One quick final question before I accept your answer: Why is there a <space> behind each of the assignments \catcode\_=8 % (once) and \catcode\_=12 % (twice). Shouldn’t they be just \catcode\_=8% and \catcode\_=12% since this is no LaTeX3 syntax? – Ruixi Zhang Aug 21 '18 at 20:45
• @RuixiZhang No: TeX is looking for an optional space to terminate the number. I've deliberately given it one: best-practice. – Joseph Wright Aug 21 '18 at 20:50
• Thanks! And you are right about mtpro2’s approach. A simple test \typeout{\the\catcode\_} shows that _ changes to 8 or 12 immediately after mtpro2 with nosubcorr and subcorr, respectively. But _ remains 8 after newtxmath no matter nosubcorr or subcorr, and changes to 12 if subcorr due to \AtBeginDocument. Your solution works with any of these four situations. – Ruixi Zhang Aug 21 '18 at 21:31