# width-fitted (shrunk!) verbatim that can break pages

Is there a verbatim variant that can check if the content exceeds the \textwidth, and [if so] shrink the contents (font-size) inside the verbatim to fit the textwidth? (pdflatex is an ok requirement.)

Ideally, this would work across pages, too.

• Not that I know of. Why would you want that, though? Isn't a line break preferable? Especially if you don't have control over the input and there might be very long lines.. – schtandard May 17 at 21:34
• I think using the verbatim-package you could implement a verbatim-environment which does not "spit out" the verbatim-text line by line but does collect it line by line/\hbox-command by \hbox-command into a token-register or macro. Then you can in a loop (re)use the tokens from the register/macro for creating and measuring \vboxes until you found a font-size where the width of the \vbox does not exceed \textwidth. Then do not create a \vbox but after selecting that font-size use the tokens from your token-register/macro directly. – Ulrich Diez May 17 at 21:57
• I think sometimes line breaks are better, sometimes this can be better. Someone like David C might even know how to add intelligence to this---shrink no more than X before you break lines, for example. – ivo Welch May 19 at 1:13

You can do the verbatim in a box, then look through and find the longest line, then loop again and scale all the lines so the longest line is \textwidth

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\makeatletter
\global\setbox1\vbox{}
\setbox0\vbox{%
\hsize0pt
\hfuzz\maxdimen
\begin{verbatim}
ab
abc
1234567890
123456789012345678901234567890
12345678901234567890123456789012345678901234567890
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
555
abcdefg
\cos\sin\tan
abcde abcde abcde abcde abcde abcde abcde
*
*
*
*
*
*
*
*
*
*
*
PPPPPPPPPPP
ab
abc
1234567890
123456789012345678901234567890
12345678901234567890123456789012345678901234567890
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
555
abcdefg
\cos\sin\tan
abcde abcde abcde abcde abcde abcde abcde
*
*
*
*
*
*
*
*
*
*
*
PPPPPPPPPPP
ab
abc
1234567890
123456789012345678901234567890
12345678901234567890123456789012345678901234567890
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
555
abcdefg
\cos\sin\tan
abcde abcde abcde abcde abcde abcde abcde
*
*
*
*
*
*
*
*
*
*
*
PPPPPPPPPPP
ab
abc
1234567890
123456789012345678901234567890
12345678901234567890123456789012345678901234567890
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
555
abcdefg
\cos\sin\tan
abcde abcde abcde abcde abcde abcde abcde
*
*
*
*
*
*
*
*
*
*
*
PPPPPPPPPPP

\end{verbatim}
\dimen1=0pt
\loop
\ifnum\lastnodetype=-1
\else
\ifnum\lastnodetype=11 \skip0\lastskip\unskip\global\setbox2\vbox{\vskip\skip0\unvbox2}\fi
\ifnum\lastnodetype=13 \count0\lastpenalty\unpenalty\global\setbox2\vbox{\penalty\count0\unvbox2}\fi
\ifnum\lastnodetype=1 \setbox4\lastbox\global
\setbox2\vbox{\hbox{\unhbox4}\unvbox2}%
\ifdim\wd2>\dimen0 \dimen0=\wd2 \fi
\fi
\repeat
\unvbox2
\Gscale@div\@tempa\textwidth{\dimen0}%
\loop
\ifnum\lastnodetype=-1
\else
\ifnum\lastnodetype=11 \skip0\lastskip\unskip\global\setbox2\vbox{\vskip\skip0\unvbox2}\fi
\ifnum\lastnodetype=13 \count0\lastpenalty\unpenalty\global\setbox2\vbox{\penalty\count0\unvbox2}\fi
\ifnum\lastnodetype=1 \setbox4\lastbox\global\setbox2\vbox{\hbox{\scalebox\@tempa{\unhbox4}}\unvbox2}\fi
\repeat
}

\unvbox2

\end{document}

• this is great, david! thank you. can this be coaxed into its own environment? (or macro)? – ivo Welch May 19 at 1:12