# Forcing math mode to be on the same line

I would like to have math mode expressions in dollar signs always to stay on the same line, which is what happens to a sequence of characters with no spaces while not in math mode. Is there a way to avoid having to do this manually by adding line breaks every time (which is terrible style, and gets very messy if one has to go back and change things, altering everithing that comes after)?

-
For regular inline math that you want to never break across the line boundary, you could use \mbox{$...$}. Making this a rule seems a bit harsh, since longer expressions are typically allowed to break around binary and relational operators, which looks okay. – Werner Jan 4 '13 at 19:42
@Werner Thanks, I only plan to use it for short expressions like $f: M \rightarrow \mathbb{R}$ which got broken after the ":". Your \mbox{$...$} solves this by making the whole expression stay on the first line, but that isn't exactly what I was after: I would like everything inside the dollar signs to be treated as it it were a sequence of characters with no spaces, so if it all doesn't fit on the first line, I would like it to be typeset on the line below that, adjusting spacing accordingly. Is this possible, maybe with a command in the preamble? – Emilio Ferrucci Jan 4 '13 at 19:50
Check whether the first answer here solves your problem. – Scott H. Jan 4 '13 at 20:12
@ScottH. Thanks for your comment, I hadn't seen that question. However it seems as though using braces has more or less the same outcome as mbox: it just forces the stuff between the dollar signs to stay on the initial line, not to be treated as "unbreakable". – Emilio Ferrucci Jan 4 '13 at 20:22
The braces do indeed, I was more checking whether the \sloppy portion of the answer gave the intended result (i.e. preventing the overfull box). I should have been more explicit about that. – Scott H. Jan 4 '13 at 20:23

## 2 Answers

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

-
Thanks, your recommended preamble commands combined with the sloppy command look like a good solution. – Emilio Ferrucci Jan 4 '13 at 20:36

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.

-
Thanks for your answer. As you predicted, by using \colon I got a line break after the arrow, which in my opinion doesn't look good either. I'm a bit reluctant to add \nolinebreaks in the final revision, because I might always go back and change something, which would change everything... There must be a standard way to do this though: if getting line breaks that don't look good is nevertheless considered standard, I have no problem adopting it. Is it standard? – Emilio Ferrucci Jan 4 '13 at 20:29
I'm never very worried about this kind of breaks, I mean after the arrow; it's just like a+b=[break]c+d, but in some cases it may be psychologically bad; in these cases, add \nolinebreak, which will only prevent a break at the spot and do nothing else. – egreg Jan 4 '13 at 20:34