Skip to content

@wspr /breaklines.tex
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
\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}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.