如何强制 LaTeX 对以下文本进行“连字符”处理?这样边距就不会被覆盖(见图)。有没有办法防止这种情况发生?
编辑:这是添加后的文件 -
\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}
答案1
(编辑答案以纳入 OP 的第二个补充示例)
正如 David Carlisle 在评论中指出的那样,LaTeX 不会对等宽字体进行连字。但是,可以通过插入\-
连字点来覆盖这一点。假设可以像GaussianNB
那样进行连字Gaus\-sian\-NB
,并且进一步假设可以交换最后两个术语的顺序,则以下解决方案可以完成工作。类似的方法也适用于 OP 的第二个示例。
\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}
答案2
这用于\sloppy
允许额外的空白,并自动添加\-
小写字母后跟大写字母,我会显示几个宽度,以显示连字符。
\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}
答案3
使用babel
这个工具箱,你就有了一个可以解决这个问题的复杂工具箱。如果你能读懂德语,那么这里有一个很好的概述:
http://homepage.ruhr-uni-bochum.de/Georg.Verweyen/silbentrennenung.html
我尝试翻译一下:
-
创建破折号。只有在此位置才可以使用连字符。"=
无论是否发生连字符,都会创建破折号。在此位置可以连字符;在其他位置也可以连字符。例如,Schmutzwasser"=Auffangbecken
写成Schmutzwasser-Auffangbecken
,前一个表达式允许使用连字符里面子词Schmutzwasser
和Auffangbecken
。"~
创建不间断的破折号。在此位置,不可以进行连字符连接。\-
不创建破折号除非发生连字。只有在 指示的位置才可以进行连字\-
。这对于一次性连字需求很有用,例如在单词Decision\-Tree\-Classifier
和中MLP\-Classi\-fier
。(一般来说,这样的单词不受 babel 的连字规则的约束。)请注意,\-
即使在等宽字体排版的单词上,“也可以”使用。""
在此位置可以换行,但如果出现换行则不会显示破折号。