1

Let's take a look at a some random fragment of text I am writing. I have $k\in{1\dots,K}$ written as a $k\in\{1\dots,K\}$ here, which gets separated and doesn't really please me:

enter image description here

What would you advise to use, in order to keep parts of the code together, to treat them as some kind of "cells"/"bricks" that are placed in the text as a whole and not divided into the next line?

  • 1
    What does \mbox{$k\in\{1\dots,K\}$ give you? – user121799 Feb 15 '18 at 19:55
  • 1
    mathjax, which processes tex-like input, is deliberately disabled on the TeX.SE site, since we're far more interested in seeing the actual (La)TeX code than in how it may be rendered on-screen. – Mico Feb 15 '18 at 19:57
  • 1
    To my taste, it's actually ok to have a line break between k\in and \{1,...,K\}. It would probably not be OK to have a line break inside \{1,...,K\}. – Mico Feb 15 '18 at 20:00
  • 1
    You can almost always place the needed code in mbox like: $\mbox{No Break This Text}$. Thus, \mbox{$k\in\{1\dots,K\}$} would keep it together in your text – koleygr Feb 15 '18 at 20:02
  • 1
    Related/duplicate: How can I prevent LaTeX from breaking inline formulas globally? – Werner Feb 15 '18 at 20:04
3

Use

 ${k\in\{1\dots,K\}}$

${...}$ cannot have a linebreak

  • Ohhh, it's that simple? Just put it into brackets, without need for some additional commands? Thank you, I didn't knew it works like this. – Kusavil Feb 15 '18 at 20:06
  • @koleygr - Actually, let's get rid of all our comments, since they're bound to be less than fully enlightening. I've already my earlier comments. – Mico Feb 15 '18 at 22:13
  • But it looses the ability to stretch and shrink the formula (just like \mbox would do as suggested in a comment above). – Frank Mittelbach Feb 16 '18 at 7:45
3

TeX is only breaking a formula automatically in 2 places: after a binary or a relational operator. They are controlled through the parameters \binoppenalty and \relpenalty. If you set both to 10000 there will be no break ever. If you do that locally in a formula then it will apply only there. So a definition like

\newcommand\nobreakformula{\relpenalty10000\relax \binoppenalty 10000\relax}

might be your friend (perhaps with a shorter command name :-))

If you want to prevent only a single break in a formula but not all, then you can use \nolinebreak at the point where TeX did break. However, you may end up with an even less desirable break so it really depends on the situation (so use this kind of fine-tuning only if your document is next to be ready). For example if in the showcase below we use $a+b =\nolinebreak c+d+e$ then TeX will break after a + which I think is worse than breaking after the equal sign.

The use of a brace group or the use of \mbox is less advisable as it will result in the formula always be set at its nominal width regardless of how much stretching or shrinking goes on inside the line. You can see this nicely in the following example:

\documentclass{article}

\usepackage{calc}

\begin{document}

\newlength\normwidth


\newcommand\myline{A test $a+b=c$ with $1+1$ formulas inside}

\settowidth\normwidth{\myline}


Spreading a line b 0, 5,15,25 and 50 points:

\indent \mbox{\myline}

\makebox[\normwidth+5pt][s]{\myline}

\makebox[\normwidth+15pt][s]{\myline}

\makebox[\normwidth+25pt][s]{\myline}

\makebox[\normwidth+50pt][s]{\myline}

\bigskip

Contrast this with using a brace group or \verb=mbox= on the first formula:

\renewcommand\myline{A test ${a+b=c}$ with $1+1$ formulas inside}

\indent \mbox{\myline}

\makebox[\normwidth+5pt][s]{\myline}

\makebox[\normwidth+15pt][s]{\myline}

\makebox[\normwidth+25pt][s]{\myline}

\makebox[\normwidth+50pt][s]{\myline}


\bigskip

\newcommand\nobreakformula{\relpenalty10000\relax \binoppenalty 10000\relax}

Testing \verb=\nobreakformula=:

Some sample text to  make this break inside the first formula 
  A test $ a+b=c + d + e$ with $1+1$ formulas inside

And now with suppression:

Some sample text to  make this break inside the first formula 
A test $\nobreakformula a+b=c + d + e$ with $1+1$ formulas inside

\end{document}

which results in this output:

enter image description here

  • Wouldn’t \nolinebreak at the appropriate spot be better than globally inhibiting breaks? – egreg Feb 16 '18 at 10:19
  • @egreg I wasn't suggesting global, I was suggesting to prevent all break within a single formula. You are right, if you use \nolinebreak (or even plain's \nobreak you can prevent a single break in a certain place. However, take the above example, that only results in TeX breaking after "a+" which is not necessarily better. But in some cases you may prefer that and I should add a remark on this above. – Frank Mittelbach Feb 16 '18 at 11:22

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.