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)?
2 Answers
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.
- 1,121,712
-
Thanks for your answer. As you predicted, by using
\colonI 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 04 '13 at 20:29 -
5I'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 04 '13 at 20:34
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
- 51,322
-
Thanks, your recommended preamble commands combined with the sloppy command look like a good solution. – Emilio Ferrucci Jan 04 '13 at 20:36
\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 04 '13 at 19:42\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 04 '13 at 19:50\sloppyportion of the answer gave the intended result (i.e. preventing the overfull box). I should have been more explicit about that. – Scott H. Jan 04 '13 at 20:23