5

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?

  • 1
    How would you expect \texttt{long\_monospace\_variables} to be broken? – Manuel Jun 18 '16 at 9:05
  • 1
    @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
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

4

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

3

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.

  • Nope. Use flushleft instead, preferably from ragged2e. – Reinstate Monica - M. Schröder Jun 18 '16 at 12:41
  • @MartinSchröder Thanks. If you want to write up an answer, it would be great to see how flushleft is used, and what effect it has on the text. – Richie Cotton Jun 18 '16 at 17:42
0

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.

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.