4

In the following text we want all instances of "BACON" to appear on the same line.

Lorem ipsum dolor sit amet, BACON BACON BACON BACON. Mauris sit amet laoreet ante.

That is, we want to disallow the insertion of a line break into "BACON [...] BACON," unless the string is so long that prohibiting a line break causes line overflow. If the string is too long, we want the BACONs to be treated as if they were just normal text. Additional commands inside of the string of bacons, such as \it and \bf should still work.

Note that \mbox will ensure that "BACON [...] BACON" will all appear on one line, but \mbox also risks line overflow.

test file:

\documentclass[twocolumn]{article}

\begin{document}
Lorem ipsum dolor sit amet, BACON BACON BACON BACON. Mauris sit amet laoreet ante.


Lorem ipsum dolor sit amet, BACON BACON BACON BACON. Mauris sit amet laoreet ante.
\end{document}
  • 2
    Would a non-breaking space (~) be a suitable solution? i.e. Lorem ipsum dolor sit amet, BACON~BACON~BACON~BACON. Mauris sit amet laoreet ante. This means it will never allow a line break between the BACON words, but everywhere else is fine, and it'll automatically insert it if needed. – Ulysses Jan 7 at 3:42
  • It would have been clearer if you had provided a test file. If I use the text you posted as a paragraph in a default article document then all the BACON appear on the same line, so you presumably have a different line length in mind. – David Carlisle Jan 7 at 8:00
  • 1
    I'm not sure where the problem is: the text will stay together if it fits a line, otherwise it won't. Can you be more specific, pointing to an example that can better explain your aim? – egreg Jan 7 at 8:56
  • I don't understand your problem. Your BACONs will stay on one line unless there are too many of them in which case some will appear on the next line. Isn't that what you want? – Peter Wilson Jan 7 at 18:58
  • @DavidCarlisle The particular string containing BACONs is short enough that all of the BACONs usually appear all on one line. You are taking the example too literally. Sometimes there is text which we do not want broken up. Having the first half of the sentence on one line and the second half of the sentence on the next line is undesirable. We could simply put a line break at the beginning, but that's not always necessary. We want a conditional line break: if X will all fit on the current line, put X on the current line, else insert a line break and put X on the next line. – IdleCustard Jan 10 at 16:43
3

Command \nolinebreak from an answer to LaTeX: Prevent line break in a span of text

\documentclass[]{article}
\begin{document} 
\begin{enumerate}
    \item No line break after BACON

%\noindent 
Lorem ipsum dolor sit amet, BACON BACON BACON BACON BACON \nolinebreak BACON. Mauris sit amet laoreet ante.


\item line break after BACON

%\noindent
Lorem ipsum dolor sit amet, BACON BACON BACON BACON BACON BACON. Mauris sit amet laoreet ante.
\end{enumerate}

\end{document}

enter image description here

  • 1
    perhaps? you should also disable hyphenation. – David Carlisle Jan 7 at 7:57
  • @DavidCarlisle Yes surely but first I don't know how to do it and second I didn't feel it was required. Yes it's a good idee! – Hafid Boukhoulda Jan 7 at 8:00
  • 1
    Note that \nolinebreak takes an optional argument, which can be usefully exploited in this situation. – GuM Jan 7 at 9:26
  • When you use \nolinebreak, the BACONs do not all appear on the same line. It's true that no line break is inserted, but a hyphen is inserted instead. – IdleCustard Jan 8 at 15:25
  • @IdleCustard To avoid hyphenation try to insert \hyphenation{BACON} somewhere in the preamble. – Hafid Boukhoulda Jan 8 at 15:38

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.