12

I'm having some text in an \texttt environment and it turns out that latex breaks the lines in these environments sometimes correctly and sometimes not.

A working minimum example

\documentclass{scrartcl}
\begin{document}
\section{Test}

This is some text text and even more text \texttt{and some variable names which are written in typewriter font

\end{document}

And a non-working one (leading to an overfull \hbox)

\documentclass{scrartcl}
\begin{document}
\section{Test}

This is some text text and even more text \texttt{and some variable names which are writtenin typewriter font}

\end{document}

It seems like if there is an too long word at the end of the line LaTeX isn't capable of setting a proper line break? I've found some similar problems with line breaks in \texttt, but the problem was always that there was no space in the \texttt, this is definitely not the problem here.

Is there a workaround for this problem?

1
  • LaTeX sets things so hyphenation is not tried for words in typewriter font.
    – egreg
    Oct 31, 2014 at 16:41

2 Answers 2

14

LaTeX doesn't hyphenate tt fonts, which makes it hard to set the paragraph.

You could use sloppypar to allow white space to stretch more to compensate, or you could reset the hyphen for the tt font (which is a global change, affecting tt for the rest of the document).

enter image description here

\documentclass{scrartcl}
\begin{document}

\section{Test1}

This is some text text and even more text \texttt{and some variable names which are writtenin typewriter font}

\section{Test2}
\begin{sloppypar}
This is some text text and even more text \texttt{and some variable names which are writtenin typewriter font}
\end{sloppypar}

\section{Test3}
{{\ttfamily \hyphenchar\the\font=`\-}%
This is some text text and even more text \texttt{and some variable names which are writtenin typewriter font}\par}

\end{document}
7

Because LaTeX doesn't hyphenate words that are typeset in "teletype" font (aka typewriter font, monospaced font), you may need to give up on full justification of the paragraph in question. Instead, you may wish to typeset it in \raggedright mode. To keep the scope of \raggedright local to just one paragraph, be sure to encase it in curly braces, making sure to insert \par before the closing curly brace.

\documentclass{scrartcl}
\begin{document}
\section{Test}
{\raggedright
This is some text text and even more text \texttt{and some variable names 
which are writtenin typewriter font}.
\par}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .