Automatically placing TikZ in a float/figure

Is there a way to insert a diagram generated by Tikz/PGF directly from within a LaTex file? Currently, I am writing it in its own file, compiling it with pdflatex and then including the generated output file as an image.

• Oops, this appears to be a duplicate question tex.stackexchange.com/questions/28258/…
– puk
Feb 8, 2012 at 3:56
• If you are producing the pictures in a separate file (recommended), then you should also consider using the standalone package. An example is in this question on crop entire document around figure Feb 8, 2012 at 16:22
• @PeterGrill the standalone package appears to be a more complex version of the input command does it not?
– puk
Feb 8, 2012 at 20:25
• The standalone package can be used to produce complete compilable documents with the tikzpictures, and then you can use \input to imports those directly into the parent document without having to remove/comment the preamble required to compile the tikzpicture by itself. Feb 8, 2012 at 20:48
• @puk: Have a look at the answer to this question: tex.stackexchange.com/questions/31825/figure-preview-wrapper Feb 9, 2012 at 0:11

Yes, this is possible. If you have a file diagram.tex with your TikZ instructions, just use the following

\begin{figure*}[tbp]
\input{diagram.tex}
\caption{This is my diagram.}
\label{f:diagram}
\end{figure*}


This should work just fine.

• I was just about to flag this question as a duplicate, but then you answered. I was not aware that you could input it via an external file. Thanks.
– puk
Feb 8, 2012 at 3:55

In the preamble add \usepackage{tikz} and necessary tikz libraries and in the document

    \begin{figure}[htb]
\centering{
\resizebox{0.8\textwidth}{!}{\input{diagram.tex}}}
\caption{This is my diagram} \label{fig:diagram}
\end{figure}


This enables to control the figure dimensions in your text.

• can you describe what \resizebox{0.8\textwidth}{!} does
– puk
Feb 8, 2012 at 4:38
• @puk You can adjust the size of the picture. Change 0.8\textwidth to say 0.4\textwidth and see your self.
– user11232
Feb 8, 2012 at 7:50