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}
  • 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 1 '18 at 19:36
  • 2
    add \sloppy somewhere before the paragraph to give tex a bit of help – David Carlisle May 1 '18 at 19:48
  • 2
    unrelated but never use \\ \\ to get a blank line in the output – David Carlisle May 1 '18 at 19:54
  • 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 1 '18 at 20:34
  • 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 2 '18 at 20:26
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}
  • +1: Never heard of the special case (regarding hyphenation) "monospaced" before. Good to know, – Dr. Manuel Kuehner May 1 '18 at 19:55
6

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}
  • 1
    You are a fan of zzz? :) – Dr. Manuel Kuehner May 1 '18 at 21:04
  • 4
    @Dr.ManuelKuehner Sorry, I realize it's hard to find z on a German keyboard. – David Carlisle May 1 '18 at 21:06
  • oh, please do mention sloppypar so it doesn't go on forever. – barbara beeton May 1 '18 at 21:30
  • 1
    @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. – David Carlisle May 1 '18 at 21:33
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.
  • I am using \usepackage[ngerman]{babel}, but it does not solve the problem. – MrYouMath May 1 '18 at 19:48
  • @MrYouMath Wait a min – Dr. Manuel Kuehner May 1 '18 at 19:49
  • @MrYouMath Now, have a look :) – Dr. Manuel Kuehner May 1 '18 at 19:54
  • 1
    Sorry, but the MWE was not using all the stuff that I had included in my preamble. I just wanted to show the problem. – MrYouMath May 1 '18 at 20:02
  • 1
    I've taken the liberty of rephrasing some of the English-language explanatons. Feel free to revert. – Mico May 1 '18 at 21:27

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.