129

I hope this question was not asked before. At least I haven't found it. I have an inline math equation and LaTeX is breaking it into two parts. In this case I'd prefer to have the whole and short equation in one line. Is there any way I can do that? I already tried to put it into a \mbox but that destroyed the whole layout. Here is an example:

\documentclass{minimal}
\usepackage{siunitx}
\begin{document}
A text with an inline equation which is broken in to two parts
but is not wanted right here $v_{initial} = \SI{1000}{m/s}$.
\end{document}

Here the result would be:

A text with an inline equation which is broken in to two parts but is not wanted right here v_{initial} =
1000m/s.
  • 1
    Did you try \hbox'ing the math? – Joseph Wright Feb 11 '11 at 17:13
  • 1
    @Joseph Wright, the result is the same like with \mbox. You get an overfull box. – quinmars Feb 11 '11 at 17:41
155

Put the math expression into braces {...}, then it will be a math atom and not broken at the end of the line.

${v_{initial} = \SI{1000}{m/s}}$

to prevent an overfull box use \sloppy or better the sloppypar environment:

\documentclass{article}
\usepackage{siunitx}
\begin{document}

\begin{sloppypar}
A text with an inline equation which is broken in to two parts
but is not wanted right here ${v_{initial} = \SI{1000}{m/s}}$.
\end{sloppypar}

\end{document}
  • 8
    Thanks, that was what I was looking for. But LaTeX is splitting it for good reason, maybe I'm going to re-arrange the sentence. – quinmars Feb 11 '11 at 17:43
  • 1
    The sloppypar is cool! – mavzolej Feb 15 '17 at 3:31
48

As other answers have mentioned you may use an extra set of {} however this is essentially equivalent to using \mbox{......} and like all such boxing does two things. It prevents line breaking but it also freezes all white space at its natural size and prevents stretching or shrinking, this makes it even harder to fit the unbreakable box into the paragraph.

It is usually better to prevent line breaks without freezing the white space. TeX will break after binary operators and relations and the penalty for breaking in those places are set by default in LaTeX to

\binoppenalty=700
\relpenalty=500

So if you set

\binoppenalty=\maxdimen
\relpenalty=\maxdimen

Then Line breaking will be prevented for the rest of the document (or environment) without needing to add markup to each inline expression and allowing TeX to stretch or shrink white space to fit the expression into the surrounding paragraph.

  • 3
    Perhaps better than \maxdimen is 9999, which will avoid it as much as possible, but permit it in extreme cases. From this answer. – chmullig Dec 16 '14 at 2:45
  • isnt this obviously the best answer? – peter Feb 28 at 13:59
  • @peter obviously I think so:-) – David Carlisle Feb 28 at 14:45
  • @peter Better than mine, I agree. But it wouldn't hurt to add that you can put this assignments inside the formula, if you wish to limit the effect to that one formula. It's the values at the end of math mode that counts, so put them wherever you like (but not inside a group). – Harald Hanche-Olsen Mar 1 at 9:45
14

Line breaking will not occur inside a part of a formula that is enclosed in braces:

${v_{initial} = \SI{1000}{m/s}}$
12

You can prevent the linebreak by using braces. But you will then get an overfull box:

\documentclass{minimal}
\usepackage{siunitx}
\begin{document}
A text with an inline equation which is broken in to two parts
but is not wanted right here ${v_{initial} = \SI{1000}{m/s}}$.
\end{document}
7

TeX thinks an equals sign is a good place to break an inline equation. Putting \nolinebreak right after the equals sign avoids the break (you do get an overfull box, though).

5

You can simply include a line break (\\).

\documentclass{minimal}
\usepackage{siunitx}
\begin{document}
A text with an inline equation which is broken in to two parts
but is not wanted right here \\ $v_{initial} = \SI{1000}{m/s}$.
\end{document}
  • 5
    But then the first line won't be justified. (Not that it would look good if it's justified, but unjustified may be even worse.) – Hendrik Vogt Feb 11 '11 at 21:42
4

If your are really sure that a linebreak before the equation is a good idea, you can do it manually:

\documentclass{minimal}
\usepackage{siunitx}
\begin{document}
A text with an inline equation which is broken in to two parts
but is not wanted right here \linebreak $v_{initial} = \SI{1000}{m/s}$.
\end{document}

enter image description here

In contrast to the answer of Yiannis Lazarides, this will leave the first line justified.

  • You may want to elaborate on the difference between your answer and the one by Yiannis Lazarides. – Mico Feb 15 '17 at 7:10
  • Umm, are you sure that double-backslash creates a new paragraph (in the TeX sense of the word)? Incidentally, your MWE creates a horribly underfull line if compiled with the article instead of the minimal document class. – Mico Feb 15 '17 at 8:18
  • @Mico Not in the tex sense, I tried to describe the line breaking behaviour. I tried to reword it. – user36296 Feb 15 '17 at 8:24
0

I wasn't satisfied with any of the above answers, because it would cause some equations to go outside the margins. My solution takes advantage of the \( \) LaTeX syntax, and is inspired by \filbreak: if it is not enough space on the line, it will print the equation on a new line. It will however leave some space: that is if it doesn't fit, it will look as if you did \\ before the equation.

\documentclass{minimal}
\usepackage{siunitx}
\usepackage{etoolbox}

\binoppenalty=\maxdimen
\relpenalty=\maxdimen
\def\hfillbreak{\hskip 0pt plus 1fill\relax\penalty20\hskip 0pt plus -1fill}
\preto\(\hfillbreak %<- You might want this in a separate file or something
                    %   as it might affect your IDEs syntax highlighting
\begin{document}
    A text with an inline equation which is broken in to two parts
    but is not wanted right here \( v_{initial} = \SI{1000}{m/s} \).
    More math here \( v_{initial} = \SI{1000}{m/s} \)
\end{document}

It's probably possible to do it with the $ syntax using some redefinitions and such, but I don't have time to figure it out now.

NB This might not cover all the special cases and such, but seems to work fine for my usecases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.