11

Is it possible to define a function where the page is resized to exactly the dimension of an mbox? For example, I want to do something like the following:

\documentclass{report}
\thispagestyle{empty}
\begin{document}
\mbox{\Huge buffer}
\end{document}

And have the output be a PDF whose dimension are exactly the size of the bounding box around the word "buffer". Currently, I am using the program pdfcrop to trim the resulting image, but this is a hack. I was wondering if there was a way to do this in TeX natively.

Hooked
  • 4,046

4 Answers4

9

I use something like:

\newbox\mybox
\setbox\mybox=\hbox{stuff goes here}
\pdfhorigin=0pt
\pdfvorigin=0pt
\pdfpagewidth=\wd\mybox
\pdfpageheight=\ht\mybox
\advance\pdfpageheight by \dp\mybox
\shipout\box\mybox

But it works for PDFTeX/LuaTeX only.

6

You can use the standalone class with either the preview or crop option. The first was the default for v0.x and uses the preview package. The second is new with v1.0 and the default for v1.x. It boxes the content and resizes the page to fit it. This works similar to Khaled Hosny's answer but works with LaTeX in many flavors: latex (DVI mode), pdflatex, xelatex and lualatex.

\documentclass[border=0pt]{standalone}% Add maybe `crop` or `preview` manually
\begin{document}
\mbox{\Huge buffer}
\end{document}
Martin Scharrer
  • 262,582
6

For the type of things that you seem to be doing, perhaps you might also want to look at the preview package. The \mbox isn't necessary, and it gives you a way to make a series of properly trimmed images, in a single run.

Lev Bishop
  • 45,462
2

Here is one with eplain, and I think this form of \special works only with XeTeX:

\input eplain
\hoffset=-1in
\voffset=-1in
\hruledefaultheight=0pt
\hruledefaultdepth=0pt
\vruledefaultwidth=0pt
\boxitspace=1pt % amount of whitespace on all sides
\setbox0\boxit{\hbox{This content right here will be the whole page.}}
\special{papersize=\the\wd0,\the\ht0}
\box0
\bye
morbusg
  • 25,490
  • 4
  • 81
  • 162