public
Created

  • Download Gist
breaklines.tex
TeX
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{calc}
\makeatletter
 
\def\test#1{%
\begingroup
\edef\@tempa{#1}%
\edef\@tempa{\detokenize\expandafter{\@tempa}}%
\expandafter\scan\@tempa\scanstop
\printwrappedtext{\wrappedtext}
\endgroup
}
 
\newcount\charcount
\newcount\wordcount
 
\def\scanstop{}
 
\def\scan{%
\futurelet\let@token\process
}
 
{\def\1{}
\global\futurelet\blankspace\1 }
 
\def\currtext{}
\def\currline{}
\def\currword{}
 
\protected\def\NL{}
 
\charcount=-1
 
\newcount\maxlinecount
 
\def\process{%
\ifx\let@token\scanstop\relax
\ifnum\numexpr\charcount+\wordcount+1\relax<\maxlinecount
\protected@edef\wrappedtext{\currtext\currline\space\currword}%
\else
\protected@edef\wrappedtext{\currtext\currline\NL\currword}%
\fi
\let\next\relax
\else\ifx\let@token\blankspace
\ifnum\numexpr\charcount+\wordcount\relax<\maxlinecount
\advance\charcount by \wordcount
\advance\charcount by 1% account for the space character
\protected@edef\currline{\currline\space\currword}%
%\showtokens{\the\charcount: \currword}
\else
\charcount=\numexpr\wordcount\relax
\protected@edef\currtext{\currtext\currline\NL}%
\let\currline\currword
\fi
\wordcount=1
\let\currword\@empty
\let\next\dochar
\else
\advance\wordcount by 1
\let\next\dochar
\fi\fi
\next}
 
\def\dochar#1{\edef\currword{\currword#1}\scan}
 
\begin{document}
 
\def\printwrappedtext#1{%
\def\NL{\\}
\setlength\@tempdima{\widthof{\ttfamily 0}}%
\@tempdima=\dimexpr \the\maxlinecount\@tempdima\relax
\noindent Hard-wrapped text:\\
\fbox{\parbox{\@tempdima}{%
\ttfamily
\frenchspacing % important!
#1}}
}
 
\noindent
\fbox{\ttfamily 12345678901234567890123456789012345678901234567890123456789012345678901234567890}\par
 
\maxlinecount=40
 
\test{%
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibu
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur auctor semper
ut, placerat ac, \string\adipiscing\space vitae, felis. Curabitur dictum gravida mauris. Nam arcu
libero, nonummy eget, consectetuer id, vulputate a, magna. Donec
vehicula augue eu neque. Pellentesque habitant morbi tristique
senectus et netus et malesuada fames ac turpis egestas. Mauris ut
leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus eu tellus sit amet tortor gravida
placerat. Integer sapien est, iaculis in, pretium quis, viverra ac,
nunc. Praesent eget sem vel leo ultrices bibendum. Aenean faucibus.
Morbi nulla. Donec varius orci eget risus. Duis
nibh mi, congue eu, accumsan eleifend, sagittis quis, diam. Duis
eget orci sit amet orci dignissim rutrum.}
 
 
\maxlinecount=60
 
\test{%
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibu
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur auctor semper
ut, placerat ac, \string\adipiscing\space vitae, felis. Curabitur dictum gravida mauris. Nam arcu
libero, nonummy eget, consectetuer id, vulputate a, magna. Donec
vehicula augue eu neque. Pellentesque habitant morbi tristique
senectus et netus et malesuada fames ac turpis egestas. Mauris ut
leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus eu tellus sit amet tortor gravida
placerat. Integer sapien est, iaculis in, pretium quis, viverra ac,
nunc. Praesent eget sem vel leo ultrices bibendum. Aenean faucibus.
Morbi nulla. Donec varius orci eget risus. Duis
nibh mi, congue eu, accumsan eleifend, sagittis quis, diam. Duis
eget orci sit amet orci dignissim rutrum.}
 
\maxlinecount=79
 
\test{%
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibu
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur auctor semper
ut, placerat ac, \string\adipiscing\space vitae, felis. Curabitur dictum gravida mauris. Nam arcu
libero, nonummy eget, consectetuer id, vulputate a, magna. Donec
vehicula augue eu neque. Pellentesque habitant morbi tristique
senectus et netus et malesuada fames ac turpis egestas. Mauris ut
leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus eu tellus sit amet tortor gravida
placerat. Integer sapien est, iaculis in, pretium quis, viverra ac,
nunc. Praesent eget sem vel leo ultrices bibendum. Aenean faucibus.
Morbi nulla. Donec varius orci eget risus. Duis
nibh mi, congue eu, accumsan eleifend, sagittis quis, diam. Duis
eget orci sit amet orci dignissim rutrum.}
 
\maxlinecount=50
 
\def\printwrappedtext#1{%
\def\NL{\MessageBreak}
\PackageWarning{breaklines}{#1}
}
 
\test{%
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibu
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur auctor semper
ut, placerat ac, \string\adipiscing\space vitae, felis. Curabitur dictum gravida mauris. Nam arcu
libero, nonummy eget, consectetuer id, vulputate a, magna. Donec
vehicula augue eu neque. Pellentesque habitant morbi tristique
senectus et netus et malesuada fames ac turpis egestas. Mauris ut
leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus eu tellus sit amet tortor gravida
placerat. Integer sapien est, iaculis in, pretium quis, viverra ac,
nunc. Praesent eget sem vel leo ultrices bibendum. Aenean faucibus.
Morbi nulla. Donec varius orci eget risus. Duis
nibh mi, congue eu, accumsan eleifend, sagittis quis, diam. Duis
eget orci sit amet orci dignissim rutrum.}
 
\end{document}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.