The following two lines put at the document preamble should help:
\binoppenalty=10000
\relpenalty=10000
It is a good idea to add \begin{sloppy}...\end{sloppy}
to the paragraphs where mathematics appear, so that you allow the lines to be more stretchy. You can allow this stretch globally by putting \sloppy
into the preamble.
However, this will completely forbid implicit linebreaks inside inline mathematics. I don't think it is a good idea. Better idea would be to set the pelanties to some value that will discourage the breaks, but allow them if really necessary:
\binoppenalty=3000
\relpenalty=3000
With no warranty of any kind!
\documentclass{article}
\usepackage{color}
\makeatletter
\def\colorizemath #1#2{%
\expandafter\mathchardef\csname orig:math:#1\endcsname\mathcode`#1
\mathcode`#1="8000
\toks@\expandafter{\csname orig:math:#1\endcsname}%
\begingroup
\lccode`~=`#1
\lowercase{%
\endgroup
\edef~{{\noexpand\color{#2}\the\toks@}}}%
}
\@for\@tempa:=a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z\do{%
\expandafter\colorizemath\@tempa{green}}
\@for\@tempa:=A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z\do{%
\expandafter\colorizemath\@tempa{green}}
\@for\@tempa:=0,1,2,3,4,5,6,7,8,9\do{%
\expandafter\colorizemath\@tempa{red}}
\makeatother
\everymath{\color{blue}}
\everydisplay{\color{blue}}
\begin{document}\thispagestyle{empty}
Hello $world$. Do you know that $E=mc^2$?
\[ \widehat f(\omega) = \int_{-\infty}^\infty f(x) e^{-2\pi i \omega x}\,dx\]
\[ (I - M)^{-1} = \sum_{k=0}^\infty M^k\]
\end{document}
Let me add, with regards to \everymath
and \everydisplay
that it would have been better to do:
\everymath\expandafter{\the\everymath \color{blue}}
\everydisplay\expandafter{\the\everydisplay \color{blue}}
This preserves, rather than erases, the previously stored data in these token lists. (I just checked and Lamport's book does not have a single mention of token list
, and even the word token
is not to be found (it seems) in the entire book...). Admittedly, packages who put things in them should do that At Begin Document
so even the brutal way used in my initial code, as long as it is in the preamble, is maybe not that destructive. People interested in token lists can learn about it in, for example, TeX by Topic
by Victor Eijkhout (texdoc topic
).
Best Answer
the math analogues of
\kern
(for fixed spaces) and\hskip
(for stretchy space) are\mkern
and\mskip
which takemu
units where amu
is 1/18 emso you can do
but it is rather rare to use explicit space rather than use the named spaces such as
\,
which is\mkskip\thinmuskip
\thinmuskip
isn't a fixed length, it is given an initial value ofbut if you are using a math font other than computer modern a slightly larger or smaller amount may be more suitable, so a global setting such as
would give tighter spacing