Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
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

5 Answers 5

You may try the seqsplit package.

share|improve this answer
    
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
2  
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
3  
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.)

share|improve this answer
    
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}
share|improve this answer

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}
share|improve this answer

A general solution with LaTeX3; the command \foo has an optional argument that can be any character or box to mark the splitting point. With varwidth we round the box width to what is really necessary.

\documentclass{article}
\usepackage{varwidth}
\usepackage{xparse}

\setlength{\parindent}{0pt} % just for the example

\ExplSyntaxOn

\NewDocumentCommand{\foo}{ O{} m m }
 { 
  % some setup for the particular problem
  \begin{varwidth}[t]{#2}
  % some padding on the right
  \skip_set:Nn \rightskip { 0pt plus .5em }
  % just in case
  \skip_set:Nn \leftskip { 0pt }
  % a strut to set the first row height
  \strut

  % the main routine
  \mayer_foo:nnn { #1 } { #2 } { #3 }

  % finish off with a strut (attached from the last digit)
  \strut
  \end{varwidth}
 }

\seq_new:N \l_mayer_input_seq

\cs_new_protected:Npn \mayer_foo:nnn #1 #2 #3
 {
  % split the input into pieces
  \seq_set_split:Nnn \l_mayer_input_seq { } { #3 }

  % between any two pieces insert a discretionary
  \seq_use:Nn \l_mayer_input_seq { \discretionary{#1}{}{} }
 }

\ExplSyntaxOff

\begin{document}

\rule{2in}{0.4pt}

\foo{2in}
{17823941237490812347123904712389407123408917234890123748912034712985712389012351}

\foo[-]{2in}
{17823941237490812347123904712389407123408917234890123748912034712985712389012351}

\foo[*]{2in}
{17823941237490812347123904712389407123408917234890123748912034712985712389012351}

\bigskip
The box doesn't take more than the stated space\\
(but it can be slightly less wide)

\medskip

\fbox{\foo{2in}
{17823941237490812347123904712389407123408917234890123748912034712985712389012351}}


\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.