# Automatic line breaking of long lines of text?

Here, it is just a example. I have a line:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa

Now, I want to let LaTeX automatically wrap the line according to the width of the text block/margins.

-
Does the text have no spaces at all? –  Mico Nov 3 '11 at 10:08
thanks for your remind. there would be some space between words. –  Frank Wang Nov 3 '11 at 10:57
Are your words really that long? Where do you want the breaks? Only at spaces, or within "words"? If within "words", how do you want to indicate continuation of a word? –  Loop Space Nov 3 '11 at 11:24
this is just an example. in my tex, these world may be some virtual word that make no sense. If within "words", i want to use '-' to indicate the continuation. –  Frank Wang Nov 3 '11 at 11:32

You are probably looking for something like the seqsplit package.

\documentclass[11pt]{article}
\usepackage{seqsplit}

\begin{document}
\seqsplit{aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa}
\end{document}

## Update:

For lines with spaces the \seqsplit command can of course be used several times.

\documentclass[11pt]{article}
\usepackage{seqsplit}

\begin{document}
\seqsplit{aaaaaaaaaaaaaaaaaaaaaaaa} \seqsplit{aaaaaaaaaaaaaaaaaaaaaaaaaaaaa} \seqsplit{aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa} \seqsplit{aaaaaaaaaaaaaaa}
\end{document}
-
This answer is based on the original version of the question where there had been no spaces in the concerned expression. –  Thorsten Donig Nov 3 '11 at 15:25
This also won't add a hyphenation character to indicate that the word has been split. Not sure if that is required though. –  Peter Grill Nov 4 '11 at 15:30
@PeterGrill: I can't see where the talk was of hyphens in the question. –  Thorsten Donig Nov 4 '11 at 20:34
Yep, that is why I said not sure if it is required by the OP, but seems like a natural thing when breaking up long words. –  Peter Grill Nov 4 '11 at 20:42
It's not sure if we are talking about real words. Could also be something like DNA sequences. –  Thorsten Donig Nov 4 '11 at 20:49

If a word is too long and it does not have a hyphenation pattern, the TeX engine does not know where to insert a break. You can force it by adding a minuscule amount of glue in-between the letters. TeX will then be able to insert a break. How much glue? As it happens even 1sp which is the smallest unit can do the trick (there are 65 536 scaled points in a point, which is less than the wavelength of visible light). All we need is a scanner to scan through the letters. Here is a minimal:

\documentclass{article}
\usepackage{lipsum}
\begin{document}
\parindent0pt
\makeatletter
\def\scanfunction#1{#1}
\let\tempa\@empty
\def\scan@letters#1#2{%
\g@addto@macro{\tempa}{#1\hskip 0pt plus 1sp minus 1sp}%
\ifx#2\@empty
\else
\expandafter\scan@letters
\fi
#2}

\def\scan#1{%
\scan@letters #1\@empty
}
\scan{aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa}
\tempa

\lipsum[1]
\end{document}

Edit: egreg at chat brought to my attention that even hskip 0pt will also work.

-
This won't add a hyphenation character to indicate that the word has been split. Not sure if that is required though. –  Peter Grill Nov 4 '11 at 15:30
@PeterGrill Yes it does not add one as the individual letters now represent words. For this type of example is difficult to understand what the OP wants. One can easily extend this method for example to break letters into groups of three and hyphenate at that point. –  Yiannis Lazarides Nov 4 '11 at 17:40

You can adapt the solution to Option to break urls with carriage-return symbol? which used the hyphenat pacakge to add a breakable character after each character.

Here is the output for various widths. The last paragraph width was chosen to ensure that the hyphen was not added if the break occurred at a space.

\documentclass[border=5pt]{standalone}
\usepackage{hyphenat}
\usepackage{xstring}
\usepackage{forloop}

\newsavebox\MyBreakChar%
\sbox\MyBreakChar{\hyp}% char to display the break after non char
\newsavebox\MySpaceBreakChar%
\sbox\MySpaceBreakChar{}% char to display the break after space
\makeatletter%
\newcommand*{\BreakableChar}[1][\MyBreakChar]{%
\leavevmode%
\prw@zbreak%
\discretionary{\usebox#1}{}{}%
\prw@zbreak%
}%

\newcounter{index}%
\StrLen{#1 }[\stringLength]%
\forloop[1]{index}{1}{\value{index}<\stringLength}{%
\StrChar{#1}{\value{index}}[\currentLetter]%
\IfStrEq{\currentLetter}{ }
{\currentLetter\BreakableChar[\MySpaceBreakChar]}%
{\currentLetter\BreakableChar[\MyBreakChar]}%
}%
}%

\newcommand*{\MyLongString}{aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa caaaaaaaaaaaaaaaaaa}%

\begin{document}