6

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?

1 Answers1

9

enter image description here

\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
David Carlisle
  • 757,742
  • 1
    This looks very strange, but it works... Why do you use \stop instead of \end{document}? – lukas.coenig Mar 14 '16 at 19:45
  • 2
    @lukas.coenig so you don't get a normal latex output page just the \shipout box – David Carlisle Mar 14 '16 at 20:02
  • 1
    Great solution, thanks! It'll take some time until I understand it completely ;-) – lukas.coenig Mar 15 '16 at 07:01
  • 1
    Worth mentioning you need to be using pdflatex for this to work? – Frames Catherine White Apr 26 '18 at 07:28
  • 1
    @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 08:05
  • @David Carlisle Are you sure it works for every content? I tried it with normal text and with math expression and it worked, but when I tried it with a prooftree environment (using bussproofs package) it didn't work. Do you know why? – Michael Novak Mar 03 '20 at 15:31
  • 1
    @MichaelNovak If you use \setbox0\hbox then its a horizontal box like \mbox so can not have vertical material, you could use \vbox instead (which will be full width) or you could nest a \parbox of specified width inside the hbox or .... So the code here will always make a page the size of a given box, but exactly how you make the box may depend on what you want to do. – David Carlisle Mar 03 '20 at 15:34
  • 1
    What I want is a general way for any LaTex document to have a page width as long as needed in order to fit some graphical elements that in normal settings would raise an hbox error. The reason, I can't just configure my document so it doesn't have hbox errors is that this is a document I generate automatically by a python script. To be honest I don't entirely understand how I could do it. Do you have any idea how to do it? If so, I can create a question, because I think this could be useful for other people as well. By the way, your answer did work for me with multiple normal text lines. – Michael Novak Mar 03 '20 at 16:29
  • 1
    there is another answer that shows how to ship out a page of normal width but as long as needed, I'll see if I can find..... @MichaelNovak – David Carlisle Mar 03 '20 at 16:43
  • Oh, thank you so much! You have no idea how much you are helping me. – Michael Novak Mar 03 '20 at 16:44
  • 1
    @MichaelNovak there is this one but that is not the one I had in mind https://tex.stackexchange.com/questions/49686/is-there-a-way-to-make-page-size-match-document-length/49688#49688 – David Carlisle Mar 03 '20 at 16:45
  • Oh, sorry it's not what I need. I need the same thing but instead of height width. – Michael Novak Mar 03 '20 at 17:01