12

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:

image

while still using the breqn.sty package?

qgp07
  • 1,026
  • 1
    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 07 '11 at 01:21
  • I modified the question to make it not as narrow, hopefully that'll help my "luck" on this one. :-) – qgp07 Dec 07 '11 at 19:12

4 Answers4

10

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.)

  • 3
    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) – qgp07 Dec 07 '11 at 20:41
  • Good point about keeping catcode assignments as late as possible. – Bruno Le Floch Dec 07 '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? – qgp07 Dec 07 '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 07 '11 at 23:22
  • @Bruno: I didn't pry into the code of breqn, it seems quite complex. I wonder why it sets the catcode of _ and ^ at \begin{document}. It works fine if I use \AtBeginDocument{\catcode^=7\catcode\_=8} after \usepackage{breqn}, and then I can use \usepackage{tensor} before it. – Leo Liu Dec 08 '11 at 16:24
  • @LeoLiu: breqn loads mathstyle, and making sub and supscripts of catcode other allows to make them active in math mode, to add some code keeping track of the current style (that's not possible in plain TeX because of primitives such as \over, which change the style of previous stuff on the math list...). Now, tracking the math style is useful to avoid needing the expensive typesetting of \mathchoice. – Bruno Le Floch Dec 08 '11 at 17:00
  • 1
    @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 08 '11 at 17:14
  • @LeoLiu: it seems that \breqnpopcats is the culprit: it is trying to restore the catcode of _, but it picks up the "current value" at the wrong time. I think that \ExplSyntaxOn and \ExplSyntaxOff are doing their job correctly here, and simply removing \breqnpopcats altogether may fix it. Joseph pointed out to me in chat that the breqn doc has the relevant address to report bugs. – Bruno Le Floch Dec 08 '11 at 18:20
4

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}

enter image description here

Gonzalo Medina
  • 505,128
3

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}
Leo Liu
  • 77,365
2

You could just typeset it manually:

enter image description here

\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.

Werner
  • 603,163