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.

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.

share|improve this question
1  
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
1  
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? –  Andrew Stacey 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
add comment

3 Answers

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

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.

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

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.

enter image description here

\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}%
\newcommand{\AddBreakableChars}[1]{%
  \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}
\parbox{4cm}{\AddBreakableChars{\MyLongString}}

\bigskip
\parbox{7cm}{\AddBreakableChars{\MyLongString}}

\bigskip
\parbox{9.1cm}{\AddBreakableChars{\MyLongString}}
\end{document}
share|improve this answer
add comment

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.