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}
IdleCustard
  • 1,194
  • 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 07 '19 at 03: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 07 '19 at 08: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 07 '19 at 08: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 07 '19 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 '19 at 16:43
  • @PeterWilson The BACONs will stay on one line unless there are too many of them in which case some will appear on the next line. However, we don't want SOME of the BACONs on the current line and the rest of the BACON on the next line. We want ALL of the bacon on the same line. If all of the the BACON will fit on the current line, good, put it there; else, insert a line break and put all of the BACONs on the next line. – IdleCustard Jan 10 '19 at 16:46
  • @IdleCustard Your comments seem to me not to match your question. Perhaps you could edit the question to match your comments. Anyway, what if there are too many BACONs to fit on a single line? – Peter Wilson Jan 11 '19 at 19:32
  • @PeterWilson If there are too many BACONs to fit on a single line, then we want the behavior to be is as if no special commands were inserted at all. The first few BACONs appear on the current line and the next few appear on the next line. – IdleCustard Jan 12 '19 at 01:36

1 Answers1

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 07 '19 at 07: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 07 '19 at 08:00
  • 1
    Note that \nolinebreak takes an optional argument, which can be usefully exploited in this situation. – GuM Jan 07 '19 at 09: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 08 '19 at 15:25
  • @IdleCustard To avoid hyphenation try to insert \hyphenation{BACON} somewhere in the preamble. – Hafid Boukhoulda Jan 08 '19 at 15:38
  • @GuM [What do the optional arguments to \nolinebreak` do](https://tex.stackexchange.com/questions/469209/what-are-valid-arguments-to-nolinebreak-and-what-affect-do-they-have)? – IdleCustard Jan 08 '19 at 17:25
  • HafidBoukhoulda: \hyphenation{BACON} would then only affect the example word here and not in general. You need a macro for the text which creates a group and switches of hyphenation in it. Also, as stated by @GuM you should use the optional argument, i.e. \nolinebreak[3] to heavily discurage the line break at this position but not make it impossible. – Martin Scharrer Jan 09 '19 at 06:59
  • Having a look at What is the scope of \hyphenpenalty? it seems that you can switch off hyphenation inside a group as it is done for the whole paragraph. You would need to box it :-( – Martin Scharrer Jan 09 '19 at 07:04
  • One problem is that \nolinebreak works once EXACTLY where you put it and then is not in effect after that point. To make it work in the example, you would have to insert \nolinebreak in the white-space between every single pair of BACONS. That would make the source code unreadable. ~ is not any better, as it would also have to be inserted everywhere. Imagine if for boldface text the command worked until the next white space. You would have to insert a command before each individual word. We want something like \nolinebreak which is sticky and/or accepts a large block of text. – IdleCustard Jan 12 '19 at 01:29