# How do I wrap text without spaces?

I have a very long section title which has no spaces in it. This seems to cause problems with wrapping the title in the table of content and in the header (it doesn't work). I'm also having the same problem with long lines with no spaces using the minted package.

Is there a generic fix for this?

Here is an example (also at https://www.overleaf.com/read/qyvwgrnhpbfm):

\documentclass{article}

\usepackage{minted}

\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}

• Welcome to TeX.SX! Is this a real situation or just a curiosity? Add \hspace{0pt} in front of the title, like \section{\hspace{0pt]LONG}. For minted, there's an option to break lines anywhere. – egreg Mar 21 '18 at 14:24
• There is also \allowbreak which can be inserted in specific sites. – John Kormylo Mar 21 '18 at 15:12
• Hi @egreg. This is a real situation. I'm adapting this answer and have some very long file paths (some without spaces) which need wrapping in the table of content. I tried \hspace{0pt} and got this result. – user158752 Mar 21 '18 at 15:42

Actually, this seems like two questions:

In section headings, you can use \- to allow \LaTeX to hyphenate there:

\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?}


For the minted package you can use the breakanywhere option (which egreg mentions in his comment):
\begin{minted}[breakanywhere=true]{javascript}