7

If I have long words inside \texttt blocks, in my case for writing about variable names, sometimes the word-wrapping doesn't quite work, resulting in slightly-too-long lines.

\documentclass{report}
\begin{document}
Most of the time, LaTeX line-wrapping works fine.  However, sometimes when I'm writing about code with lots of \texttt{long\_monospace\_variables}, \texttt{the\_line\_wrapping} doesn't quite get the position correct resulting in slightly-too-long lines.
\end{document}

A PDF generated from the previous code. The second line is too long. I can manually insert \linebreaks to fix this, but that feels clunky. Are there any better options?

Richie Cotton
  • 243
  • 1
  • 2
  • 7
  • 1
    How would you expect \texttt{long\_monospace\_variables} to be broken? – Manuel Jun 18 '16 at 09:05
  • 4
    @Manuel without mercy. If there is no whitespace to break at anyway, the only way is to break at the last character before it becomes too wide. – Luc Feb 24 '17 at 18:08

4 Answers4

7

Based on the answers to this question

Line wrapping on narrow pages

it seems that the best way to do this is to wrap the offending paragraph in a sloppypar environment.

\documentclass{report}
\begin{document}
\begin{sloppypar}
Most of the time, LaTeX line-wrapping works fine.  However, sometimes when I'm writing about code with lots of \texttt{long\_monospace\_variables}, \texttt{the\_line\_wrapping} doesn't quite get the position correct resulting in slightly-too-long lines.
\end{sloppypar}
\end{document}

A PDF generated from the text.  The long line issue has been fixed by the use of sloppypar.

Using a \sloppy command has a similar effect for the whole document, but this seems a little dangerous since it could break formatting elsewhere.

Richie Cotton
  • 243
  • 1
  • 2
  • 7
6

Depending on how you want the variables to be broken, you can define a macro that allows breaking with certain rules. Here I just allowed breaking after _. Apart from that, you use a more semantic command that you can redefine with flexibility.

\documentclass{report}

\newcommand*\ttvar[1]{\texttt{\expandafter\dottvar\detokenize{#1}\relax}}
\newcommand*\dottvar[1]{\ifx\relax#1\else
  \expandafter\ifx\string_#1\string_\allowbreak\else#1\fi
  \expandafter\dottvar\fi}

\begin{document}

Most of the time, \LaTeX{} line-wrapping works fine. However, sometimes when
I'm writing about code with lots of \ttvar{long_monospace_variables},
\ttvar{the_line_wrapping} doesn't quite get the position correct resulting in
slightly-too-long lines.

\end{document}

enter image description here

Manuel
  • 27,118
6

It's easy to do with the url package. I define a \longvar command which does the job:

\documentclass{report}

\usepackage{url}

\newcommand\longvar[1]{\mathchardef\UrlBreakPenalty=100
\mathchardef\UrlBigBreakPenalty=100\url{#1}}

 \begin{document}

Most of the time, LaTeX line-wrapping works fine. However, sometimes when I'm writing about code with lots and lots of \longvar{very_long_monospace_variables}, \texttt{the\_line\_wrapping} doesn't quite get the position correct resulting in slightly-too-long lines.

\end{document} 

enter image description here

Bernard
  • 271,350
3

I’ve had the same problem, and was suggested to reword the line so that the texttts don’t end up on the same line.

There’s another workaround which is to do…

\emergencystretch 5em%

… with the caveat that 5 em is already very large. This is extra stretch used to justify text in order to try and make it fit, so basically the whitespace between the words on the lines can, in emergency cases, be made larger.

Too large and it looks bad, though, so use a small-ish value and manually reword the line to make it fit is the way to go. (A bit sad that one has to do this in 2019, but I admit I wouldn’t know how to programmatically do it either.) Deploy a small emergencystretch for the simple cases, then proofread the document.

mirabilos
  • 808
  • This worked for me, thanks. I particularly like this solution because it does not involve manually defining areas where you might need to break a line, such as using a sloppypar section, and it does not require you to define characters where a line break is allowed. This is handy because when you write a document it is often not linearly from the beginning to the end. – David Wright Dec 23 '20 at 08:13
  • sloppypar wont work with long filenames where there is no spacing. – Owl Oct 25 '22 at 22:47