LaTeX: Prevent line break in a span of text

Latex

Latex Problem Overview


How can I prevent LaTeX from inserting linebreaks in my \texttt{...} or \url{...} text regions? There's no spaces inside I can replace with ~, it's just breaking on symbols.

Update: I don't want to cause line overflows, I'd just rather LaTeX insert linebreaks before these regions rather than inside them.

Latex Solutions


Solution 1 - Latex

\mbox is the simplest answer. Regarding the update:

TeX prefers overlong lines to adding too much space between words on a line; I think the idea is that you will notice the lines that extend into the margin (and the black boxes it inserts after such lines), and will have a chance to revise the contents, whereas if there was too much space, you might not notice it.

Use \sloppy or \begin{sloppypar}...\end{sloppypar} to adjust this behavior, at least a little. Another possibility is \raggedright (or \begin{raggedright}...\end{raggedright}).

Solution 2 - Latex

Surround it with an \mbox{}

Solution 3 - Latex

Also, if you have two subsequent words in regular text and you want to avoid a line break between them, you can use the ~ character.

For example:

As we can see in Fig.~\ref{BlaBla}, there is nothing interesting to see. A~better place..

This can ensure that you don't have a line starting with a figure number (without the Fig. part) or with an uppercase A.

Solution 4 - Latex

Use \nolinebreak

> \nolinebreak[number] > > The \nolinebreak command prevents LaTeX from > breaking the current line at the point of the command. With the > optional argument, number, you can convert the \nolinebreak command > from a demand to a request. The number must be a number from 0 to 4. > The higher the number, the more insistent the request is.

Source: http://www.personal.ceu.hu/tex/breaking.htm#nolinebreak

Solution 5 - Latex

Define myurl command:


\def\myurl{\hfil\penalty 100 \hfilneg \hbox}




I don't want to cause line overflows,
I'd just rather LaTeX insert linebreaks before
\myurl{\tt http://stackoverflow.com/questions/1012799/}
regions rather than inside them.


Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrampionView Question on Stackoverflow
Solution 1 - LatexJouni K. SeppänenView Answer on Stackoverflow
Solution 2 - LatexMichiel BuddinghView Answer on Stackoverflow
Solution 3 - LatexUriView Answer on Stackoverflow
Solution 4 - LatexTomaszView Answer on Stackoverflow
Solution 5 - LatexAlexey MalistovView Answer on Stackoverflow