# Automatic page size to fit arbitrary content

Is there a way to let LaTeX automatically set the page size to fit any given content? To be clear, the code should work for ALL types of contents and both crop and stretch the page depending on its size.

For example, this code:

\documentclass[varwidth=true, border=10pt]{standalone}
\begin{document}
\begin{tabular}{cccc}
test & test & test & test
\end{tabular}
\end{document}


works as long as the table is not too large, but if the width is increased, for example like this:

\documentclass[varwidth=true, border=10pt]{standalone}
\begin{document}
\begin{tabular}{cccc}
testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest & test & test & test
\end{tabular}
\end{document}


then the page is not (sufficiently) increased in size, but the table is cut off on the right side.

Is there a good way to do this for all cases?

• Zoom out Zoom out – Johannes_B Mar 14 '16 at 17:39
• varwidth=\maxdimen, but there is an interesting effect if you need to set text in paragraphs (AKA the main purpose of using varwidth). ;-) – Paul Gessler Mar 14 '16 at 17:57
• Yeah, that's the problem... – lukas.coenig Mar 15 '16 at 7:12

\documentclass{article}
\begin{document}

\hoffset=-1in
\voffset=-1in
\setbox0\hbox{\begin{tabular}{cccc}
testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest & test & test & test
\end{tabular}}
\pdfpageheight=\dimexpr\ht0+\dp0\relax
\pdfpagewidth=\wd0
\shipout\box0

\stop

• This looks very strange, but it works... Why do you use \stop instead of \end{document}? – lukas.coenig Mar 14 '16 at 19:45
• @lukas.coenig so you don't get a normal latex output page just the \shipout box – David Carlisle Mar 14 '16 at 20:02
• Great solution, thanks! It'll take some time until I understand it completely ;-) – lukas.coenig Mar 15 '16 at 7:01
• Worth mentioning you need to be using pdflatex for this to work? – Lyndon White Apr 26 '18 at 7:28
• @LyndonWhite or xetex and at the time I wrote it probably also luatex, but now in luatex it would be \pageheight not \pdfpageheight – David Carlisle Apr 26 '18 at 8:05