# Wrapping long lines that contain no spaces

I need to format extremely long strings of text. I would like to be able to specify their width, and have them wrap, with or without some special symbol to indicate that the line is wrapped. The problem is, however, that these strings do not contain any spaces. Here's an example of what I would like:

\foo{2in}
{17823941237490812347123904712389407123408917234890123748912034712985712389012351}


So I would like to get an hbox containing this long number, but spanning several lines, so that I get a rectangle 2in wide.

Does anyone know of a package/trick/macro that does this?

-
Welcome to tex.sx! A tip: If you indent a line by 4 spaces, then it's marked as a code sample. –  Hendrik Vogt Nov 29 '10 at 19:17

You may try the seqsplit package.

-
Nice! There was bound to be a package that does it; I'd never heard of seqsplit before. For completeness I add a compilable example in a separate answer (hope you don't mind). –  Hendrik Vogt Nov 30 '10 at 13:10
I tend to give the user some credit for basic intelligence. The package is very easy to handle. In case of problems I'm sure we will see follow-ups. –  Thorsten Donig Dec 1 '10 at 17:02
Agreed, it's very easy. Nevertheless, if one has time to post it, then some code for copy and paste is always nice. (By the way, if you had used "@Hendrik" in your comment, then I'd found it much earlier.) –  Hendrik Vogt Dec 13 '10 at 19:03
Just click the envelope symbol on the left of your name at the top of the page when logged in to get new replies. Furthermore I know when it is suggestive to post code. Trying to rule others how to post is not a good idea. –  Thorsten Donig Dec 17 '10 at 22:25
Just found your comment. (Again, as I said, if you had used "@Hendrik", then I'd found it much earlier. I do know about the (now gone) envelope and the inbox, but they rely on the use of "@"!) Concerning posting code: I'm not trying to rule others, I'm just suggesting that it's nice to post code. I've actually upvoted your answer a long time ago since it is very useful (and my suggestion was about how to make it even more useful). –  Hendrik Vogt Mar 17 '11 at 9:36

Something along these lines perhaps:

\def\breaknumberanywhere{\hskip0pt\futurelet\next\breaknumberi}
\def\breaknumberi{\ifcat\next9\expandafter\breaknumberii\fi}
\def\breaknumberii#1{#1\breaknumberanywhere}

\def\foo#1#2{\vtop{\hsize=#1\rightskip=0pt plus 1fil \leftskip=0pt
\noindent\breaknumberanywhere#2}}

Here is a long number:
\foo{2in}
{17823941237490812347123904712389407123408917234890123748912034712985712389012351}
\bye


(Edited to become a minimal working example, as requested in a comment. LaTeXifization left to the interested reader. I also set \leftskip in order to make \foo a bit more robust.)

-
Nice! Could you make an MWE out of that? One possible simplification: Replace \discretionary{}{}{} with \hskip0pt or \hspace{0pt}. (I just wanted to post an answer, too, but mine was very inefficient.) –  Hendrik Vogt Nov 29 '10 at 18:42
I've been looking for a good example that explains \futurelet; this might be it. –  Matthew Leingang Nov 29 '10 at 18:59
@Hendrik: What is an MWE? –  Harald Hanche-Olsen Nov 29 '10 at 19:07
@Harald: Oops, sorry: Minimal working example, i.e., a code that compiles as is. –  Hendrik Vogt Nov 29 '10 at 19:13
Okay, MWE provided. Nobody said I couln't use plain TeX. Oh, and I changed the \discretionary into \hskip. Good suggestion; thanks. –  Harald Hanche-Olsen Nov 29 '10 at 19:27

Thorsten told you what package to use; just for completeness I give a compilable example:

\documentclass{article}
\usepackage{seqsplit}
\newcommand\foo[2]{%
\begin{minipage}{#1}
\seqsplit{#2}
\end{minipage}
}
\begin{document}
\foo{2in}
{17823941237490812347123904712389407123408917234890123748912034712985712389012351}
\end{document}

-

This is a very old post. I am adding one solution, thinking that this answer may be helpful for any one who arrives here:

\documentclass{article}
\begin{document}

\makeatletter
\def\foo#1#2{%
\parbox{#1}{\strut\@tfor\xx:=#2\do{%
\xx\linebreak[0]%
}\strut}%
}

\foo{2in}
{17823941237490812347123904712389407123408917234890123748912034712985712389012351}

\foo{3in}
{17823941237490812347123904712389407123408917234890123748912034712985712389012351}
\end{document}

-