# How to force LaTeX to 'hyphenate' (monospaced font)

How can I force LaTeX to 'hyphenate' the following text? Such that the margins are not overridden (see picture). Is there a way to prevent this?

Edit: This is the file after adding -

\documentclass[12pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{graphicx}
\usepackage{lmodern}
\geometry{a4paper,left=25mm,right=25mm, top=2cm, bottom=2cm}
\begin{document}
\noindent \texttt{LinearDiscriminantAnalysis}, \texttt{QuadraticDiscriminantAnalysis}, \texttt{Perceptron}, \texttt{GaussianNB}, \texttt{LogisticRegression}, \texttt{DecisionTreeClassifier}, \texttt{SVC}, \texttt{MLPClassifier} and \texttt{KNeighborsClassifier}.\\
\\
Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\end{document}

• by default latex doesn't hyphenate tt you can reset that but it isn't really set up to hyphenate camel case either, so you may prefer to use \texttt{KNeighbors\-Classifier} May 1, 2018 at 19:36
• add \sloppy somewhere before the paragraph to give tex a bit of help May 1, 2018 at 19:48
• unrelated but never use \\ \\  to get a blank line in the output May 1, 2018 at 19:54
• you should never use \\ \\  or \noindent, see my answer where I specified that paragraphs are not indented but are set off with vertical space. You should almost never need \\  outside of tables and two consecutive ones (or one at the end of a paragraph) are always wrong and generate an underfull box of maximum (10000) badness, May 1, 2018 at 20:34
• \\  never ends a paragraph, if you want to end a paragraph use a blank line. If you want paragraphs to be offset with vertical space and no indent use a document class that uses that design or use the parskip package as I used in my answer. If you use \\  atth eend of a paragraph you will get bad layout and a warning "underfull hbox badness 10000" note that is the maximum badness, tex is telling you the output is as bad as it can be by its internal consistency checking. May 2, 2018 at 20:26

(edited answer to incorporate OP's second, supplemental example)

As David Carlisle has already observed in a comment, LaTeX doesn't "do" hyphenation with monospaced font faces. However, this can be overridden by inserting \- hyphenation points. Assuming that it's ok to hyphenate GaussianNB as Gaus\-sian\-NB, and assuming further that it's permissible to exchange the ordering of the final two terms, the following solution gets the job done. A similar approach works with the OP's second example as well.

\documentclass[12pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\geometry{hmargin=25mm, vmargin=2cm}
\usepackage{lmodern}
\usepackage[ngerman]{babel}

\begin{document}
\hrule % just to illustrate width of textblock

\smallskip\noindent
\texttt{LinearDiscriminantAnalysis},
\texttt{Perceptron},
\texttt{Gaus\-sian\-NB},
\texttt{LogisticRegression},
\texttt{DecisionTreeClassifier},
\texttt{SVC},
\texttt{KNeighborsClassifier} and % exchange order of final two terms
\texttt{MLPClassifier}.

\medskip\noindent
portiert werden. Hierzu z"ahlen die Klassen
\texttt{LinearDiscriminantAnalysis},
\texttt{Perceptron},
\texttt{GaussianNB},
\texttt{LogisticRegression},
\texttt{SVC},
\texttt{Deci\-sion\-TreeClassifier},
\texttt{MLPClassifier} and
\texttt{KNeighborsClassifier}.
Zur Bestimmung der G"ute werden zus"atzlich
die zwei Hilfsklassen
\texttt{confusion\_matrix} und
\texttt{accuracy\_score} importiert.

\end{document}

• +1: Never heard of the special case (regarding hyphenation) "monospaced" before. Good to know, May 1, 2018 at 19:55

This uses \sloppy to allow extra white space and adds automatic \- if a lower case letter of followed by a capital, I show several widths, to show hyphenation.

\documentclass[12pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{graphicx}
\usepackage{lmodern}

\def\zzz#1{\texttt{\zzzuc#1\relax}}

\def\zzzuc#1{%
\ifx\relax#1%
\else
#1%
\ifnum\lccode#1=#1 %
\expandafter\expandafter\expandafter\zzzlc
\else
\expandafter\expandafter\expandafter\zzzuc
\fi
\fi}
\def\zzzlc#1{%
\ifx\relax#1%
\else
\ifnum\lccode#1=#1 %
#1\expandafter\expandafter\expandafter\zzzlc
\else
\-#1\expandafter\expandafter\expandafter\zzzuc
\fi
\fi}

\usepackage{parskip}
\sloppy

\geometry{a4paper,left=25mm,right=25mm, top=2cm, bottom=2cm}
\begin{document}
\zzz{Perceptron}, \zzz{GaussianNB}, \zzz{LogisticRegression},
\zzz{DecisionTreeClassifier}, \zzz{SVC}, \zzz{MLPClassifier} and
\zzz{KNeighborsClassifier}.

Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod
tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex
ea commodi consequat. Quis aute iure reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat
cupiditat non proident, sunt in culpa qui officia deserunt mollit anim
id est laborum.

\begin{quote}
\zzz{Perceptron}, \zzz{GaussianNB}, \zzz{LogisticRegression},
\zzz{DecisionTreeClassifier}, \zzz{SVC}, \zzz{MLPClassifier} and
\zzz{KNeighborsClassifier}.
\end{quote}

\begin{quote}
\begin{quote}
\zzz{Perceptron}, \zzz{GaussianNB}, \zzz{LogisticRegression},
\zzz{DecisionTreeClassifier}, \zzz{SVC}, \zzz{MLPClassifier} and
\zzz{KNeighborsClassifier}.
\end{quote}
\end{quote}
\end{document}

• You are a fan of zzz? :) May 1, 2018 at 21:04
• @Dr.ManuelKuehner Sorry, I realize it's hard to find z on a German keyboard. May 1, 2018 at 21:06
• oh, please do mention sloppypar so it doesn't go on forever. May 1, 2018 at 21:30
• @barbarabeeton perhaps, although if the real document has lots of camel case programming terms and not so much mock latin text, using \sloppy globally isn't necessarily a bad choice. May 1, 2018 at 21:33

Using babel you have a sophisticated toolbox for this problem. If you can read Geman then here's an excellent overview:

http://homepage.ruhr-uni-bochum.de/Georg.Verweyen/silbentrennung.html

I try to translate a bit:

• - Creates a dash. Only at this position is hyphenation possible.
• "= Creates a dash whether or not hyphenation occurs. Hyphenation is possible at this position; hyphenation at other positions may also be possible. The advantage of writing, say, Schmutzwasser"=Auffangbecken instead of Schmutzwasser-Auffangbecken is that the former expression permits hyphenation inside the sub-words Schmutzwasser and Auffangbecken.
• "~ Creates a non-breaking dash. At this position, no hyphenation is possible.
• \- Does not create a dash unless hyphenation occurs. Hyphenation is possible only at positions indicated by \-. This is useful for one-off hyphenation needs, e.g., in the words Decision\-Tree\-Classifier and MLP\-Classi\-fier. (Such words will, in general, not be covered by babel's hyphenation rules.) Note that \- "works" even on words typeset in a monospaced font.
• "" At this position a line break is possible, but no dash is shown in case a line break occurs.
• I am using \usepackage[ngerman]{babel}, but it does not solve the problem. May 1, 2018 at 19:48
• @MrYouMath Wait a min May 1, 2018 at 19:49
• @MrYouMath Now, have a look :) May 1, 2018 at 19:54
• Sorry, but the MWE was not using all the stuff that I had included in my preamble. I just wanted to show the problem. May 1, 2018 at 20:02
• I've taken the liberty of rephrasing some of the English-language explanatons. Feel free to revert.
– Mico
May 1, 2018 at 21:27