5

I'm having a problem that, whenever I have a lengthy region of \texttt{} at the end of a line where it continues to trail off rather than wrapping to the next line.

Here is a MWE:

\documentclass{article}

\begin{document}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean
\texttt{commodo.ligula.eget} dolor. Aenean massa. Cum sociis natoque 
penatibus et magnis dis parturient montes, nascetur ridiculus mus. 
Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. 
Nulla consequat massa quis enim. Donec pede justo, fringilla vel, 
aliquet nec, vulputate eget, arcu. 

\end{document}

Rendered example

Other users have asked how to wrap the text within the texttt, however, I'm interested in moving the entire texttt to the next line (since none of my regions are long enough that they should need to be broken up). To be clear: I want all of each texttt to be on a single line.

This is NOT a duplicate of this SO question, in which the OP wanted to solve this problem by breaking the text inside the teletype region.

  • Using an empty line and \noindent\textt{...} after Aenean does not help? But I would use another command for this, which does this for you – user31729 Jul 15 '15 at 20:48
  • Should the line before the \texttt be filled up or just broken at the point? – egreg Jul 15 '15 at 21:11
6

If you want that the line which can't contain the text in typewriter font be broken at the spot, here's a variation of the \filbreak macro in the TeXbook (page 111):

\documentclass{article}

\newcommand\wtt[1]{%
  \hfil\penalty0\hfilneg\texttt{#1}%
}

\begin{document}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean
\wtt{commodo.ligula.eget} dolor. Aenean massa. Cum sociis natoque
penatibus et magnis \wtt{dis.parturient.montes}, nascetur ridiculus mus.
Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem.
Nulla consequat massa quis enim. Donec pede justo, fringilla vel,
aliquet nec, vulputate eget, arcu.

\end{document}

enter image description here

If the break is taken at the penalty, the \hfilneg will disappear at the line break; otherwise it will cancel the \hfil.

However, in the case you have many unbreakable long chunks of text, \raggedright typesetting should be considered.

  • 1
    Wow, I've been looking for this from my beginning with TeX. I always preferred that rather than an overfull box. – Manuel Jul 15 '15 at 21:25
  • This works great! I'll be using this from now on, though I think I'm going to override the texttt function, rather than creating my own, so I don't have to replace them all in the document. – GJStein Jul 15 '15 at 22:11
2

The suggestion here would be to allow your paragraphs to be a little \sloppy:

enter image description here

\documentclass{article}

\begin{document}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean
\texttt{commodo.ligula.eget} dolor. Aenean massa. Cum sociis natoque 
penatibus et magnis dis parturient montes, nascetur ridiculus mus. 
Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. 
Nulla consequat massa quis enim. Donec pede justo, fringilla vel, 
aliquet nec, vulputate eget, arcu. 

\sloppy
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean
\texttt{commodo.ligula.eget} dolor. Aenean massa. Cum sociis natoque 
penatibus et magnis dis parturient montes, nascetur ridiculus mus. 
Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. 
Nulla consequat massa quis enim. Donec pede justo, fringilla vel, 
aliquet nec, vulputate eget, arcu. 

\end{document}

From source2e (section 63 Page styles and related commands):

\sloppy will never (well, hardly ever) produce overfull boxes, but may produce underfull ones.

If you wish the \sloppy to be isolated or localized, issue it as part of the sloppypar environment:

\begin{sloppypar}
<sloppy paragraph(s)>
\end{sloppypar}
  • I've preferred @egreg's answer because it's much cleaner, but this is no less correct. Thanks! – GJStein Jul 15 '15 at 22:18
  • @GJStein: I'd almost suggest using a \raggedright formatting for your paragraphs. – Werner Jul 15 '15 at 22:19
  • This is for my thesis, for which the formatting is largely not up to me, which is why I've tried to keep side-effects to a minimum. – GJStein Jul 15 '15 at 22:25

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.