如何使文字换行而不留空格?

如何使文字换行而不留空格?

我有一个很长的章节标题,其中没有空格。这似乎会导致在目录和标题中换行出现问题(不起作用)。使用 minted 包时,我也遇到了没有空格的长行问题。

有没有通用的解决办法?

以下是一个例子(也在https://www.overleaf.com/read/qyvwgrnhpbfm):

\documentclass{article}

\usepackage[hidelinks]{hyperref}
\usepackage{minted}
\usepackage[a4paper,includeheadfoot,margin=2.54cm]{geometry}

\makeatletter
\expandafter\def\csname PYGdefault@tok@err\endcsname{\def\PYGdefault@bc##1{{\strut ##1}}}
\makeatother

\setminted{
    linenos=true,
    breaklines=true,
    encoding=utf8,
    tabsize=2
}

\begin{document}

\tableofcontents

\section{AVeryLongTitleWithNoSpacesInItCasuesProblemsWhenHavingATableOfContentHowCanIHyphenateOrWrapThis?}

\begin{minted}{javascript}
/ThisIsAVeryLongRegularExpressionOrSomeVeryLongTextWhichDoesNotWrapHowCanIFixThisItShouldWrapOnToTheNextLineIsThatPossible/
\end{minted}

\end{document}

答案1

实际上,这看起来像是两个问题:

在章节标题中,您可以使用\-允许\LaTeX在那里进行连字符:

\section{A\-Very\-Long\-Title\-With\-No\-Spaces\-In\-It\-Casues\-Problems\-When\-Having\-A\-Table\-Of\-Content\-How\-Can\-I\-Hyphenate\-Or\-Wrap\-This?}

这也会在目录中起连字符(不一定在同一位置)。

对于该minted包,您可以使用该breakanywhere选项(egreg 在他的评论中提到):

\begin{minted}[breakanywhere=true]{javascript}
  /ThisIsAVeryLongRegularExpressionOrSomeVeryLongTextWhichDoesNotWrapHowCanIFixThisItShouldWrapOnToTheNextLineIsThatPossible/
\end{minted}

输出: 修改后的输出

相关内容