I know it is possible to break long lines using the seqsplit package. The following code block shows a minimal working example.
\documentclass{article}
\usepackage[a4paper, showframe]{geometry}
\usepackage{seqsplit}
\begin{document}
Without using seqsplit:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
Using seqsplit:
\seqsplit{%
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ}
\end{document}
I have defined a macro that generates a string containing ASCII letters in random order called \randomStringAllASCIILetters (see code and screenshot below). The macro uses another macro called \prunelist which was extracted from this answer, it randomly obtains items from a list without repetitions.
\documentclass{article}
\usepackage{pgf}
\usepackage{forloop}
\pgfmathsetseed{\number\pdfrandomseed}
\makeatletter
\def\prunelist#1{%
\expandafter\edef\csname pgfmath@randomlist@#1\endcsname
{\the\numexpr\csname pgfmath@randomlist@#1\endcsname-1\relax}%
\count@\pgfmath@randomtemp
\loop
\expandafter\let
\csname pgfmath@randomlist@#1@\the\count@\expandafter\endcsname
\csname pgfmath@randomlist@#1@\the\numexpr\count@+1\relax\endcsname
\ifnum\count@<\csname pgfmath@randomlist@#1\endcsname\relax
\advance\count@@ne
\repeat}
\makeatother
\newcounter{asciiLetters}
\newcommand\randomStringAllASCIILetters{
% "mylist" contains all ASCII letters in lowercase and
% uppercase. The list was obtained using the following one-liner:
%
% $ python3 -c "import string; print(''.join([f'{{{x}}}' for x in string.ascii_letters]))"
\pgfmathdeclarerandomlist{mylist}{{a}{b}{c}{d}{e}{f}{g}{h}{i}{j}{k}{l}{m}{n}{o}{p}{q}{r}{s}{t}{u}{v}{w}{x}{y}{z}{A}{B}{C}{D}{E}{F}{G}{H}{I}{J}{K}{L}{M}{N}{O}{P}{Q}{R}{S}{T}{U}{V}{W}{X}{Y}{Z}}
% The following loop iterates as many times as the number of
% lowercase and uppercase ASCII letters. The length was obtained
% using the following one-liner.
%
% $ python3 -c "import string; print(len(string.ascii_letters))"
\forloop{asciiLetters}{0}{\value{asciiLetters}<52}{%
\pgfmathrandomitem\z{mylist}\z\prunelist{mylist}%
}%
}
\begin{document}
\par foo
\par \randomStringAllASCIILetters
\par bar
\end{document}
The problem is that even though the string returned by the macro \randomStringAllASCIILetters is passed to \seqsplit, the long string returned by the macro is not broken in the correct positions, so it exceeds the page limits.
\documentclass{article}
\usepackage[a4paper, showframe]{geometry}
\usepackage{seqsplit}
\usepackage{pgf}
\usepackage{forloop}
\pgfmathsetseed{\number\pdfrandomseed}
\makeatletter
\def\prunelist#1{%
\expandafter\edef\csname pgfmath@randomlist@#1\endcsname
{\the\numexpr\csname pgfmath@randomlist@#1\endcsname-1\relax}%
\count@\pgfmath@randomtemp
\loop
\expandafter\let
\csname pgfmath@randomlist@#1@\the\count@\expandafter\endcsname
\csname pgfmath@randomlist@#1@\the\numexpr\count@+1\relax\endcsname
\ifnum\count@<\csname pgfmath@randomlist@#1\endcsname\relax
\advance\count@@ne
\repeat}
\makeatother
\newcounter{asciiLetters}
\newcommand\randomStringAllASCIILetters{
% "mylist" contains all ASCII letters in lowercase and
% uppercase. The list was obtained using the following one-liner:
%
% $ python3 -c "import string; print(''.join([f'{{{x}}}' for x in string.ascii_letters]))"
\pgfmathdeclarerandomlist{mylist}{{a}{b}{c}{d}{e}{f}{g}{h}{i}{j}{k}{l}{m}{n}{o}{p}{q}{r}{s}{t}{u}{v}{w}{x}{y}{z}{A}{B}{C}{D}{E}{F}{G}{H}{I}{J}{K}{L}{M}{N}{O}{P}{Q}{R}{S}{T}{U}{V}{W}{X}{Y}{Z}}
% The following loop iterates as many times as the number of
% lowercase and uppercase ASCII letters. The length was obtained
% using the following one-liner.
%
% $ python3 -c "import string; print(len(string.ascii_letters))"
\forloop{asciiLetters}{0}{\value{asciiLetters}<52}{%
\pgfmathrandomitem\z{mylist}\z\prunelist{mylist}%
}%
}
\begin{document}
\seqsplit{%
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ}
\seqsplit{%
\randomStringAllASCIILetters\
\randomStringAllASCIILetters\
\randomStringAllASCIILetters}
\end{document}
I have compiled the above document multiple times and I have noticed that sometimes the page limit is exceeded, but sometimes it isn't.
In the following image, there is at least one character that exceeds the page limits.
In the following image, there is no character that exceeds the page limits.
The question
As we could see above, the string abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ is broken in the correct position by \seqsplit{...} so that the string doesn't exceed the page limit. However, the string returned by \randomStringAllASCIILetters exceeds the page limit even though it has been passed to \seqsplit{...}. Why does this happen?





\randomStringAllASCIILettersis not a sequence of letters. – David Carlisle Mar 16 '24 at 15:11