198

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.
7
  • 1
    Did you try \hbox'ing the math?
    – Joseph Wright
    Commented Feb 11, 2011 at 17:13
  • 1
    @Joseph Wright, the result is the same like with \mbox. You get an overfull box.
    – quinmars
    Commented Feb 11, 2011 at 17:41
  • 1
    Does this answer your question? How can I prevent LaTeX from breaking inline formulas globally? Commented Apr 16, 2023 at 17:12
  • I would vote not to close this question but the newer one with the lower score.
    – cabohah
    Commented Apr 16, 2023 at 18:34
  • 1
    @DonaldDuck Reading only the top answer IMHO is not a good idea.
    – cabohah
    Commented Apr 16, 2023 at 20:05

8 Answers 8

260

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}
3
  • 11
    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
    Commented Feb 11, 2011 at 17:43
  • 1
    The sloppypar is cool!
    – mavzolej
    Commented Feb 15, 2017 at 3:31
  • Note that, depending on your exact situation, enclosing the paragraph with sloppypar may be enough, without freezing the whitespace in the math environment (See @david-carlisle s answer) by using braces. So maybe try that first.
    – Eike
    Commented Feb 21, 2022 at 21:06
79

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=10000 
\relpenalty=10000 

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.

7
  • 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
    Commented Dec 16, 2014 at 2:45
  • 4
    isnt this obviously the best answer?
    – peter
    Commented Feb 28, 2019 at 13:59
  • 2
    @peter obviously I think so:-) Commented Feb 28, 2019 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). Commented Mar 1, 2019 at 9:45
  • Using \maxdimen gave me an error saying "bad mathchar 1073741823", but using 10000 worked. Commented Apr 16, 2023 at 17:10
18

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

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

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}
10

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).

6

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}
1
  • 8
    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.) Commented Feb 11, 2011 at 21:42
6

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.

3
  • You may want to elaborate on the difference between your answer and the one by Yiannis Lazarides.
    – Mico
    Commented Feb 15, 2017 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
    Commented Feb 15, 2017 at 8:18
  • @Mico Not in the tex sense, I tried to describe the line breaking behaviour. I tried to reword it. Commented Feb 15, 2017 at 8:24
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .