4

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?

enter image description here

Edit: This is the file after adding -

enter image description here

\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}
Troy
  • 13,741
MrYouMath
  • 670
  • 7
    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} – David Carlisle May 01 '18 at 19:36
  • @DavidCarlisle I added - at all places that would be fine for me. It is better now but still not perfect. See added picture. – MrYouMath May 01 '18 at 19:45
  • 2
    add \sloppy somewhere before the paragraph to give tex a bit of help – David Carlisle May 01 '18 at 19:48
  • Please edit your LaTeX code so that it corresponds to the screenshot (or vice versa). – Mico May 01 '18 at 19:52
  • 2
    unrelated but never use \\ \\ to get a blank line in the output – David Carlisle May 01 '18 at 19:54
  • @DavidCarlisle What would you recommend for a blank line? And Why is \ \ not good? – MrYouMath May 01 '18 at 19:55
  • @DavidCarlisle: Perfect! \sloppy solved my problem :). Do I have to stop sloppy or will it automatically end after the paragraph? – MrYouMath May 01 '18 at 19:57
  • If change the type of typewriter font is an option, you could try with \usepackage{tgcursor} or with \renewcommand*\ttdefault{cmvtt}, for instance. – Fran May 01 '18 at 20:15
  • 3
    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, – David Carlisle May 01 '18 at 20:34
  • @DavidCarlisle: I think I am too slow for your reasoning :D. What answer are you referring to? What is the best way to replace \ (Enter) \ (Enter) Text to obtain a new paragraph with a black line and no indenting? A link to your answer would be great. – MrYouMath May 02 '18 at 20:00
  • 1
    \\ 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. – David Carlisle May 02 '18 at 20:26
  • @DavidCarlisle Great \usepackage{parskip} is perfect! Thank you for taking your time to share your valuable knowledge. – MrYouMath May 02 '18 at 21:32

3 Answers3

8

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.

enter image description here

\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{LinearDiscriminantAnalysis}, \zzz{QuadraticDiscriminantAnalysis},
\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{LinearDiscriminantAnalysis}, \zzz{QuadraticDiscriminantAnalysis},
\zzz{Perceptron}, \zzz{GaussianNB}, \zzz{LogisticRegression},
\zzz{DecisionTreeClassifier}, \zzz{SVC}, \zzz{MLPClassifier} and
\zzz{KNeighborsClassifier}.
\end{quote}

\begin{quote}
\begin{quote}
\zzz{LinearDiscriminantAnalysis}, \zzz{QuadraticDiscriminantAnalysis},
\zzz{Perceptron}, \zzz{GaussianNB}, \zzz{LogisticRegression},
\zzz{DecisionTreeClassifier}, \zzz{SVC}, \zzz{MLPClassifier} and
\zzz{KNeighborsClassifier}.
\end{quote}
\end{quote}
\end{document}
David Carlisle
  • 757,742
7

(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.

enter image description here

\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{QuadraticDiscriminantAnalysis}, 
\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{Quadratic\-DiscriminantAnalysis}, 
\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}
Mico
  • 506,678
3

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

enter image description here

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.
Mico
  • 506,678