Site icon texblog

Cropping the output file to its content in LaTeX

The documentclass options a4paper or letterpaper are certainly not the right choices when creating a set of equations or vector graphics using TiKZ for embedding in another document or standalone usage. Ideally, the output file is automatically set to fit its content, possibly with some whitespace around it.

The following post describes two possible ways:

 

The standalone documentclass

Really simple, instead of using a standard documentclass such as article or report, use standalone.

 
A simple example

\documentclass[varwidth=true, border=10pt, convert={size=640x}]{standalone}
\usepackage{blindtext}
\begin{document}
\blindtext
\end{document}

I don’t show the output here. Its a png-file showing some blind-text.

 
Options used

A full list of options is provided in the standalone bundle documentation. Interestingly, the background of the figure is transparent, which may not always be desirable. As the name already suggests, standalone is a bundle and has a lot more to offer than what is shown here. However, I hope this covers the most common usage.

 
A more fancy example

The code was taken from A calendar of circles on TeXample.net. Again, the pdf output was converted to png using the standalone documentclass option convert in order to be able to embed it here.

TeXample.net has a great collection of pgf/TiKZ graphics. Go check it out if you haven’t done so yet!

 
A graphic I recently created using the standalone documentclass and the adjustbox package:

Here is the pdf, perfectly scalable.

 

The preview package

Preview is a package rather than a document-class like standalone. Therefore, it has to be loaded in the preamble. Here is the same example given for standalone above:

\documentclass[11pt]{article}
\usepackage{blindtext}
\usepackage[active, tightpage]{preview}
\setlength\PreviewBorder{10pt}%
\begin{document}
\begin{preview}
\blindtext
\end{preview}
\end{document}

Using PDFs will not affect the quality of the font and is perfectly scalable. I used “png” here for visualization purposes. The quality is reasonable, but the figure is not scalable which is why it looks so bad here.

A few notes on the code

See the documentation for more details.

Under Linux or Mac OS X if Ghostscript is installed, use the following command in the terminal to convert pdf to png:

gs -sDEVICE=png16m -dTextAlphaBits=4 -r300 -dGraphicsAlphaBits=4 -dSAFER -q -dNOPAUSE -sOutputFile=⟨outputfile⟩%d.png ⟨inputfile⟩.pdf
Exit mobile version