Forcing math mode to be on the same line

The usual way to typeset a function is

$f\colon A \to B$

(\to is the same as \rightarrow, but shorter to type). You'll see a different spacing: no space before and a space after the colon. This will also inhibit a line break after the colon. If you really don't want a line break after the arrow, then

$f\colon A \to\nolinebreak B$

will do. You can of course define a macro for this:

\newcommand{\function}[3]{#1\colon #2 \to\nolinebreak #3}

to be called as

$\function{f}{A}{B}$

but I believe this is unnecessary. Just check the line during the final revision, adding \nolinebreak where really needed.


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