# Multiple tensor indices with breqn package

When I try to use the tensor style file to create multiple tensor indices with the breqn style file, I run into the following problem shown in this (minimal) code:

\documentclass{article}
\usepackage{breqn}
\usepackage{tensor}
\begin{document}
$\tensor{F}{_a}$
\end{document}


I get the following error:

ERROR: Package tensor Error: Sub/Superscript items out of order on input line 5,

--- TeX said ---
(tensor)                some index tokens may now have been lost.

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

l.5 $\tensor{F}{_a}$
--- HELP ---
From the .log file...

An index string has an extra or missing ^' or _' token.


I suspect this has to do with how breqn.sty redefines ^ and _ as the user egreg talked about here. If I take out the breqn package, everything compiles nicely.

My question is: Is there something I can put in the preamble or elsewhere to return functionality to the \tensor command while still using the breqn package?

OR

Is there something I can use that is not the tensor.sty style file to create tensors like \tensor[^{ab}_c]{F}{_{def}^g_h} which would produce something that looks like this:

while still using the breqn.sty package?

-
I have a feeling that you may be out of luck on this one. You may want to send an email to the maintainer of the breqn package and ask him for advice. – Mico Dec 7 '11 at 1:21
I modified the question to make it not as narrow, hopefully that'll help my "luck" on this one. :-) – thequark Dec 7 '11 at 19:12

The breqn package plays around with category codes of the subscript and superscript characters, but it only sets them at the \begin{document}. What we need is to set them earlier, before \usepackage{tensor}. For instance, the following appears to work.

\documentclass{article}
\usepackage{breqn}
\catcode_=12
\catcode^=12
\usepackage{tensor}
\begin{document}
$\tensor[^{ab}_c]{F}{_{def}^g_h}$
\end{document}


Also, breqn seems to have a bug: it leaves _ with category code 11 rather than 8 or 12. (But you don't need to worry about that.)

-
Given my previous question, I should have been able to figure this out. Thanks, @bruno. My only comment would be what you've all but said explicitly: To make sure the \catcode_=12 \catcode^=12 \usepackage{tensor} part is as close to the \begin{document} as possible since doing this can mess up other packages (personally, I ran into problems with the verbatim.sty package being called after the \catcode's) – thequark Dec 7 '11 at 20:41
Good point about keeping catcode assignments as late as possible. – Bruno Le Floch Dec 7 '11 at 20:48
Better yet, @bruno, would it be OK to just call \catcode_=12 \catcode^=12 \usepackage{tensor} \catcode_=8 \catcode^=7 so that packages after the tensor package will still try to function normally? Or is there something wrong with this? – thequark Dec 7 '11 at 22:18
@thequark: the only drawback to what you propose is that it introduces more code. Otherwise, no problem. – Bruno Le Floch Dec 7 '11 at 23:22
@Bruno: OK. For compatibility with other package, I would rather use mathstyleoff option even it is more expensive. But the catcode bug for _ of breqn should still be fixed. I'll send a email to Morten Høgholm. – Leo Liu Dec 8 '11 at 17:14

Perhaps you could use the tensind package:

\documentclass{article}
\usepackage{breqn}
\usepackage{tensind}

\tensordelimiter{?}

\begin{document}

$?{}^{ab}_c?$$?F_{def}^{}^g_h?$

\begin{dmath}
f(x)=\frac{1}{x} \condition*{x\neq 0}.
\end{dmath}

\end{document}


-

You could just typeset it manually:

\documentclass{article}
\usepackage{breqn}% http://ctan.org/pkg/breqn
\begin{document}
\begin{dmath*} \mathstrut^{ab}\mathstrut_c F\mathstrut_{def}\mathstrut^g\mathstrut_h \end{dmath*}
\end{document}


\mathstrut stretches to a required vertical length with zero horizontal width. Of course, this takes away from the readability of the code. But this depends to what extent you formerly used tensor in your document.

-

It is mathstyle package (included by flexisym, and flexisym is included by breqn) which messes the catcodes of _ and ^. You can use (undocumented) mathstyleoff option to turn it off. It seems that mathstyle package does nothing to do with the main purpose of breqn, but I'm not sure.

Since you use [mathstyleoff] option of breqn, you can use \usepackage{tensor} before or after breqn package.

But as Bruno said, it is a bug that the catcode of _ remains 11 after breqn, since expl3 and \ProvidesExplPackage makes _ to be a letter. Therefore you should use \catcode\_=8 after the package.

This is a working example:

\documentclass{article}

\usepackage{tensor}
\usepackage[mathstyleoff]{breqn}
%\catcode\_=8 % use it for safey

\begin{document}

$\tensor[^{ab}_c]{F}{_{def}^g_h}$

\begin{dmath}
a+b=c=d
\end{dmath}

\end{document}

-