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
\newcommand\noexpand+{\mathplus\discretionary{}{+}{}}
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
\newcommand+{\mathplus\discretionary{}{+}{}}
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
\begingroup\lccode`~=`+
\lowercase{\endgroup\def~}{\mathplus\discretionary{}{+}{}}
because the \lowercase
would put back into the main token list
\endgroup\def+
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
\parbox{1pt}{\itshape$a+b$}
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
\discretionary{}{\the\textfont0+}{}
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.