I'm using XeLaTeX and I want to do automatic repeating of plus sign after breakline. I've found the unoffical package for this but it seems to me a bit overkill (although didn't try it).

I tried to modify the example in Can (pdf)LaTeX recognize text strings and replace (expand) them?, I've just rewritten 'e's to the '+'s. This works as long as the macro is defined after \begin{document}, if I put it before it worked only for the letters (not for the plus).

I also tried a little different approach, I found here:

{\catcode`\+=13 \expandafter }\expandafter \newcommand\noexpand+{\mathplus\discretionary{}{+}{}}
\mathchardef\mathplus=\mathcode`\+  % + character in math mode
\mathcode`\+="8000  % set + active in math mode, otherwise inactive
\binoppenalty=10000 % do not break elsewhere

This is the same, it works only if I put it after the \begin{document} but I want it as a part of my package.

I'm translating it with XeLaTeX and packages: fontspec, amssymb, amsmath, xltxtra, hyperref, geometry and fancyhdr.

I'm really confused by those llcode or expandafter idioms, so I'll be glad for every piece of advice how to simply solve my problem.

2 Answers 2


First of all a little bit of theory: when TeX finds a character of category 11 or 12 in math mode, it looks at its mathcode; for example, the usual mathcode of + is "202B in hexadecimal form, which means

+ an operation symbol (first digit "2); the character must be taken from math family "0 (second digit); use the character in slot "2B (third and fourth digits).

However, if the mathcode is the special value "8000, TeX looks for a definition of that character as if it were active (catcode 13) and uses that in place of it. Plain TeX and LaTeX exploit this feature for ' in formulas such as $f''(x)$, with a clever definition of active '.

Therefore the problem is to provide such a definition without activating the character. Let's look at the presented code.

\mathchardef\mathplus=\mathcode`\+  % + character in math mode

This line just provides a command that is an alias for the usual +; note that one doesn't need to know the mathcode: that notation access it implicitly.

\mathcode`\+="8000  % set + active in math mode, otherwise inactive

This assigns the special mathcode to +.

\binoppenalty=10000 % do not break elsewhere

This tells TeX that no binary operation symbol is a good point for breaking a line, unless an explicit good break point is manually added: for example a penalty or a discretionary item.

{\catcode`\+=13 \expandafter }\expandafter 

The purpose of this code is to define the active + to expand to \mathplus\discretionary{}{+}{}. One cannot say

{\catcode`\+=13 \gdef+{\mathplus\discretionary{}{+}{}}}

because there would be an active + in the replacement text and this would lead to infinite recursion, because + would be replaced by its definition ad infinitum. Thus the + is activated in a group and, before the group is closed, the \noexpand+ is expanded via the two \expandafter commands. The expansion of \noexpand+ is just an active + that, however, cannot be expanded just now, because its ability to expand has been nullified. Then the group is closed, but the first + (not preceded by \noexpand any more) has already been tokenized and TeX sees


where the first + is active and the second one isn't. The purpose of \discretionary is twofold: first of all it inserts a plus sign with the correct mathcode that qualifies it as a binary operation and provides a break point; secondly, if a break is taken, after the break a + is inserted.

A perfectly equivalent definition is


because the \lowercase would put back into the main token list


with + of the same category code as ~ (that is, active). The \endgroup undoes the \lccode assignment. The second + has category 12 as usual.

However this definition is not as good as it might seem: try, for example


The \parbox with a very small width is just to force a break after the +; the + at the start of the second line will be from the italic font! Indeed the arguments of \discretionary are processed in horizontal mode, as if \mbox surrounded them (TeXbook, page 287). A more sensible definition would use


Since no break can be taken in subscripts or superscripts, this is quite safe. For the - it's another story.

Personal opinion. Some typographical traditions, notably in Russia, use the repetition of operation and relation symbols at breaks. This is a tradition that's worthy of being forgotten as it has no mathematical justification.

  • 1
    Nice answer! ("Nice answer!" doesn't have 15 characters so I had to add this). Commented Sep 16, 2011 at 1:26

I suppose that \begin{document} changes catcodes (does it?). In any case, changing catcodes might (will in your case) break compatibility with a lot of other packages.

Perhaps you are better off to define some sort of environment inside of which your changes are in effect. This has two effects: first, it avoids the problem of \begin{document} and second, you can still use other packages inside of the same document.

You could put your code into the respective \begin{<environment>} statement. The changes would last until \end{<environment>}.

  • 1
    \begin{document} does nothing with catcodes by itself; some packages (babel, for instance) force LaTeX to change some catcodes at \begin{document}. The purpose of mathcode "8000 is just to have the functionality of active characters without actually activating them.
    – egreg
    Commented Sep 16, 2011 at 0:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .