About these ads

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

  • varwidth: Uses \linewidth as document-width. If the package is loaded without the option, all line-breaks are ignored.
  • border: Adds some whitespace around the content.
  • convert: Converts the output to png. Convert itself takes arguments such as size used in the example or the output image format extension. Loading the package without the option creates the output in pdf format. Standalone uses Image Magick (or Ghostscript) for conversion, different image formats are available. Please see the documentation for more information.

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 “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

  • active: If not used, the package won’t do anything.
  • \PreviewBorder: Controls the whitespace around the content. Is set through \setlength, as shown in the example.
  • preview-environment: An easy way to tell preview what to include. Another way would be to use an existing environment such as center by adding: \PreviewEnvironment{center} to the preamble and using \begin{center}...\end{center}.

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
About these ads

4 Responses to “Cropping the output file to its content in LaTeX”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 711 other followers

%d bloggers like this: