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 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
- 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
Lucas
I am curious, why not use the externalize library in tikz? It will allow you to keep the relevant code for the figure in the corresponding document it should appear in and at the same time it will compile a pdf fitting just the image exactly as you describe in the post. On subsequent compiles, tikz will just include the pre-compiled pdf when it runs into that code. For large documents with a lot of figures this can significantly speed up compile time.
tom
Hi Lucas,
You are right, externalize is definitely the better choice for multiple or complex TiKZ figures.
Here is the link to the TiKZ/PGF manual.
Best, Tom.
sidimok
Hello,
May also use “pdfcrop” –> http://pdfcrop.sourceforge.net/
tom
Thanks for the link! Tom.