# Make \texttt not exceed line length

I have to state a few long numbers in a document. These aren't meant to be regarded as regular numbers but instead as data. So I don't want to format them with \num{<...>} but with \texttt{<...>} so they appear in typewriter font like code. They can't be approximated. The last digit of each one of these numbers is as important as the first one.

Unfortunately, when these numbers appear at the end of a line, they sometimes exceed the lengths of the surrounding lines by quite a margin.

Example:

\documentclass{article}

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut sit amet ornare eros. Vivamus ac bibendum sapien. Morbi efficitur iaculis elit et pellentesque. Maecenas fermentum augue non tortor rutrum, vel malesuada nunc venenatis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Integer fringilla. Suspendisse ac scelerisque tortor \texttt{1937294810383219482}. Pellentesque at iaculis risus. Curabitur porta ante non nunc eleifend, sit amet porta enim congue. Nullam semper fringilla suscipit. Nam ac elementum eros. Vivamus sed quam fringilla, tristique odio a, pretium lacus.
\end{document}


Can I tell LaTeX to just end the line early and continue in the next one?

As it's not a URL, I probably shouldn't put the numbers in \url{<...>}. This as a solution that pops up quite often when googling the problem. I can't use this answer either because these numbers can't be hyphenated. About half of them are actually negative numbers and I don't want the readers to be confused about how many numbers I mentioned and which of the digit blocks belong to the same number.

• So, what do you want LaTeX to do, if it is not allowed to break the number across lines? – GuM Mar 22 '18 at 19:00
• You have a bunch of options: (1) set \emergencystretch=\textwidth (or some really large value), the cost of which is that spaces will be really stretched out on those lines. (2) Use ragged-right typesetting, the cost of which will be giving up on justified paragraphs. (3) Have just those lines be ragged-right, with some clever tricks. This seems to be what you're asking, but I think the output will actually be confusing. – ShreevatsaR Mar 22 '18 at 19:15
• @Werner It's not a duplicate of that question exactly, because here the OP does not want to break the big number over several lines. – ShreevatsaR Mar 22 '18 at 19:15
• @ShreevatsaR Yes, that's what I want LaTeX to do in those cases. In all cases where there \texttt isn't involved, I think it does a great job at typesetting what I wrote. And yes, none of my numbers are longer than a line. They all are 64 bit two's complement numbers, so the longest they get is 20 characters (including the potential negative sign). – UTF-8 Mar 22 '18 at 19:26
• @UTF-8: In most cases you'll be better of rewording the paragraph text rather than create a sub-optimal layout. This doesn't come easy, but you may also consider a different way of presenting your content if that doesn't work. – Werner Mar 22 '18 at 19:31

As with overfull boxes in general, you have a bunch of options:

1. Manually rewrite the paragraph until the output is fine,
2. Add \usepackage{microtype} (often works magic),
3. Allow more hyphenation (something you said you don't want here),
4. Allow breaks in more places (something you said you don't want here),
5. Increase \tolerance and \emergencystretch a little (e.g. with \sloppy or sloppypar),
6. Increase \emergencystretch a lot,
7. Allow just that one line to be ragged-right (what you're asking for here),
8. Give up on justified typesetting entirely, and use ragged-right typesetting.

Here are a examples of a few of these options:

# Option 2: microtype

Doesn't always solve everything, but it's really easy to do with very little downsides.

\documentclass{article}
\usepackage{microtype}
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut sit amet ornare eros. Vivamus ac bibendum sapien. Morbi efficitur iaculis elit et pellentesque. Maecenas fermentum augue non tortor rutrum, vel malesuada nunc venenatis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Integer fringilla. Suspendisse ac scelerisque tortor \texttt{1937294810383219482}. Pellentesque at iaculis risus. Curabitur porta ante non nunc eleifend, sit amet porta enim congue. Nullam semper fringilla suscipit. Nam ac elementum eros. Vivamus sed quam fringilla, tristique odio a, pretium lacus.
\end{document}


# Option 5: Use \sloppy or sloppypar

What: Use \sloppy or the environment sloppypar, to increase \tolerance, \hfuzz and \emergencystretch a little; see here for what \sloppy does.

Pros: Not the worst typesetting. Will avoid overfull lines in most cases.

Cons: Worse typesetting than usual. Not guaranteed not to prevent overfull lines.

\documentclass{article}
\begin{document}
\begin{sloppypar}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut sit amet ornare eros. Vivamus ac bibendum sapien. Morbi efficitur iaculis elit et pellentesque. Maecenas fermentum augue non tortor rutrum, vel malesuada nunc venenatis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Integer fringilla. Suspendisse ac scelerisque tortor \texttt{1937294810383219482}. Pellentesque at iaculis risus. Curabitur porta ante non nunc eleifend, sit amet porta enim congue. Nullam semper fringilla suscipit. Nam ac elementum eros. Vivamus sed quam fringilla, tristique odio a, pretium lacus.
\end{sloppypar}
\end{document}


## Option 6: Increase \emergencystretch a lot

What: Increase \emergencystretch to a ridiculously large value.

Pros: If at all it is possible to have things fit on a line (e.g. no unbreakable text longer than a line's width), this will do it.

Cons: That line will look ugly, with lots of stretching.

\documentclass{article}

\begin{document}

{\emergencystretch=\maxdimen
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut sit amet ornare eros. Vivamus ac bibendum sapien. Morbi efficitur iaculis elit et pellentesque. Maecenas fermentum augue non tortor rutrum, vel malesuada nunc venenatis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Integer fringilla. Suspendisse ac scelerisque tortor \texttt{1937294810383219482}. Pellentesque at iaculis risus. Curabitur porta ante non nunc eleifend, sit amet porta enim congue. Nullam semper fringilla suscipit. Nam ac elementum eros. Vivamus sed quam fringilla, tristique odio a, pretium lacus.

}
\end{document}


# Option 7: Allow ragged-right line breaks just before the texttt

(What you asked for here: I think the output is questionable, but decide for yourself.)

This can be done with the right sequence of boxes, glue and penalties: first, a glue with stretch X, then a penalty (to allow a break), then a glue with stretch -X. This way, if the break is not taken, the glues cancel, while if the break is taken, you have added X amount of stretchability to the line.

\documentclass{article}

% Like \texttt, but allows the previous line to end early if it won't fit.
\newcommand{\ttbreakbefore}[1]{%
\hskip 0pt plus 1fil\relax
\penalty0
\hskip 0pt plus -1fil\relax
\texttt{#1}%
}

\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut sit amet ornare eros. Vivamus ac bibendum sapien. Morbi efficitur iaculis elit et pellentesque. Maecenas fermentum augue non tortor rutrum, vel malesuada nunc venenatis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Integer fringilla. Suspendisse ac scelerisque tortor \ttbreakbefore{1937294810383219482}. Pellentesque at iaculis risus. Curabitur porta ante non nunc eleifend, sit amet porta enim congue. Nullam semper fringilla suscipit. Nam ac elementum eros. Vivamus sed quam fringilla, tristique odio a, pretium lacus.

\end{document}


In practice you can probably make the added stretch smaller (which will mean less awkward stretching on the earlier line): for example with 13pt instead of (the infinite) 1fil, one gets:

and you can increase the value as appropriate, to trade-off between the stretching the spaces, and leaving blank space at the end.

• \sloppy cam be isolated to a single paragraph bu using the environment sloppypar. – barbara beeton Mar 22 '18 at 19:37
• In Option 7, you could also say \hfil \penalty 0 \hfilneg. More accurately, I’d say \unskip \nobreak\hfil \penalty 0 \hfilneg. – GuM Mar 22 '18 at 22:34
• @GuM Ah yes thanks, I was tweaking and trying various finite values (instead of 1fil) so used the verbose form instead of \hfil. I think the \unskip may not be desired (if the user wants it to behave like \texttt), but the \nobreak is interesting to think about. Maybe it's fine to not have it in this particular example though, but that's a useful thing to remember. – ShreevatsaR Mar 22 '18 at 22:48