# Build cropped PDF image from LaTeX

I have created an image with pgfplots in a separate file and want to add the PDF that is built from that as an image in my report. The problem is that the image PDF created is a complete A4 format.

How can I just build a LaTeX file with minimal paper size? What documentclass do I need?

\documentclass{article}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
title={title here},
ylabel={workshift occupation (in \%)},
symbolic x coords={Day shift, Weekend shift},
xtick=data,
x=0.2\textwidth,
bar width=0.1\textwidth,
enlarge x limits=0.5,
legend style={
anchor=north,
at={(0.5,-0.1)},
legend columns=-1}]
{(Day shift,60) (Weekend shift,30)};
{(Day shift,30) (Weekend shift,65)};
{(Day shift,10) (Weekend shift,5)};
\end{axis}
\end{tikzpicture}
\end{document}

-
have you looked at the standalone class? – cmhughes Jun 6 '12 at 15:01
you can just add \pagestyle{empty} to your document to remove the page counter and then run pdfcrop filename.pdf in a command line window. For pdfcrop to work in windows, you need to have a perl interpreter installed. You could also use the externalization function from tikz/pgf. This would require to compile with shell-escape – Martin H Jun 6 '12 at 15:07
Standalone is the correct answer! Thanks. Place it as an answer and I will accept it. – Marnix Jun 6 '12 at 15:08
preview is also the correct answer. :-) – kiss my armpit Jun 8 '12 at 14:37

You can use the standalone document class for this.

Note that the standalone bundle offers a lot more than just cropping (as detailed in the documentation). In particular, rather than including a cropped pdf in your file, you can \input the standalone file into the mainfile using (in your example)

\documentclass{article}

\usepackage{pgfplots}   % need to load all packages needed by sub-files
\usepackage{standalone} % load as a package in the main file

\begin{document}

\input{myfigurefile}

\end{document}


This will strip away the preamble from myfigurefile.tex and include just the code between \begin{document} and \end{document}.

-

If you always compose each TikZ/PSTricks diagram (or whatever) in a separate, compilable (La)TeX input file and compile it to produce a PDF, then using preview is enough and simple!

\documentclass{article}

\usepackage{pgfplots}

\usepackage[active,tightpage]{preview}
\PreviewBorder=2pt
\PreviewEnvironment{tikzpicture}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
title={title here},
ylabel={workshift occupation (in \%)},
symbolic x coords={Day shift, Weekend shift},
xtick=data,
x=0.2\textwidth,
bar width=0.1\textwidth,
enlarge x limits=0.5,
legend style={
anchor=north,
at={(0.5,-0.1)},
legend columns=-1}]
{(Day shift,60) (Weekend shift,30)};
{(Day shift,30) (Weekend shift,65)};
{(Day shift,10) (Weekend shift,5)};
\end{axis}
\end{tikzpicture}
\end{document}


I don't think typing 3 lines below

\usepackage[active,tightpage]{preview}
\PreviewBorder=2pt
\PreviewEnvironment{tikzpicture}


will waste much time.

-